code-8
code-8

Reputation: 58662

POST message to Firebase Cloud Messaging via NodeJS

I'm trying to implement a simple node js using this package

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

to make 1 simple push message to my Firebase CM, so it can trigger a push notificcation to an iPhone

I already did these steps below, not sure why it still NOT working :

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

I kept falling into

console.log(err);

with these messages

⚡️  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".' } }

I don't know what to put as a collapse_key. How do I find it on my Firebase account?

Upvotes: 0

Views: 2874

Answers (1)

Manish J
Manish J

Reputation: 736

You need collapse_key: "new_message" and notification: { 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");

just for your context, collapse_key is meant to work when the device is offline, and when regains internet connection it will only deliver the last notification received while offline.

Upvotes: 2

Related Questions