我有一个简短的问题。我想从云功能发送自定义值 它会监听Firebase数据库中的onWrite事件。 我的云功能代码:
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);});
现在数据库结构如下所示:
聊天
-notifications
-some ID
-username:fdsdf
-uid:9043554
如何将UiD发送到我的设备?在代码中的方式不适用于displayName ...我该怎么做呢
答案 0 :(得分:2)
您可以通过在有效负载中添加与通知密钥相同级别的数据密钥来传递通知消息中的任意键值对:
{"notification": {...},
"data" : {
"uid": 9043554
}
}
答案 1 :(得分:1)
您需要为数据库中的每个用户保存通知令牌。
Here the documentation to get this notification token.
Here a great sample to send Push Notifications with Firebase Cloud Functions.
要自定义您的有效负载:
{
"to": "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification": {
"body": "great match!",
"title": "Portugal vs. Denmark",
"icon": "myicon"
},
"data": {
"Nick": "Mario",
"Room": "PortugalVSDenmark"
}
}