简体   繁体   English

如何发送推送通知Firebase

[英]How to send Push Notification Firebase

I am new to Firebase and the main reason I adapted to it from my old MySql DB is the ability to send push notification and dynamic links. 我是Firebase新手,从旧的MySql DB适应它的主要原因是能够发送推送通知和动态链接。 I have been trying for the past two days to send notification to a group of people who have subscribed to a topic from my node.js script. 在过去的两天里,我一直在尝试将通知发送给从我的node.js脚本订阅了某个主题的一群人。 The script always returns InternalServerError . 该脚本始终返回InternalServerError I am able to send notification from the Firebase console but that is not good enough for my app as I need to implement dynamic notification (ie triggered by one users action). 我能够从Firebase控制台发送通知,但这对我的应用程序还不够好,因为我需要实现动态通知(即由一个用户操作触发)。

So far I did not understand what was in the official docs and tried following a tutorial I found and I am currently here 到目前为止,我不了解官方文档中的内容,并尝试按照我发现的教程进行操作,目前我在这里

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);
    }
});
})

My first question is what should I do in the to field so that all the users in my app reciece the notification. 我的第一个问题是我应该在to字段中执行什么操作,以便我应用中的所有用户都能收到通知。

I would also like to take a look at correct and complete implementation of this concept with android code. 我也想看看用Android代码正确,完整地实现这个概念。 If anyone has such code please share it here as it would help the future Firebase users who cannot understand the official docs like me. 如果有人拥有这样的代码,请在此处共享它,因为它将帮助无法理解像我这样的正式文档的将来的Firebase用户。

Following is one approach using node-gcm ( https://github.com/ToothlessGear/node-gcm ) 以下是使用node-gcmhttps://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);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM