如何在 Node js 中使用 FCM 向多个 android 设备发送消息?
How to send message to multiple android devices using FCM in Node js?
我尝试将消息发送到单个设备,即发送到单个注册 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
。强烈建议改用主题。
发送到指定的多个注册令牌时,必须使用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
}
};
此线程的更新:使用 admin.messaging.Messaging.sendToDevice()
将消息发送到多个 android 设备。
https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging#sendToDevice
messaging.sendToDevice(registrationTokens, payload, options)
registrationTokens: Array of String (Tokens of the recipients)
payload: Message payload
options: (Optional) admin.messaging.MessagingOptions
var message = {
// to : "token", //for single device
registration_ids: ["token1", "token2"], // for Multiple device
collapse_key: 'your_collapse_key',
notification: notify,
data: payload,
};
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);
})
}
我尝试将消息发送到单个设备,即发送到单个注册 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
。强烈建议改用主题。
发送到指定的多个注册令牌时,必须使用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
}
};
此线程的更新:使用 admin.messaging.Messaging.sendToDevice()
将消息发送到多个 android 设备。
https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging#sendToDevice
messaging.sendToDevice(registrationTokens, payload, options)
registrationTokens: Array of String (Tokens of the recipients)
payload: Message payload
options: (Optional) admin.messaging.MessagingOptions
var message = {
// to : "token", //for single device
registration_ids: ["token1", "token2"], // for Multiple device
collapse_key: 'your_collapse_key',
notification: notify,
data: payload,
};
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);
})
}