如何使用Firebase云消息传递将推送通知发送到多个设备

Joh*_*ana 3 javascript node.js express google-cloud-messaging firebase-cloud-messaging

我找到了一种从我的expressJS服务器向我的离子应用程序发送推送消息的方法,我找到了GCM.使用GCM,我可以通过令牌列表传递消息,如下所示:

 sender.send(message, {
        registrationTokens: deviceTokens
    }, function (err, response) {
        if (err) console.error(err);
        else console.log('response' + JSON.stringify(response));
    });
Run Code Online (Sandbox Code Playgroud)

但是当我发现GCM成为FCM时,我试图使用FCM做同样的事情,但直到现在还没有运气.我听说过发送主题,但我找不到一个例子.

任何人都可以举例说明如何使用FCM发送主题消息?

我的FCM代码:(只使用1个令牌)

 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));
    }
});
Run Code Online (Sandbox Code Playgroud)

小智 15

我估计你正在使用fcm push库进行推送通知,如果你想向多个用户发送相同的通知,那么使用"registration_ids"参数代替"to".此标记接受字符串数组.

例如:registration_ids:["registrationkey1","registrationkey2"].

注意:限制一次是100键.

  • 自这个答案以来,限制可能已经改变.现在是1000.https://firebase.google.com/docs/cloud-messaging/http-server-ref#table1 (4认同)