如何使用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