In this demo app, We are subscribing to a FCM Channel discount-offers
. We'll receive Data payload from FCM to this subscribed channel and we will process and schedule it.
Firebase Messaging
- Firebase Cloud Messaging Library.Firebase IID
- Firebase Instance ID Library.WorkManager
- Used for Background Work Processing.google-services.json
configuration file in /app
directory.src
โ
โโโโfcm
โ โ MyFirebaseMessagingService.kt
โ โ NotificationBroadcastReceiver.kt
โ โ ScheduledWorker.kt
โ
โโโโutil
| โ NotificationUtil.kt
| โ SettingUtil.kt
|
โโโโui
| โ MainActivity.kt
|
MyFirebaseMessagingService
: FCM Receiver Service Implementation. Process of Notification Scheduling using AlarmManager
is implemented here.NotificationBroadcastReceiver
: BroadcastReceiver
Implementation. Executed when AlarmManager is triggered. WorkManager is initiated and executed for background processing.ScheduledWorker
: WorkManager
Implementation. Notification is displayed in the system tray and other background processed are executed.
NotificationUtil
: Implementation to display Notification on the system tray.
SettingUtil
: Function implementation to check whether Automatic Date & Time setting is ON/OFF.
MainActivity
: UI Implementation to subscribe to FCM Channel.
discount-offers
FCM Channel from Android Device.{
"to": "/topics/discount-offers",
"priority": "high",
"data" : {
"title" : "TITLE_HERE",
"message" : "MESSAGE_HERE",
"isScheduled" : "true",
"scheduledTime" : "2019-12-13 09:41:00"
}
}
Format of scheduledTime
: YYYY-MM-DD HH:MM:SSonMessageReceived()
in MyFirebaseMessagingService
will be invoked. In this, following operations will be done-isScheduled
parameter received is false
then notifications is displayed in system tray instantly.isScheduled
is true
then scheduledTime
is parsed from payload and AlarmManager
is used to set one-time alarm at that time and NotificationBroadcastReceiver
implementation will be executed on that time.onReceive()
, we have scheduled a WorkManager ScheduledWorker
for background work processing. There in doWork()
, we're finally we're displaying Notification on system tray. Do any background proessing and return status from WorkManager.Hurrah!๐ we have successfully implemented On-Device Scheduling of FCM Push Notification๐.
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"
}
}
๐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 ๐.
Yippie ๐! It's working as expected. Hope you liked that. If you find it helpful please share this. Maybe it'll help someone needy!
If you found this project useful, then please consider giving it a :star: on Github and sharing it with your friends via social media.
Sharing is Caring!
If you want to contact me, feel free to reach meโฆ Visit My Profile.