我在firebase消息中使用云下功能来通知用户firestore文档中的更改,该文档运行良好,但我希望只有在达到特定条件时才触发通知
exports.sendNotification2 = functions.firestore.document("users/{to_user_id}/notifications_received/{notification_id}").onWrite((change, context)=>{
const to_user_id = context.params.to_user_id;
const notification_id = context.params.notification_id;
const to_token_id = context.params.to_token_id;
return admin.firestore().collection('users').doc(to_user_id).collection('notifications_received').doc(notification_id).get().then(queryResult=>{
const from_user_id = queryResult.data().from_user_id;
const to_token_id = queryResult.data().to_token_id;
const to_user_id = queryResult.data().to_user_id;
const notificaionmessage = queryResult.data().notification_message;
const notification_id = queryResult.data().notification_id;
const value = queryResult.data().value;
const payload = {
notification : {
title: notificaionmessage,
body: notificaionmessage,
icon:"default"
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK2',
notification_id: notification_id,
category: 'default'
}
};
return admin.messaging().sendToDevice(to_token_id,payload).then(result=>{
console.log("Notification Sent Successfulllllllllllll");
return null;
});
}
);
});
我希望当value
变量是一个数字,是无序的倍数时,只有这样才应该触发通知。我知道value%100 == 0
的逻辑来检查数字是否是无序的倍数,但我不知道如何实现这个逻辑?请指导我进一步
希望这会有所帮助!只需将发送通知代码放在if
块中,并在else
中返回null
或其他内容。
if (value%100 === 0){
const payload = {
notification : {
title: notificaionmessage,
body: notificaionmessage,
icon:"default"
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK2',
notification_id: notification_id,
category: 'default'
}
};
return admin.messaging().sendToDevice(to_token_id,payload).then(result=>{
console.log("Notification Sent Successfulllllllllllll");
return null;
});
}//end if block
else return null;