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 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"
}
}
+1
source to share