【发布时间】:2018-10-16 18:48:27
【问题描述】:
无法让 Firebase Cloud Functions for Firestore 在我的集合的 onWrite 上触发。尝试为聊天应用设置设备到设备的推送通知。功能已部署并按按需付费计划进行,但不会触发文档更改、更新或“聊天”集合中的创建。 Firebase 云消息传递应该发送推送并写入日志。两者都没有发生。 Push 正在与其他来源合作。
感谢您的帮助,希望设备到设备的推送通知更容易,计划是观看聊天文档并在更新或创建新对话时触发推送通知。接受其他想法。谢谢
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.firestore
.document('chats/{chatID}')
.onWrite((data, context) => {
// Get an object representing the document
console.log('chat triggered');
// perform desired operations ...
// See documentation on defining a message payload.
var message = {
notification: {
title: 'Hello World!',
body: 'Hello World!'
},
topic: context.params.chatID
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return true
})
.catch((error) => {
console.log('Error sending message:', error);
});
});
更新:我正在使用“firebase-functions”:“^1.0.1”
更新:更新了代码以反映我们当前已部署的内容,但仍然无法正常工作。
【问题讨论】:
-
您在函数日志中看到错误消息吗?
-
“Can't get the method to trigger a log statement either”究竟是什么意思。在 Firebase 控制台中,在浏览器 (console.firebase.google.com) 中,如果您在垂直菜单中选择“Functions”并选择“LOGS”选项卡,您将看到来自 Functions 的日志。
-
我不清楚您看到的日志。您是否看到指示
sendNotification Function execution started的日志? -
您如何在
chats/{chatID}、代码或 Firebase 控制台上编写 Firestore 数据?如果是代码,请编辑您的帖子以包含它。 -
我的问题是关于您如何写入 Firestore 数据库,而不是日志。无论您使用哪种方法,您是否看到 Firebase 控制台数据库选项卡中的数据发生变化?
标签: javascript firebase firebase-cloud-messaging google-cloud-firestore google-cloud-functions