⏰Scheduling FCM Push Notifications🔔 On Device — Android📱

In this article, We will schedule On Device FCM (Firebase Cloud Messaging) Push Notifications without using Cloud Pub/Sub or Cron jobs.

Shreyas Patil
Dec 19, 2019 · 5 min read

Hello everyone, If you’re developing an app where you want to send scheduled Notifications to users then you can achieve in many ways. You can use Google Cloud Pub/Sub using Firebase Cloud Functions. Cloud Pub/Sub and Cron jobs are costly solutions. In this demo, we’ll schedule FCM Notifications by just sending normal Push Notification to subscribed channel and we’ll process/schedule it on the device.

# What will we do?

  • We’ll subscribe to FCM Topic.
  • We’ll send Data payload with scheduling information to the FCM topic.
  • Process received notification in a device, Schedule it using .
  • At the Scheduled time, create for background processing and display notification on the system tray.

# What are the Advantages of using this technique? 😕

Imagine you have developed XYZ app which is related to Online Shopping and you always provides exciting discount/offers to users. It has 5000 active user installs. Imagine Following 2 Scenarios with respect to the above data.

#1️⃣Scenario 1:

You have scheduled a 50% discount offer notification to send on 12:00 am using Google Cloud Pub/Sub. This offer is going to expire in 5 minutes. Out of 5000 users, only 3000 users are online at that time. Remaining 2000 users will receive notification after they’ll turn on data and till that time, the offer will be expired!😔 Thus, you’ll lose 2000 users 😐.

#️2️⃣Scenario 2:

You have scheduled a 50% discount offer notification to send on 12:00 am using the method we discussed earlier. Suppose we sent Data payload on 09:00 pm (i.e. Before 3 hours). In between that time period. All online users will receive payload on device and notification will be scheduled. Consider, out of 5000 users, 4500 users were online between 9 pm to 12 am. Then all these 4500 users will receive a notification. Most exciting🤩 will be… Though users are offline on 12 am still notification is displayed (because they already received notification earlier on device). Thus we’ll not lose many users.😀

Thus from above both scenarios, the Second scenario seems efficient and useful for some use cases. Let’s implement it!


💻 Implementation

I have created a repository on GitHub. You can take reference of it.

  • Set up Project on Firebase Console
  • Download a configuration file and paste it in directory of the project.
  • Add dependencies in of app module.
  • Service and BroadcastReceiver declarations in Manifest:
  • In MainActivity.kt, subscribe to FCM Notification Channel. Let’s say we’re subscribing to FCM Channel.

Let’s Understand Format of Data Payload:

We’ll send FCM Data payload as below.

When we send payload, if isScheduled is false then the notification is displayed instantly otherwise it’s displayed on scheduledTime.

Format of : YYYY-MM-DD HH:MM:SS.

  • In , whenever Notification is received, is invoked. We have to do all the process of scheduling in this method. We have to parse data.

We’ll first parse data like title, message, etc. We’ll check if `Automatic Date and Time` is turned ON in system settings (otherwise notification can be displayed at the wrong time). Then check if is true then schedule notification otherwise display the notification.

Let’s come to .

Set One-time Alarm using AlarmManager

We’re using to set one-time (non-repeating) Alarm. We have parsed using class to get its millisecond value.

Finally, we have scheduled alarm using method. It sets non-repeating one time alarm in the system and executes exactly on specified millies value.

RTC_WAKEUP flag will trigger the alarm according to the time of the clock which will wake up the device when it goes off. At that time, of will be executed. Let’s see an implementation of it.

NotificationBroadcastReceiver Implementation

In this, we’ll get data from the Intent and create data. We’ll create because this work should only be executed once. In the end, we’ll enqueue work and execution of Work will be started. Thus, method of is executed.

ScheduledWorker Implementation

In that, we’ll get data(like title, message, etc) which we received from notification and finally, we’ll display it on the system tray. Other background tasks will also be processed here. If the process is successful, return using . If you want to retry process simple return otherwise return .

Hurrah! 😍 we have successfully implemented and scheduled FCM Push Notification on Android device.

# What If Device is Rebooted?😕

If a device is rebooted, Alarm will not work. For this, you’ll have to store all the information about FCM Notifications using Room database. After this, you’ll need to create a () which will be executed when Device is Rebooted. In that, all notifications in Room database should be scheduled again using .

Let’s Test It 😃

I have sent below payload with to the FCM Channel (discount-offers).

🚀See output below and notice that Internet/Wi-Fi is OFF still at exactly 02:12 pm I’m getting a notification on the system tray 😃.

At exact 02:12, Notification is displayed though Data is OFF.

Yippie 😍! It’s working as expected. Hope you liked that. If you find it helpful please share this article. Maybe it’ll help someone needy!

Sharing is Caring!

Support this repository:

Thank you 😄!

If you want to contact me, feel free to reach me…

MindOrks

Our community publishes stories worth reading on Android Development

Shreyas Patil

Written by

📱Android Developer 🌐Web Developer. http://shreyaspatil.me

MindOrks

MindOrks

Our community publishes stories worth reading on Android Development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade