【问题标题】:How to send Push Notification Firebase如何发送推送通知 Firebase
【发布时间】:2017-01-14 02:48:33
【问题描述】:

我是 Firebase 的新手,我从旧的 MySql DB 适应它的主要原因是能够发送推送通知和动态链接。在过去的两天里,我一直在尝试向一组订阅了我的 node.js 脚本中的主题的人发送通知。该脚本始终返回InternalServerError。我可以从Firebase 控制台发送通知,但这对我的应用来说还不够好,因为我需要实现动态通知(即由一个用户操作触发)。

到目前为止,我不明白官方文档中的内容并尝试按照我找到的教程进行操作,我目前在这里

app.get('/push',function(req,res){
  /*var title = req.params.title;
  var body = req.params.body;*/
//  var confName = req.params.name;
  var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
      to: '/topics/ilisten',
    //  collapse_key: 'your_collapse_key',

      notification: {
          title: 'This is Title',
          body: 'This is body'
      },

      data: {  //you can send only notification or only data(or include both)
          my_key: 'Conf Name here'
      }
  };
  fcm.send(message, function(err, response){
    if (err) {
        console.log("Something has gone wrong!"+err);
    } else {
        console.log("Successfully sent with response: ", response);
    }
});
})

我的第一个问题是我应该在to 字段中做什么,以便我应用中的所有用户都能收到通知。

我还想看看这个概念在 android 代码中的正确和完整实现。如果有人有这样的代码,请在这里分享,因为它将帮助像我这样无法理解官方文档的未来Firebase用户。

【问题讨论】:

    标签: android node.js firebase


    【解决方案1】:

    以下是使用node-gcm (https://github.com/ToothlessGear/node-gcm) 的一种方法

      var gcm = require('node-gcm');
    
      var sender = new gcm.Sender(<sender_key>);
    
      var message = new gcm.Message();
      message.addNotification('title', title);
      message.addNotification('body', body);
    
      sender.send(message, { topic: "/topics/" + topic }, function (err, response) {
        if (err) console.error(err);
        else console.log(response);
      });
    

    【讨论】:

    • @john-oreilly 我认为如果您能解释如何向所有用户发送通知,这将对未来的访问者有用。
    • @NirmalRaj 这将向所有订阅给定topic的用户发送消息
    • @john-oreilly 有没有办法向所有安装了我的应用程序的用户发送消息。为了。例如,我们可以从 firebase 控制台进行操作
    猜你喜欢
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 2022-07-16
    • 2022-08-22
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多