如何解决如何使用node-fcm将通知发送到多个设备
关闭应用程序后通知不起作用
const令牌= [“ AAASDfsadfasdfaFDSDFSDLFJLSDAJF; DSJFAsdfdsfsdf”];
app.get('/send-notfication',(req,res) => {
var FCM = require('fcm-node');
var fcm = new FCM(serverKey);
var messageToSend = "Hi there this is message";
var message = {
to: token,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.results);
res.send("Successfully sent")
}
});
})
问题是如何在其中设置通知标题的?并且通知不起作用,应用程序既不在后台也没有在前台关闭,为什么?
解决方法
尝试这种方式..
如果令牌位于数组中并将信息传递到数据对象中,请使用“ registration_ids”而不是“ to”。
app.get("/send-fcm-notification",(req,res) => {
var FCM = require("fcm-node");
var fcm = new FCM(serverKey);
var message = {
registration_ids: token,data: {
title: "Hi there this is title",message: "Hi there this is message"
},};
fcm.send(message,function (err,response) {
if (err) {
console.log("Something has gone wrong!",err);
} else {
console.log("Successfully sent with response: ",response.results);
res.send("Successfully sent");
}
});
});
,
有两种选择:
选项 1:这是一个用户(一个设备令牌)。
var message = {
to: token,};
结果:
成功 JSON
选项 2:这适用于多个用户(多个设备令牌)..
var message = {
registration_ids: token,};
结果:
成功 JSON
注意:创建通知数据只有一处不同。 使用 registration_ids 而不是 to。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。