3

我有一个简短的问题。我想从云端功能 发送一个自定义值,该值会侦听Firebase数据库中的onWrite事件。 我对云计算的功能代码:发送具有通知样式的任意数据Firebase云消息传送

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

- 一些ID

-username:fdsdf

-uid:9043554

如何将UiD发送到我的设备?代码的方式不适用于displayName ...我该怎么做

+0

我没有在数据库结构上作弊或什么不工作 –

+0

我用supscibing发送通知给我的UID主题。在节点脚本中,我将它发送给主题。并且知道我只想知道如何发送自定义值并通过remoteMessage.getData.get(“userUiD”)将其接收到我的FirebaseMessagingService中; –

回答

1

您可以通过添加一个数据键同级别传递通知消息的任意键值对作为您的有效载荷中的通知密钥:

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

谢谢。我只是忘了它,但无法在Google上找到它 –

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" 
    } 
} 

Data message FCM documentation.

+0

这工作正常。但是,我如何将自定义值发送到我的设备?例如userUiD,... –

+0

检查我的文章更新。 – Pipiks

+0

您知道如何从Firebase脚本中删除数据库中的父级? –

相关问题