【问题标题】:Send push notification after X minutes using Firebase Functions使用 Firebase 函数在 X 分钟后发送推送通知
【发布时间】:2018-12-29 21:27:13
【问题描述】:

我有一个适用于 iOS 和 Android 的移动应用,我正在尝试实现一项功能,该功能可在用户存在应用 X 分钟后发送推送通知。

在我的数据库中,我已经可以判断用户何时不再活动。

我考虑过使用 App Engine & Firebase Functions & FCM https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html

但似乎 cron.yaml 是静态的(定期发送通知), 而且我正在寻找一种动态方法,我可以在其中动态决定发送推送通知的时间。

例如 - 基于一些数据库数据, 1 位用户将在 30 分钟不活动后收到通知, 而其他用户将在 15 分钟后得到它。

我怎样才能实现这种特定行为? 谢谢。

【问题讨论】:

    标签: android ios firebase google-app-engine push-notification


    【解决方案1】:

    这是我为解决通知调度问题所做的工作。我允许 5 分钟的容差(即,如果您想在 10:03 发送通知,那么它将在 10:05 发送)。

    假设您有一个函数 sendNotificationFunction(userId, notificationMessage) ,它以 userIdnotificationMessage 作为参数并向该特定用户发送通知。

    1. 在 firebase 实时数据库中,我创建了一个节点,其中包含有关通知计划的信息:

      scheduleNotification: {
          <pushId>: {
              "userId":<userId>,
              "scheduledTimestamp":<1530000000000>,
              "notificationMessage":<Message that you want to send>
          }
      }
      
    2. 制作每 5 分钟触发一次的函数,并检查要安排哪些通知。

      exports.every5MinTrigger = functions.https.onRequest((req,res)=>{
          let currentTime = new Date().getTime(); //Say 10:00 
          let startTime = currentTime; //10:00 
          let endTime = currentTime + 5*60*1000; //10:05  
          firebase.database.ref().child("scheduleNotification").orderByChild("scheduledTimestamp").once('value').then((snap)=>{
               if(snap.exists()){
                   snap.forEach(childSnap=>{
                       let userId = childSnap.child('userId').val()
                       let notificationMessage = childSnap.child('notificationMessage').val()
                       //Now you have userId and your notification's language. Call your sendNotificationFunction() Here
                   })
               }
          })
      })
      

      在您的 Firebase 云函数中部署此函数。你会得到这个函数的 url,假设它是这样的:https://us-central1-&lt;your-project&gt;.cloudfunctions.net/every5MinTrigger

    3. 每隔 5 分钟从您的 cron 作业中调用此 ,https://us-central1-&lt;your-project&gt;.cloudfunctions.net/every5MinTrigger, url,以便将通知安排在接下来的 5 分钟内。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-18
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 2018-07-08
      • 2017-11-16
      相关资源
      最近更新 更多