我有聊天应用程序,我正在使用Firebase firestore DB,我正在保存最近的消息,如下所示
发件人端: 消息 ->当前用户 ID -> recent_messages
接收器侧: 消息 ->接收者 ID -> recent_messages
这意味着相同的消息将写入当前用户和接收方用户文档。(相同的消息将写入两个不同的文档(
消息内容如下:
- 发送方
let fromData = ["text": text , "uid": toId, "userProfileUrl": userAvatarUrl, "timestamp": Timestamp(date: Date()), "seen": false]
-接收器侧
let toData = ["text": text ,"uid": currentUserID, "userProfileUrl": currentUser?.profileImageUrl ?? "", "timestamp": Timestamp(date: Date()),"seen": false]
现在我想观察recent_messages
集合,只向接收方用户发送通知 有没有办法避免向他们两个发送推送通知?
获取接收用户的注册令牌:
// Callback fired if Instance ID token is updated.
messaging.onTokenRefresh(() => {
messaging.getToken().then((refreshedToken) => {
console.log('Token refreshed.');
// Indicate that the new Instance ID token has not yet been sent to the
// app server.
setTokenSentToServer(false);
// Send Instance ID token to app server.
sendTokenToServer(refreshedToken);
// ...
}).catch((err) => {
console.log('Unable to retrieve refreshed token ', err);
showToken('Unable to retrieve refreshed token ', err);
});
});
然后使用sendToDevice
向用户发送通知:
admin.messaging().sendToDevice(token_id, payload)
.then(function(response) {
console.log('Successfully sent:', response);
})
.catch(function(error) {
console.log('Error sending:', error);
});
您可以通过将令牌添加到数据库来将令牌发送到服务器,然后在服务器端可以从数据库读取并检索令牌。
https://firebase.google.com/docs/cloud-messaging/js/first-message
https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging.html#send-todevice