【问题标题】:how to send notification to multiple devices using node-fcm如何使用 node-fcm 向多个设备发送通知
【发布时间】:2020-08-11 12:46:52
【问题描述】:

应用关闭时通知不起作用

const token = ["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")
            }
        });
    })

问题是如何在其中设置通知标题?并且通知不起作用应用程序在后台和前台都没有关闭,为什么?

【问题讨论】:

  • 我相信这里不需要循环,并且通知在后台不起作用
  • 错误:默认 Firebase 应用已存在。这意味着您多次调用 initializeApp() 而不提供应用程序名称作为第二个参数。在大多数情况下,您只需要调用一次 initializeApp()。但是,如果您确实要初始化多个应用程序,请将第二个参数传递给 initializeApp() 以给每个应用程序一个唯一的名称。

标签: node.js express push-notification firebase-cloud-messaging


【解决方案1】:

试试这个方法..

如果令牌在数组中并将信息传递到数据对象,则使用“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");
    }
  });
});

【讨论】:

    【解决方案2】:

    有两种选择:

    选项 1:这是针对一位用户(一个设备令牌)。

    var message = {
    
       to: token,
    
        data: {
          title: "Hi there this is title",
          message: "Hi there this is message"
        },
    
      };
    

    结果:

    成功的 JSON

    选项 2:这适用于多个用户(多个设备令牌)..

         var message = {
    
       registration_ids: token,
    
        data: {
          title: "Hi there this is title",
          message: "Hi there this is message"
        },
    
      };
    

    结果:

    成功的 JSON

    注意:创建通知数据只有一个区别。 使用 registration_ids 而不是 to

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多