按节点js通过firebase云消息发送关于主题的通知

时间:2016-10-21 13:40:46

标签: android node.js firebase-cloud-messaging

我已经实现了一个firebase云消息传递,它根据设备令牌向Android设备发送通知,工作正常。

但我想向一个主题发送通知,这会让你有麻烦。

'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);
        }
    });
}

任何人都可以帮助发送有关主题的通知...此处主题令牌和服务器密钥详细信息是正确的。

1 个答案:

答案 0 :(得分:1)

这个很完美!

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")

});