NodeJS/Javascript 如何在 FCM 中发送多个通知?
How to send multiple notifications in FCM in NodeJS/Javascript?
我想向特定设备发送多个通知,我正在尝试进行一对一通信:
router.post('/send', function(req, res){
getAccessToken().then(function(access_token){
var title = req.body.title;
var body = req.body.body;
var token = req.body.token;
request.post({
headers:{
Authorization: 'Bearer ' + access_token
},
url:"https://fcm.googleapis.com/v1/projects/MYPROJECT/messages:send",
body: JSON.stringify(
{
"message":{
"token": token,
"notification":{
"body":body,
"title":title
}
}
}
)
}, function(error, response,body){
res.end(body);
console.log(body);
});
});
});
而且确实有效。问题是,我不太了解 NodeJS/JS 中的程序,代码来自 YT 教程,我想发送多条消息。我正在尝试这个:
router.post('/sendmulticast', function(req, res){
getAccessToken().then(function(access_token){
var title = req.body.title;
var body = req.body.body;
var token = req.body.token;
for(var i = 0; i <= token.length; i++){
request.post({
headers:{
Authorization: 'Bearer ' + access_token
},
url:"https://fcm.googleapis.com/v1/projects/MYPROJECT/messages:send",
body: JSON.stringify(
{
"message":{
"token": token[i],
"notification":{
"body":body,
"title":title
}
}
}
)
}, function(error, response,body){
res.end(body);
console.log(body);
});
}
});
});
但它 returns ERR_STREAM_WRITE_AFTER_END,我想因为它是一个异步任务,所以它正在等待服务器的响应。
我正在尝试为 android 用户订阅主题,但 FCM 没有注册该主题。如果您能帮我解决这个服务器端方法,我将不胜感激。
res.end(body)
似乎是问题所在。把它放在循环之外,它应该可以工作。您可以考虑为 post 请求创建一个函数。
我想向特定设备发送多个通知,我正在尝试进行一对一通信:
router.post('/send', function(req, res){
getAccessToken().then(function(access_token){
var title = req.body.title;
var body = req.body.body;
var token = req.body.token;
request.post({
headers:{
Authorization: 'Bearer ' + access_token
},
url:"https://fcm.googleapis.com/v1/projects/MYPROJECT/messages:send",
body: JSON.stringify(
{
"message":{
"token": token,
"notification":{
"body":body,
"title":title
}
}
}
)
}, function(error, response,body){
res.end(body);
console.log(body);
});
});
});
而且确实有效。问题是,我不太了解 NodeJS/JS 中的程序,代码来自 YT 教程,我想发送多条消息。我正在尝试这个:
router.post('/sendmulticast', function(req, res){
getAccessToken().then(function(access_token){
var title = req.body.title;
var body = req.body.body;
var token = req.body.token;
for(var i = 0; i <= token.length; i++){
request.post({
headers:{
Authorization: 'Bearer ' + access_token
},
url:"https://fcm.googleapis.com/v1/projects/MYPROJECT/messages:send",
body: JSON.stringify(
{
"message":{
"token": token[i],
"notification":{
"body":body,
"title":title
}
}
}
)
}, function(error, response,body){
res.end(body);
console.log(body);
});
}
});
});
但它 returns ERR_STREAM_WRITE_AFTER_END,我想因为它是一个异步任务,所以它正在等待服务器的响应。
我正在尝试为 android 用户订阅主题,但 FCM 没有注册该主题。如果您能帮我解决这个服务器端方法,我将不胜感激。
res.end(body)
似乎是问题所在。把它放在循环之外,它应该可以工作。您可以考虑为 post 请求创建一个函数。