node.js - 如何在 Node js 中使用 FCM 向多个 android 设备发送消息?

标签 node.js push-notification 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');
      });
}

最佳答案

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


发送到指定的多个注册 token 时,必须使用registration_ids而不是 to。来自文档(强调我的):

This parameter specifies the recipient of a multicast message, a message sent to more than one registration token.

The value should be an array of registration tokens to which to send the multicast message. The array must contain at least 1 and at most 1000 registration tokens. To send a message to a single device, use the to parameter.

Multicast messages are only allowed using the HTTP JSON format.

var message = {
    registration_ids: regTokens,

   data: { 
        ar_message: messageToSend
   }
  };

关于node.js - 如何在 Node js 中使用 FCM 向多个 android 设备发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42391683/

相关文章:

javascript - NodeJS res.body 仅在一台路由器上未定义

node.js - 在 Sequelize.js 中使用关联实现数据库规范化

ios - Swift 在发布时将音频文件添加到 bundle 中

php - GCM 使用 curl 发送 (php)

java - Amazon SNS推送通知服务

php - 一秒钟内有非常多的查询。

node.js - 从服务器自动发布/共享到 linkedin 公司页面

javascript - Node.js:模块安装在 "addRemoteTarBall"上永远挂起

Javapns 给出 InvalidDeviceTokenFormatException

android - 我无法通过 GCM 从服务器接收推送通知?