MindOrks
Published in

MindOrks

⏰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.

# 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 AlarmManager.
  • At the Scheduled time, create WorkManager 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.

💻 Implementation

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

  • Download a google-services.json configuration file and paste it in /app directory of the project.
  • Add dependencies in build.gradle of app module.
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
//Firebase
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-iid:20.0.2'
implementation 'com.google.firebase:firebase-messaging:20.1.0'

// Work Manager
implementation 'android.arch.work:work-runtime:1.0.1'
}
<service
android:name=".fcm.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<receiver android:name=".fcm.NotificationBroadcastReceiver" />

Let’s Understand Format of Data Payload:

We’ll send FCM Data payload as below.

{ 
"to": "/topics/discount-offers",
"priority": "high",
"data" : {
"title" : "TITLE_HERE",
"message" : "MESSAGE_HERE",
"isScheduled" : "true",
"scheduledTime" : "2019-12-13 09:41:00"
}
}
Set One-time Alarm using AlarmManager
NotificationBroadcastReceiver Implementation
ScheduledWorker Implementation

# 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 receiver (ON_BOOT_COMPLETED) which will be executed when Device is Rebooted. In that, all notifications in Room database should be scheduled again using AlarmManager.

Let’s Test It 😃

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

{ 
"to": "/topics/discount-offers",
"priority": "high",
"data" : {
"title" : "🎅 Christmas Offer 🎄",
"message" : "Grab 90% Discount 😍 on Mobile Phones",
"isScheduled" : "true",
"scheduledTime" : "2019-12-13 14:12:00"
}
}
At exact 02:12, Notification is displayed though Data is OFF.

Sharing is Caring!

Support this repository:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shreyas Patil

Google Developer Expert for Android, 👨‍💻Engineer @ Paytm Insider❤️ Android & Kotlin 💻Organizer @KotlinMumbai | More info: https://shreyaspatil.dev