【问题标题】:How to send message to multiple android devices using FCM in Node js?如何在 Node js 中使用 FCM 向多个 android 设备发送消息?
【发布时间】:2017-07-12 12:19:51
【问题描述】:

我尝试将消息发送到单个设备,即单个注册 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');
      });
}

【问题讨论】:

  • 如果单独发送,它适用于两个注册 ID。但不适用于数组。
  • 请确保code is self-contained。目前,我们不知道 regTokens 是什么,这可能是它失败的关键。

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


【解决方案1】:
var fetch = require('node-fetch');

send_fcm_notifications()
function send_fcm_notifications(){ 

// notification object with title and text

var notification = {
  'title': 'Best Deals',
  'text': 'Mobile Devices at 50% off. Only for today'
};

// fcm device tokens array

var fcm_tokens = ["Key 1", "Key 2"]

var notification_body = {
    'notification': notification,
          'registration_ids': fcm_tokens
  }


fetch('https://fcm.googleapis.com/fcm/send', {

        'method': 'POST',

        'headers': {
          // replace authorization key with your key
          'Authorization': 'key=' + 'Authorization Key',
          'Content-Type': 'application/json'
        },

         'body': JSON.stringify(notification_body)
      })
      .then(function(response) {
        console.log(response);
      })
      .catch(function(error) 
        console.error(error);
      })
}

【讨论】:

  • 请考虑edit 来解释您发布的代码。
【解决方案2】:
var message = {
            // to : "token", //for single device
            registration_ids: ["token1", "token2"], // for Multiple device
            collapse_key: 'your_collapse_key',
            notification: notify,
            data: payload,
         };

【讨论】:

    【解决方案3】:

    此线程的更新:使用admin.messaging.Messaging.sendToDevice() 向多个安卓设备发送消息。

    https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging#sendToDevice

    messaging.sendToDevice(registrationTokens, payload, options)

    • registrationTokens:字符串数组(收件人的令牌)

    • payload:消息负载

    • 选项:(可选)admin.messaging.MessagingOptions

    【讨论】:

      【解决方案4】:

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


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

      此参数指定多播消息的接收者,该消息发送给多个注册令牌。

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

      组播消息只允许使用 HTTP JSON 格式。

      var message = {
          registration_ids: regTokens,
      
         data: { 
              ar_message: messageToSend
         }
        };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-25
        相关资源
        最近更新 更多