Push Notifications How-To: Firebase, AWS cron jobs & EventBridge

Serguey Arellano Martínez
TribalScale
Published in
4 min readFeb 1, 2021

Let’s say we want to notify our users if they are subscribed to some sort of daily reminder. Here we can use an AWS cron job along with EventBridge to send push notifications with firebase.

Let’s start with a simple diagram to identify the different parts or steps:

Step 1

We retrieve the reminders from our database:

User 111 has three activities in our reminders table:

  • activity1 is enabled but the user completed the activity for the day. So we shouldn’t notify the user about this activity;
  • activity2 is not enabled, and therefore should not be notified about the activity either;
  • activity3 is enabled and hasn’t been completed for the day. We have to notify the user about this activity.

Now we’re going to send these items to our EventBridge bus so the next entity can take care of them. However, we still have some problems…

Problems

Eventbridge has a limitation of ten entries every time we send the events to the bus.

Because we are going to produce an event per reminder found for that day, the second step should receive a maximum of ten reminders at a time. We will talk about that later…

If there are a lot of items to handle, Dynamodb could return a paginated response. We would miss some items if we don’t give a solution.

Solutions

Let’s tackle the pagination issue first.

The idea is to retry our queries every time we get a paginated response. Also it’s important that we return the responses each time.

generators to the rescue!

The query to the DB looks as expected:

Notice the limit for every query.

At this point, we can iterate through the responses that the generator returns. The generator is returning promises so an async iterator is needed.

Notice how handy having retryQuery() is. A generator is useful when you don't know how many iterations you are going to need. Magic!

Step 2

As a subscriber the step 2 handler has to listen to the cron.query source (step 1) and do two things:

  • filter invalid activities inside each reminder;
  • split each reminder into a new event.

Step 3

Easy! Follow the steps in this article.

  • Set up Firebase SDK (initialize app)
  • compose message
  • send the message

There is a catch, and that is the need for an FCM token. An FCM token identifies the user’s device. It’s important to have it in our reminders database.

Summary

We could have used step-functions instead, but EventBridge works as expected and it’s pretty simple. Due to lambda’s auto-scaling, we don’t need to care about concurrency. Just don’t exceed event bridge limitations (10 events at once). You can choose the communication layer of your choice (even a different pub-sub pattern with SNS).

And there we have it! Integrating push notifications with Firebase is as easy as that! We tackled our problem in three simple steps:

  1. Get the reminders from our database table;
  2. Filter invalid reminders;
  3. Send reminders to firebase for push notifications.

Special thanks to Ryan Bui for co-authoring this article

Happy hacking!

Have questions about integrating push notifications with Firebase? Click here to speak to one of our experts.

Serguey is a Javascript developer with experience in cloud architectures and a TDD advocate.

TribalScale is a global innovation firm that helps enterprises adapt and thrive in the digital era. We transform teams and processes, build best-in-class digital products, and create disruptive startups. Learn more about us on our website. Connect with us on Twitter, LinkedIn & Facebook!

--

--