具有 Firebase (FCM) 推送通知的 Node js

2024-05-20

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

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,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}请求被命中后。请问有人可以帮助我吗?这个问题之前曾被问过here https://stackoverflow.com/questions/41106557/receive-gcm-push-notification-in-node-js-app/41215898#41215898.

但这对我不起作用。我也尝试过this https://www.npmjs.com/package/fcm-push and this https://www.npmjs.com/package/fcm-node.

但没有任何进展。请帮忙。


并不强制要求仅依赖于GCM,今天有很多包裹可以寄推送通知.

下面列出了两个节点包。

  • fcm-call- 你可以找到文档https://www.npmjs.com/package/fcm-call https://www.npmjs.com/package/fcm-call
  • fcm-node- 你可以找到文档https://www.npmjs.com/package/fcm-node/ https://www.npmjs.com/package/fcm-node/

使用 fcm 调用,

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 秒内向您的专用设备发送推送通知。

快乐的通知。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有 Firebase (FCM) 推送通知的 Node js 的相关文章

随机推荐