node.js 如何从节点服务器发送 Firebase 云消息传递?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38016652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:19:55  来源:igfitidea点击:

How to send Firebase Cloud Messaging from a node server?

node.jsfirebasefirebase-cloud-messaging

提问by Javier Manzano

Is there any way to send notifications from FCMfrom a node.jsserver?

有没有办法FCMnode.js服务器发送通知?

I haven't found anything about it inside documentation.

我在文档中没有找到任何关于它的内容。

回答by Frank van Puffelen

Sending messages through Firebase Cloud Messaging takes calling an HTTP end point as described in the documentation on sending downstream messages.

通过 Firebase Cloud Messaging 发送消息需要调用 HTTP 端点,如有关发送下游消息文档中所述。

Something as simple as this could do the trick:

像这样简单的事情可以解决问题:

var request = require('request');

function sendMessageToUser(deviceId, message) {
  request({
    url: 'https://fcm.googleapis.com/fcm/send',
    method: 'POST',
    headers: {
      'Content-Type' :' application/json',
      'Authorization': 'key=AI...8o'
    },
    body: JSON.stringify(
      { "data": {
        "message": message
      },
        "to" : deviceId
      }
    )
  }, function(error, response, body) {
    if (error) { 
      console.error(error, response, body); 
    }
    else if (response.statusCode >= 400) { 
      console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage+'\n'+body); 
    }
    else {
      console.log('Done!')
    }
  });

sendMessageToUser(
  "d7x...KJQ",
  { message: 'Hello puf'}
);

Update (April 2017): you can now also run code very similar to this in Cloud Functions for Firebase. See https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens

更新(2017 年 4 月):您现在还可以在 Cloud Functions for Firebase 中运行与此非常相似的代码。请参阅https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happens

回答by Hemant

//I done by this code using node- gcm module.
//We're using the express framework and the node-gcm wrapper

var express = require('express');
var gcm = require('node-gcm');
//init express
var app = express();
app.get('/push', function (req, res) {
    var message = new gcm.Message({
        data: { key1: 'hello' },
        notification: {
            title: 'SPECOZ Offers1',
            body: 'body_data'
        }
    });

    // Set up the sender with you API key, prepare your recipients' registration tokens.
    var sender = new gcm.Sender('Api_Key');
    sender.send(message, 'device_token', function (err, response) {
        if (err) {
            console.error("Error:", err);


        }

        else console.log("Response:", response);
        res.send(response);
    });

});
app.listen("pass the port number");

回答by Tony Aziz

const admin = require('firebase-admin');
  const payload = { 
   notification: {
        title: 'this is title', body: 'this is body'
    },
   data: {
           balance: 100,
          priceplanId: 1235 
  }
}  
const deviceToken ='yourtoekn' | ['yourtoekn'];
admin.messaging().sendToDevice(deviceToken, newpayload)
                .then((_response) => console.log(_response))
                .catch(error => console.log(error));

emphasized textYou can send a notification to both ios and Android devices;

强调文本您可以向 ios 和 Android 设备发送通知;