带有Firebase(FCM)推送通知的Node js [英] Node js with Firebase (FCM) push notification

查看:67
本文介绍了带有Firebase(FCM)推送通知的Node js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用节点js开发REST api,并且有一个休息端点来发送firebase推送通知.我的代码如下,

I'm developing REST api using node js and there is a rest endpoint to send firebase push notification. My code is below,

const bodyParser = require('body-parser');
var cors = require('cors');
var FCM = require('fcm-node');
//var FCM = require('fcm-push');

router.post('/pushmessage', function (req, res) {

    var serverKey = 'server key taken from firebase site cloud messaging tab';
    var fcm = new FCM(serverKey);
    var reqObj = req.body;
    var token = reqObj.userToken;
    console.log("Token Value  :   " + token);
    var message = {
        to: token,
        collapse_key: 'xxxxxxxxxxxxxx',
        notification: {title: 'hello', body: 'test'},
        data: {my_key: 'my value', contents: "abcv/"}
    };
    fcm.send(message, function (err, response) {
        if (err) {
            res.json({status: 0, message: err});
            console.log("error : "+err);
        } else {
            console.log("MESSAGE SEND");
            res.json({status: 1, message: response});
        }
    })
});

从android客户端应用正确生成的推送ID到达此终结点.但总是会出错 {"multicast_id":6340735554735214,成功":0,失败":1,"canonical_ids":0,结果":[{错误":"MismatchSenderId"}]}} 在请求被击中之后.请任何人可以帮助我吗?先前在此处中提出了此问题.

Generated push id from android client app hitting to this endpoint correctly. But always it gives error {"multicast_id":6340735554735214,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]} after request got hit. Please anyone can help me? This question is previously asked in here.

但这对我不起作用.我也尝试了

But this is not working for me. Also I tried this and this.

但是没有任何进展.请帮忙.

But no any progress. Please help.

推荐答案

并不是仅依赖于 GCM ,今天有很多软件包可以发送 pushNotification

It's not mandatory to depend only on GCM, today there are many packages are available for sending pushNotification.

下面列出了两个节点程序包.

Two node packages are listed below.

  • fcm-call - you can find documentation from https://www.npmjs.com/package/fcm-call
  • fcm-node - you can find documentation from https://www.npmjs.com/package/fcm-node/

使用fcm-call

Use of fcm-call,

let FCM = require('fcm-call');
const serverKey = '<Your Server Key>'; 
const referenceKey = '<Your reference key>'; //Device Key
let title = '<Your notification title here.>';
let message = '<Your message here>';

FCM.FCM(serverKey, referenceKey, title, message);

此小片段将在2-3秒内将PushNotification发送到您的专用设备.

This small snippet will send PushNotification to your dedicated device within 2-3 seconds.

快乐通知.

这篇关于带有Firebase(FCM)推送通知的Node js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆