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:
- 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));
- 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));