如何在Node js中使用FCM向多个Android设备发送消息?

Abh*_*sha 3 push-notification node.js google-cloud-messaging firebase-cloud-messaging

我尝试向单个设备发送消息,即单个注册ID,它工作正常但是当试图添加多个注册ID时,它会出现"InvalidServerResponse"错误.例如,对于regTokens ='regId1'的作品; 但不适用于regTokens = ['regId1','regId2'];

var FCM = require('fcm-node');
// Add API Key
var fcm = new FCM('<server-key>');

exports.sendMessage = function (regTokens, messageToSend, callback) {
  var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
      to: regTokens,

      data: { 
        ar_message: messageToSend
      }
  };

    fcm.send(message, function(err, response){
        if (err) {
            console.log("Something has gone wrong!",err);
        } else {
            console.log("Successfully sent with response: ", response);
        }
        callback(err, 'Success');
      });
}
Run Code Online (Sandbox Code Playgroud)

AL.*_*AL. 7

更新:对于v1,似乎registration_ids不再支持.强烈建议使用主题.


发送到指定的多个注册令牌时,您必须使用registration_ids而不是to.从文档(强调我的):

此参数指定多播消息的收件人,即发送到多个注册令牌的消息.

该值应该是要向其发送多播消息的注册令牌数组.该数组必须包含至少1个且最多1000个注册令牌.要将消息发送到单个设备,请使用to参数.

仅允许使用HTTP JSON格式的多播消息.

var message = {
    registration_ids: regTokens,

   data: { 
        ar_message: messageToSend
   }
  };
Run Code Online (Sandbox Code Playgroud)