通过NodeJS向Firebase Cloud Messaging发送消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过NodeJS向Firebase Cloud Messaging发送消息相关的知识,希望对你有一定的参考价值。

我正在尝试使用此包实现一个简单的节点js

https://www.npmjs.com/package/fcm-node

向我的Firebase CM发送一条简单的推送消息,以便它可以触发向iPhone的推送通知

我已经在下面执行了这些步骤,不知道为什么它仍然不起作用:

console.log("START");

var FCM = require('fcm-node')

var serverKey = require('/Users/john/Desktop/Apps/APNS/node/mhn-app-firebase-adminsdk-bs45c-5ac3770488.json') 

var fcm = new FCM(serverKey)

var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
    to: '##########',
    collapse_key: 'green',

    notification: {
        title: 'Title of your push notification',
        body: 'Body of your push notification'
    },

    data: {  //you can send only notification or only data(or include both)
        cpeMac: '000000000000',
        type: 'malware'
    }
}

fcm.send(message, function(err, response){
    if (err) {
        console.log("Something has gone wrong!")

        console.log(err);

    } else {
        console.log("Successfully sent with response: ", response)
    }
})

console.log("END");

Result

我一直陷入其中

console.log(err);

有了这些消息

⚡️  node  node app.js 
START
END
Something has gone wrong!
{ Error: Messaging payload contains an invalid "collapse_key" property. Valid properties are "data" and "notification".
    at FirebaseMessagingError.FirebaseError [as constructor] (/Users/john/Desktop/Apps/APNS/node/node_modules/fcm-node/node_modules/firebase-admin/lib/utils/error.js:25:28)
    at new FirebaseMessagingError (/Users/john/Desktop/Apps/APNS/node/node_modules/fcm-node/node_modules/firebase-admin/lib/utils/error.js:130:23)
    at /Users/john/Desktop/Apps/APNS/node/node_modules/fcm-node/node_modules/firebase-admin/lib/messaging/messaging.js:465:23
    at Array.forEach (<anonymous>)
    at Messaging.validateMessagingPayload (/Users/john/Desktop/Apps/APNS/node/node_modules/fcm-node/node_modules/firebase-admin/lib/messaging/messaging.js:462:21)
    at /Users/john/Desktop/Apps/APNS/node/node_modules/fcm-node/node_modules/firebase-admin/lib/messaging/messaging.js:204:37
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
    at Function.Module.runMain (module.js:686:11)
    at startup (bootstrap_node.js:187:16)
  errorInfo: 
   { code: 'messaging/invalid-payload',
     message: 'Messaging payload contains an invalid "collapse_key" property. Valid properties are "data" and "notification".' } }

我不知道放什么作为collapse_key。如何在Firebase帐户中找到它?

答案

你需要collapse_key:“new_message”和通知:{tag:“new_message”}

console.log("START");
var FCM = require('fcm-node');
var serverKey = require('secretKeyJson_path')
var fcm = new FCM(serverKey)
var collapseKey = 'new_message';
var message = {
    to: 'client_app_token',
    data: {
           cpeMac: '000000000000',
        type: 'malware' 
    },
    notification: {
        title: 'Hello motherfucker',
        body: 'Nice body',
        tag: collapseKey,
        icon: 'ic_notification',
        color: '#18d821',
        sound: 'default',
    },
};


fcm.send(message, function(err, response){
    if (err) {
        console.log("Something has gone wrong!")

        console.log(err);

    } else {
        console.log("Successfully sent with response: ", response)
    }
})

console.log("END");

只是为了您的上下文,collapse_key意味着在设备离线时工作,并且当重新获得互联网连接时,它将仅提供离线时收到的最后通知。

以上是关于通过NodeJS向Firebase Cloud Messaging发送消息的主要内容,如果未能解决你的问题,请参考以下文章

按用户属性向用户发送 Firebase Cloud Messaging 通知

当从实时数据库中删除对象时,Firebase 函数(用 NodeJS 编写)从 Cloud Storage 中删除文件

Firebase Firestore + Cloud Function 服务器端验证应用内购买收据(Swift + NodeJS)

无法向 Firebase Cloud Messaging 发送 iOS CriticalAlert

Flutter Firebase Cloud Messaging 如何自动关闭/取消通知?

如何使用 Firebase Cloud Messaging 通过网络浏览器订阅主题