Dharmesh patel
Dharmesh patel

Reputation: 654

Send notification by firebase cloud messaging on a topic by node js

I have implemented a firebase cloud messaging which sends a notification to an android device based on its device token which works fine.

but I want to send a notification to a topic, which gets trouble to do.

'use strict';
exports.send_push = function(push_array) {
    var FCM = require('fcm-node');
    var fcm = new FCM("SERVER KEY HERE");
    var message = {//this may vary according to the message type (single recipient, multicast, topic, et cetera)
        to: 'MY TOPIC TOKEN',
//        to: "/topics/foo-bar",
        //collapse_key: 'your_collapse_key',
        notification: {
            title: 'Notification title',
            body: 'this is body'
        },
        data: {//you can send only notification or only data(or include both)
            message: "Hello, This is test notification...!"
        }
    };

    fcm.send(message, function(err, response) {
        if (err) {
            console.log("Something has gone wrong!", err);
        } else {
            console.log("Successfully sent with response: ", response);
        }
    });
}

Can anyone help to send notification on topic... here topic token and server key details are proper.

Upvotes: 0

Views: 5771

Answers (1)

Abiranjan
Abiranjan

Reputation: 557

this one works perfect !

var FCM = require('fcm-node');

var serverKey = 'server-key';
var DeviceRegistrationToken = 'reg-token';
var topic1 = '/topics/global';
var fcm = new FCM(serverKey);

var message = { 
 to: topic1,  // either DeviceRegistrationToken or topic1
 notification: {
     title: 'Test message', 
     body: 'Hello Nodejs' 
 },

};

router.route('/push')                        
.get(function(req, res, next) {

fcm.send(message, function(err, response){
if (err) {
    console.log(err);
} else {
       console.log("Successfully sent with response: ", response);
    }
 });

res.send("notification sent")

});

Upvotes: 3

Related Questions