【发布时间】:2019-08-24 06:22:42
【问题描述】:
我正在尝试编写一个云函数,如果我的应用程序更改了用户的 firestore 数据库中的某些字符串。云功能需要发送推送通知。数据库架构是 Messages => {UID} => UpdatedMessages 。问题是我不知道如何检索哪个 updateMessage 在哪个 UID 下已更新。
const functions = require('firebase-functions');
const admin = require('firebase-admin')
admin.initializeApp()
const toUpperCase = (string)=> string.toUpperCase()
var registrationToken = 'dfJY6hYzJyE:APdfsdfsdddfdfGt9HMfTXmei4QFtO0u1ePVpNYaOqZ1rnDpB8xfSjx7-G6tFY-vWQY3vDPEwn_iZVK2PrsGUVB0q9L_QoRYpLJ3_6l1SVHd_0gQxJb_Kq-IBlavyJCkgkIZ';
exports.sendNotification = functions.firestore
.document('messages/{userId}/{updatedMessage}')
.onUpdate((change, context) => {
var message = {
data: {
title: 'Update',
body: 'New Update'
},
token: registrationToken
};
// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent messagesssss:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
});
我只需要从 UID 重新获取“var registrationToken”。
【问题讨论】:
-
如果你已经想触发一个云函数,为什么不首先调用你的云函数来写入你的数据库?
-
我知道你正在使用 firestore,但也许这对你有帮助 youtube.com/…
-
谢谢康斯坦丁啤酒。我想我会使用云函数在我的数据库上写。这样发送推送通知更容易
-
你怎么不知道哪条消息被编辑了?只需在
/messages/节点下的onEdit触发你的函数,就可以得到编辑消息的整个路径 -
@Emanuele 谢谢你不知道这是可能的
标签: node.js firebase google-cloud-firestore google-cloud-functions