簡體   English   中英

如何在 node.js 中集成 FCM 以進行推送通知?

[英]How to integrate FCM in node.js for push notifications?

我想在 Node.js 中使用 FCM 添加推送通知的東西。為此我試過這個

還有這個還有這個

我的 nodejs 代碼

 var FCM = require('fcm-node'); var serverkey = 'SERVERKEY'; var fcm = new FCM(serverkey); var message = { to : req.body.userToken, collapse_key : 'XXX', data : { my_key: 'my value', contents: "abcv/" }, notification : { title : 'Title of the notification', body : 'Body of the notification' } }; fcm.send(message, function(err,response){ if(err) { console.log(message); console.log("Something has gone wrong !"); } else { console.log("Successfully sent with resposne :",response); } });

每當我嘗試運行此代碼並啟動我的服務器時,我總是在控制台中收到此錯誤。

 /var/www/html/chatApp/node_modules/fcm-node/lib/fcm.js:10
 function FCM(accountKey, proxy_url=null) {
                              ^
 SyntaxError: Unexpected token =
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/html/chatApp/node_modules/fcm-node/index.js:1:80)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)

有人可以解釋一下我做錯了什么嗎?

我使用 firebase-admin 包發送通知( https://firebase.google.com/docs/cloud-messaging/admin/send-messages

var admin = require("firebase-admin");

var serviceAccount = require("./firebase-adminSDK.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
});
const messaging = admin.messaging()
    var payload = {
        notification: {
            title: "This is a Notification",
            body: "This is the body of the notification message."
        },
        topic: 'topic'
        };


    messaging.send(payload)
    .then((result) => {
        console.log(result)
    })

可以按照https://firebase.google.com/docs/admin/setup#add_firebase_to_your_app 中的步驟下載 firebase-adminSDK.json

此代碼將向主題“topic”發送通知,但是,firebase-admin 包允許向特定設備發送通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM