FCM提供推送通知的重复副本

时间:2018-04-25 16:09:04

标签: node.js firebase push-notification firebase-cloud-messaging

我正在使用fcm节点模块发送推送通知。试图发送到组(具有一个设备令牌的组)。并且所有它都提供相同推送的重复副本(每次4个副本)以进行单个警报。它应该只交付一个。我使用过fcm-node(版本:1.2.1),fcm-push(版本:1.1.3)等。

nodejs 代码如下所示:

Alerts.pushTry = function (cb) {
          var serverKey = "abcd"; //put your server key here
          var deviceToken = 'group_key'; // required

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

        var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
            to: deviceToken, 
            notification: {
                title: 'Group 555 test'+ ((new Date()).getTime()), 
                body: 'Body of your push notification' 
            },

            data: {
                my_key: 'my value',
                my_another_key: 'my another value'
            }
        };

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

    };

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

上述节点模块存在一些未解决的问题。我使用了请求模块,并直接连接了fcm推送通知url,而不必依赖于这些npm模块类型的未解决问题。

var request = require('request');
    request({ 
        url: 'fcm.googleapis.com/fcm/send',
        method: "POST", 
        headers: { "content-type" : "application/json", 
        "Authorization": key=${constants.FCM_SERVER_KEY}, 
        "project_id": constants.FCM_SENDER_ID, }, 
        json: message 
    }, function(error, response, body) {
        //You code
    });