I was findind a way to deliver push messages from my expressJS server to my ionic app and I found GCM. With GCM I could deliver the message passing a list of tokens, like this :
sender.send(message, {
registrationTokens: deviceTokens
}, function (err, response) {
if (err) console.error(err);
else console.log('response' + JSON.stringify(response));
});
But as I found that GCM became FCM I was trying to do the same using FCM, but no luck until now. I've heard about sending topics but I couldn't find an example.
Can anyone give an example on how send topic messages using FCM ?
my FCM code: (working with just 1 token)
var FCM = require('fcm-node');
var serverKey = 'xxx';
var fcm = new FCM(serverKey);
var message = {
to: 'device-token',
notification: {
title: event.title,
body: event.information
}
};
fcm.send(message, function (err, response) {
if (err) {
console.log("Something has gone wrong! \n" + err);
} else {
console.log("Successfully sent with response: \n ", JSON.stringify(response));
}
});
I reckon you are using fcm push library for your push notification,
if you want to send same notification to multiple users then use "registration_ids" parameter instead of "to". this tag accepts an array of strings.
ex:
registration_ids:["registrationkey1","registrationkey2"].
note: limit is 100 key at a time.
I think it is documented pretty well by Google. Basically, there are two ways to send notifications to multiple groups:
Topic Messaging : You have the client subscribe to specific topics and then while sending notifications you just modify the request to target a specific topic. All the clients subscribed to that topic would receive the message.
POST request to this end point.
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=SERVER_AUTHORIZATION_KEY
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!"
}
}
How you subscribe to a specific topic depends on the device context. Documentation for Android and IOS are mentioned in the link I provide.
Device Groups : This is basically building on the approach you have provided you have the registration tokens of the devices you want to target. You can form a device group like so:
POST request
https://android.googleapis.com/gcm/notification
Content-Type:application/json
Authorization:key=API_KEY
project_id:SENDER_ID
{
"operation": "create",
"notification_key_name": "appUser-Chris",
"registration_ids": ["4", "8", "15", "16", "23", "42"]
}
The following request returns a notification_key
which you can use in the to
field to send notifications. Yes, You will have to save this notification_key
somewhere and use it simply like:
POST request
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=SERVER_AUTHORIZATION_KEY
{
"to": "aUniqueKey", //This is your notification_key
"data": {
"hello": "This is a Firebase Cloud Messaging Device Group Message!",
}
}
Ofcourse, you can add and remove devices from the group and all the other fine control. Like I mentioned, it is all documented very well and should get you started without a hiccup.