I want to send the notification to my Android app developed using Ionic t from Node.Js code. I have tried following code and getting Exactly one of topic, token or condition is required.
How can I send notification all my users without any condition?
JavaScript
x
21
21
1
var serviceAccount = require("/path/to/config.json");
2
3
admin.initializeApp({
4
credential: admin.credential.cert(serviceAccount),
5
databaseURL: "https://myApp.firebaseio.com"
6
});
7
8
var message = {
9
notification: {
10
title: '$GOOG up 1.43% on the day',
11
body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
12
}
13
};
14
15
16
admin.messaging().send(message).then(res=>{
17
console.log("Success",res)
18
}).catch(err=>{
19
console.log("Error:",err)
20
})
21
Advertisement
Answer
If you want to send a notification to all users, then the best thing is to register the users to a certain topic, example food
then everyone registered to that topic will receive a notification.
In your code above, you are getting that error because you did not provide to whom you want to send the notification.
If token:
JavaScript
1
9
1
var registrationToken = 'YOUR_REGISTRATION_TOKEN'; <-- token of user
2
var message = {
3
notification: {
4
title: '$GOOG up 1.43% on the day',
5
body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
6
}
7
token: registrationToken
8
};
9
If topic:
JavaScript
1
9
1
var topic = 'food';
2
var message = {
3
notification: {
4
title: '$GOOG up 1.43% on the day',
5
body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
6
}
7
topic: topic
8
};
9
more info here:
https://firebase.google.com/docs/cloud-messaging/admin/send-messages