4

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?

var serviceAccount = require("/path/to/config.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://myApp.firebaseio.com"
});

var message = {
    notification: {
      title: '$GOOG up 1.43% on the day',
      body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
    }
  };


admin.messaging().send(message).then(res=>{
    console.log("Success",res)
}).catch(err=>{
    console.log("Error:",err)
})
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Santosh Hegde
  • 3,420
  • 10
  • 35
  • 51

1 Answers1

8

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:

var registrationToken = 'YOUR_REGISTRATION_TOKEN'; <-- token of user
var message = {
notification: {
  title: '$GOOG up 1.43% on the day',
  body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
  }
token: registrationToken
};

If topic:

var topic = 'food';
var message = {
notification: {
  title: '$GOOG up 1.43% on the day',
  body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
  }
  topic: topic
};

more info here:

https://firebase.google.com/docs/cloud-messaging/admin/send-messages

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • 2
    There is no way to specify the package like how I can send all users in Firebase notification console? – Santosh Hegde Mar 18 '18 at 14:28
  • do you want to send from the console? – Peter Haddad Mar 18 '18 at 14:29
  • Nope. I want to send through Node.JS code. But I want to mention the package name like `com.a.b` like we specify in the console and all users who have app will get notified. – Santosh Hegde Mar 18 '18 at 14:31
  • ahh this is an example: https://stackoverflow.com/questions/40052813/android-push-notifcation-toward-an-specified-application-package (only way is through topics) – Peter Haddad Mar 18 '18 at 14:35