如何在Node js中使用FCM将消息发送到多个android设备?
问题描述:
我试着发送消息到单个设备,即单个注册ID,它工作正常,但当试图添加多个注册ID时,它会给出'InvalidServerResponse'错误。 例如适用于regTokens ='regId1'; 但不适用于regTokens = ['regId1','regId2'];如何在Node js中使用FCM将消息发送到多个android设备?
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
不再支持。强烈建议使用主题来代替。
当发送到指定多个注册标记,则必须使用registration_ids
代替to
。来自docs(重点是我的):
此参数指定多播消息的接收者,即发送给多个注册令牌的消息。
该值应该是向其发送多播消息的注册令牌的数组。该阵列必须包含至少1个和至多1000个注册令牌。要将消息发送到单个设备,请使用to参数。
只允许使用HTTP JSON格式的组播消息。
var message = {
registration_ids: regTokens,
data: {
ar_message: messageToSend
}
};
它适用于两个注册ID如果单独发送。但不适用于数组。 – Abhilasha
请确保[代码是独立的](http://*.com/help/mcve)。现在,我们不知道'regTokens'是什么,这可能对它失败的原因非常重要。 –