MareCieco
MareCieco

Reputation: 157

Firebase MessagingService get push Id in onMessageReceived

I'm using FirebaseMessagingService. I'm pushing data using NodeJS like below:

exports.pushData = functions.database.ref('/Datas/{pushId}').onWrite( event => {
console.log('Push data event triggered');
/* Grab the current value of what was written to the Realtime Database */
    var valueObject = event.data.val();
/* Create a notification and data payload. They contain the notification information, and message to be sent respectively */
    const data = {
        data: {
            from_user: valueObject.from_user,
            to_user: valueObject.to_user
        }
    };

This data is storing in the Firebase like:

-L7grmlClxCd4ebzaSdo
    from_user: "Joe"
    to_user: "Jack"

I'd like to delete this data in some circumstances but I cannot access the push Id in the onMessageReceived listener. I can fetch the data using

remoteMessage.getData();

However, there is no key (push Id) value in this. Only from_user and to_user are fetching. I need to have the key of this data in the lister to delete this data later. How can I have this?

Upvotes: 0

Views: 75

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38299

The values of the wildcards in the trigger path are available in event.params.

Add that value to the data object you are sending:

const data = {
    data: {
        from_user: valueObject.from_user,
        to_user: valueObject.to_user,
        pushId: event.params.pushId // <= ADD
    }
};

Upvotes: 1

Related Questions