【问题标题】:How to trigger notification on certain condition?如何在特定条件下触发通知?
【发布时间】:2020-07-27 05:18:54
【问题描述】:

我在 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 的逻辑来检查数字是否是百的倍数,但是我不知道如何实现这个逻辑? 请进一步指导我

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions firebase-cloud-messaging


    【解决方案1】:

    希望这会有所帮助!只需将发送通知代码放在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;
    

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 1970-01-01
      • 2021-01-24
      • 2020-04-21
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多