gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:29 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com