0

I am building cloud notifications based on Firebase. We are currently working on creating a server as node js and controlling cloud notifications.

However, I have searched a lot of data, and most of the above data is transmitted to only one person by receiving the token value when sending notifications.

Of course, i can use mysql to send it to a large number of people, but is it possible to send it to all users who have the app installed as a user segment like the Firebase console?

Here is the site I referenced. I made it using the script provided here.

https://www.npmjs.com/package/fcm-node

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
SungJinLim
  • 15
  • 1
  • 6

1 Answers1

3

but is it possible to send it to all users who have the app installed as a user segment like the Firebase console?

Not possible out of box. You need a user's registration token to do any sort of messaging. If you do not have all your user's registration tokens, then you'll need to retrieve it for each user. Remember to verify the tokens before persisting them to your database: https://firebase.google.com/docs/auth/admin/verify-id-tokens

And as the readme states for fcm-node:

Warning: on February 2, 2017, the Firebase Team released the admin.messaging() service to their node.js admin module. This new service makes this module kind of deprecated

With that said, you have two options:

  1. Send notifications to multiple users via their registration token:

-

const registrationTokens = [
  'abc',
  'efg',
  '123'
];

const message = {
  data: {
    score: '850'
  },
  token: registrationToken
};

const promises = [];

registrationTokens.forEach(token => {
  const promise = admin.messaging().send(message);
  promises.push(promise);
});

Promise.all(promises)
  .then(results => console.log(results))
  .catch(err => console.error(err));
  1. Or better yet, use Topic Messaging on a "user segment". You'll need to subscribe users to their respective topic. For example, Android documentation on how to do that can be found here

-

const message = {
  data: {
    score: '850'
  },
  topic: 'myUserSegmentTopic'
};

admin.messaging().send(message)
  .then(res => console.log(res))
  .error(err => console.error(err));
Cisco
  • 20,972
  • 5
  • 38
  • 60