如何在特定条件下触发通知?
How to trigger notification on certain condition?
我在 firebase-messaging 中使用下面的云功能来通知用户 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
变量是 hundered 的倍数时,才应触发通知。
我知道 value%100 == 0
检查数字是否是 hundered 的倍数的逻辑,但我不知道如何实现这个逻辑?
请进一步指导我
希望这对您有所帮助!只需将发送通知代码放在 if
块和 else
return 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;
我在 firebase-messaging 中使用下面的云功能来通知用户 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
变量是 hundered 的倍数时,才应触发通知。
我知道 value%100 == 0
检查数字是否是 hundered 的倍数的逻辑,但我不知道如何实现这个逻辑?
请进一步指导我
希望这对您有所帮助!只需将发送通知代码放在 if
块和 else
return 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;