无法获取Firebase Cloud Functions以在我的集合的onWrite上触发Firestore。正在尝试为聊天应用设置设备到设备推送通知。功能已部署且按需付费计划,但是文档中的更改、更新或“聊天”集合中的创建不会触发。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函数”:"^1.0.1“
更新:更新了代码,以反映我们当前部署的内容,但仍然不起作用。
发布于 2018-05-07 12:53:26
对于新库(v1.0),您可能会使用旧语法(v1.0之前)。请参阅迁移指南:https://firebase.google.com/docs/functions/beta-v1-diff并检查package.json文件中的版本。
此外,请注意,云函数必须始终返回Promise (或者,如果不能,对于异步函数,至少返回一个值)。请参阅文档(和相关视频),其中详细解释了这一点:https://firebase.google.com/docs/functions/terminate-functions
你应该这样修改你的代码:
如果您使用的是Cloud Functions 1.0或更高版本:
exports.sendNotification = functions.firestore
.document('chats/{chatID}')
.onWrite((change, context) => {
正在返回:
exports.sendNotification = functions.firestore
.document('chats/{chatID}')
.onWrite((change, 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. //<- If you are using a CF version under v1.0 don't change here
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().send(message). //<- return the resulting Promise
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return true; //<- return a value
})
.catch((error) => {
console.log('Error sending message:', error);
//return. <- No need to return here
});
});
发布于 2018-05-07 13:07:23
您的firebase-admin
initialization syntax,admin.initializeApp()
,建议您使用云函数SDK版本1.0.x。早期版本中onWrite()
have changed in version 1.0.x的参数。您还需要为异步操作admin.messaging.send()
执行return a Promise
。下面说明了所需的三个更正:
exports.sendNotification = functions.firestore
.document('chats/{chatID}')
.onWrite((data, context) => { // <= CHANGE
// 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 // <= CHANGE
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().send(message) // <= CHANGE
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return
})
.catch((error) => {
console.log('Error sending message:', error);
return
});
});
发布于 2021-06-14 08:12:17
对我来说,问题是我正在写入Firestore中已经存在的数据。如果您正在写入的数据与现有数据完全相同,则onWrite显然不会被触发。
https://stackoverflow.com/questions/50206137
复制相似问题