4

I have a task to do cloud reminders using Firebase. I have a time stamp of reminder in my DB. So I need to send notification at that time.

In Firebase test console there is a option to send notification in certain time, but I haven't seen this option in MessagingOptions.

So what is the best way to implemet cloud reminders using Firebase Functions? Also: time of reminder can be changed or removed at all.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62

2 Answers2

3

Depends on the scope of your project.

  • If your project is only gonna be for 1 type of mobile client, for example, is only Android. Then set the alarms on the device, and use the Firebase database as data persistence for the user to have a flawless experience between installs and devices.

  • If your project also has to be for web, and send emails along with the notification, then you will have to schedule a cron job. The only question here is how accurate the alarms have to be. This is the important detail because if the alarms need to be exact, you are gonna have to schedule one cronjob each minute, incredibly wasteful, querying the database so often will consume your usage quota pretty fast. Besides, mobile devices have energy saving modes during the meanwhile are not completely active so push notifications can be delayed by the system until the next maintenance window. My recommendation would be to narrow down the alarm to a 30 or 15 minutes window. So you can schedule cronjobs that will query the database for objects in that span of time.

    1. For scheduling a cron job I recommend you reading this Firebase blog post

    2. For sending the push notification using FCM I recommend checking the Functions samples on Github

    3. The best for starting with Functions is this video

    4. And for your database structure, please see example below

{
  "user_alarms": {
    "uid1": {
      "pushkey1": {
        "full_object": "goes here"
      },
      "pushkey2": {
        "full_object": "goes here"
      }
    },
    "uid2": {
      "pushkey3": {
        "full_object": "goes here"
      },
      "pushkey4": {
        "full_object": "goes here"
      }
    }
  },
  "alarms": {
    "pushkey1": {
      "reduced_object": "mainly_the_time"
    },
    "pushkey4": {
      "reduced_object": "mainly_the_time"
    }
  }
}

The user_alarms node contains each user alarm, that way the user can see all the alarms that have set. The node alarms is the denormalization for the original node and is the node to be queried on each cron job. It contains a simpler object, only what is needed to perform the query for the upcoming alarms, mainly the trigger time. To improve performance after the FCM push is sent the corresponding object should be deleted.

cutiko
  • 9,887
  • 3
  • 45
  • 59
1

Easiest solution:

  1. Find the URL of your function. eg https://us-central1-myappsucks.cloudfunctions.net/myfunctionsuckstoo

  2. Use cron service to create a schedulea tasks. I use cron-job.org. Create account, go to cronjobs tab insert your url and timing.

  3. Save and Done.

hasn
  • 749
  • 6
  • 21