代码之家  ›  专栏  ›  技术社区  ›  Neha

如何将FCM集成到节点.js推送通知?

  •  0
  • Neha  · 技术社区  · 7 年前

    我想在中添加使用FCM的推送通知节点.js我试过了 this

    this also this

    我的节点代码

    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)
    

    有人能解释一下我做错了什么吗?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Pedro Silva    7 年前

    我使用firebase管理包发送通知( 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

    此代码将向主题“topic”发送通知,但是firebase管理包允许向特定设备发送通知。

    推荐文章