如何在Node js中使用FCM向多个Android设备发送消息? [英] How to send message to multiple android devices using FCM in Node js?

查看:95
本文介绍了如何在Node js中使用FCM向多个Android设备发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried sending message to single device i.e. to single Registration id and it worked fine but when tried to add multiple Registration Ids it gives 'InvalidServerResponse' error. e.g. Works for regTokens = 'regId1'; But doesn't work for 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 .强烈建议使用主题代替.

Update: For v1, it seems that registration_ids is no longer supported. It is strongly suggested that topics be used instead.

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

When sending to specified multiple registration tokens, you must use registration_ids instead of to. From the docs (emphasis mine):

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

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

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

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.

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

Multicast messages are only allowed using the HTTP JSON format.

var message = {
    registration_ids: regTokens,

   data: { 
        ar_message: messageToSend
   }
  };

这篇关于如何在Node js中使用FCM向多个Android设备发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆