Sending arbitrary data using Firebase cloud messaging notification style

I have a short question. I want to send a custom value from a cloud function that listens for onWrite events in Firebase database. My Cloud Code feature:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.pushNotification = functions.database.ref('/chats/notifications/{pushId}').onWrite( event => {
  var values = event.data.val();

const payload = {
    notification: {
        title: values.username,
        body: values.message,
        sound: "default",
        displayName: values.username,
        UiD:  values.recieverUiD
    },
};
const options = {
    priority: "high"
};
return admin.messaging().sendToTopic(values.recieverUiD, payload, options);});

      

And now the database structure looks like this:

chats

-notifications

-th identifier

-username: fdsdf

-uid: 9043554

How can I send UiD to my device? the way as in code doesn't work with displayName ... how should i do it

+3


source to share


2 answers


You can pass arbitrary key pairs in a notification by adding the data key at the same level as the notification key in your payload:



{"notification": {...},
"data" : {
      "uid": 9043554
    }
}

      

+1


source


You need to store a notification token for each user in your database.

Here's the documentation for getting this notification token.

Here's a great sample for sending push notifications with Firebase cloud features.



To customize your payload:

{
  "to": "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
  "notification": {
    "body": "great match!",
    "title": "Portugal vs. Denmark",
    "icon": "myicon"
  },
  "data": {
    "Nick": "Mario",
    "Room": "PortugalVSDenmark"
  }
}

      

Documentation on FCM messages.

+1


source







All Articles