无法使Firestore的Firebase Cloud功能在我的集合的onWrite上触发 . 尝试为聊天应用设置设备到设备推送通知 . 功能已部署并按计划付费,但是,“聊天”集合中的文档,更新或创建中的更改不会触发 . Firebase Cloud 消息传递应该发送推送和写入日志 . 两者都没有发生 . 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”更新:更新了代码以反映我们当前部署的内容,仍然无法正常工作 .
2 回答
您可能正在使用旧库(在V1.0之前)使用新库(v1.0) . 请参阅迁移指南:https://firebase.google.com/docs/functions/beta-v1-diff并检查package.json文件中的版本 .
此外,请注意,Cloud Function必须始终返回Promise(或者如果不能,则至少为异步函数返回值) . 请参阅此文档(及相关视频),详细说明:https://firebase.google.com/docs/functions/terminate-functions
您应该以这种方式修改代码:
如果您使用的是Cloud Functions 1.0或更高版本:
返回:
您的
firebase-admin
initialization syntax,admin.initializeApp()
表示您使用的是Cloud Functions SDK版本1.0.x.来自早期版本的onWrite()
have changed in version 1.0.x的参数 . 您还需要return a Promise进行异步操作admin.messaging.send()
. 需要进行的三项修正如下: