【发布时间】:2021-02-16 05:18:51
【问题描述】:
我想向特定设备发送多个通知,我正在尝试进行 1-1 通信:
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);
});
}
});
});
但它返回 ERR_STREAM_WRITE_AFTER_END,我想因为它是一个异步任务,它正在等待服务器的响应。
我正在尝试为 android 用户订阅主题,但 FCM 没有注册该主题。如果您能帮助我使用这种服务器端方法,我将不胜感激。
【问题讨论】:
标签: javascript android node.js android-studio firebase-cloud-messaging