我有这样的firestore结构:
餐厅//收藏(C(
- restid//文档(d(
- 订单//c
- 有序
- 订单详细信息的文档
- 有序
- 骑手 - 一系列电话号码(D(
- 订单//c
骑手(C(
- Riderid
- 带有骑手详细信息和Firebase令牌的文件
该应用首先创建骑手的配置文件,然后收听相应的restid和在其中创建的订单。该部分工作正常。
我已经在应用程序中实现了FCM,并且从控制台发送时会收到FCM通知。该部分工作正常。
我需要一些帮助来编写云功能以自动化此通知过程。当应用不在前景时,此过程将有助于创建通知。我不知道打字稿或JavaScript详细了解,但是试图学习和编写一些代码以自动发送此通知发送部分并写下以下内容(歉意,因为它是伪代码和实际代码的混合(
export const triggerFunc = functions.firestore
.document('Restaurant/{restID}/Orders/{orderID}')
.onCreate((snap,context)=>{
const restaurantDocument = admin.firestore().doc('{restID}').get()
//Pseudocode from here onwards
if (Riders.{profile}.phonevalue exists in restaurantDocument.riderArrayPhoneValue){
sendNotification(Riders.profile.fireBaseInstanceIDField)
}
})
function sendNotification(fireBaseInstanceID:String){
// send notifictaion to fireBaseInstanceID
}
此cloud-function
应该帮助您入门,文档中的更多信息。
firestore = admin.firestore();
exports.notifyRider = functions
.firestore.document('Restaurant/{restID}/Orders/{orderID}')
.onCreate((snapshot, context) => {
const orderData = snapshot.data();
const restID = context.params.restID;
return firestore.doc(`Restaurant/${restID}`).get()
.then(restaurantData => {
// Here I'm assuming riders is an array of registration tokens
// otherwise you will have to query your riders collection to get access to the registration tokens
const riders = restaurantData.data().riders;
// By using multicast you simply set your registration tokens array in the payload
return admin.messaging().sendMulticast({
// I personally prefer to use only data messages to have more customization
// if your app is closed or in the foreground you will get the same message
data: {
date: context.timestamp,
title: orderData.order_something,
message: orderData.order_something_else,
something_else: something_else
restaurant: restID,
// etc, etc
},
tokens: riders
});
})
.catch(reason => {
console.warn(`Rejection Code: ${reason.code}`);
console.warn(`Rejection Message: ${reason.message}`);
});
});