【问题标题】:Send notification by firebase cloud messaging on a topic by node js通过节点 js 通过 Firebase 云消息发送关于主题的通知
【发布时间】:2016-10-21 13:40:45
【问题描述】:

我已经实现了一个 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);
        }
    });
}

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

【问题讨论】:

  • 您收到什么错误?
  • 抱歉回复晚了,实际上它只返回success=0。我认为这个脚本不适用于主题。我们将需要对其进行一些更改。这就是我所问的。

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


【解决方案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")

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-18
    • 2016-12-08
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多