[英]Firebase Cloud Functions for Firestore are not triggering
無法獲取Firestore的Firebase Cloud Functions在我的集合的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”
更新:更新了代碼以反映我們當前已部署的內容,但仍無法正常工作。
您有可能在新庫(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或更高版本:
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
});
});
您的admin.initializeApp()
firebase-admin
初始化語法 admin.initializeApp()
表示您正在使用Cloud Functions SDK1.0.x。 1.0.x版中的 onWrite()
參數已從早期版本更改 。 您還需要為異步操作admin.messaging.send()
返回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
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.