2020-07-14-FCM-ForeBack
Before we read this, please make sure you've completed reading following articles.
build.gradle (Module: app)
dependencies {
...
implementation 'androidx.lifecycle:lifecycle-process:2.2.0'
implementation 'org.greenrobot:eventbus:3.2.0'
...
}Step 1: MyFirebaseMessagingService - Observe Foreground / Background
Add LifecycleObserver to your MyFirebaseMessagingService class.
class MyFirebaseMessagingService : FirebaseMessagingService(), LifecycleObserver {
private var isAppInForeground = false
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
override fun onDestroy() {
super.onDestroy()
ProcessLifecycleOwner.get().lifecycle.removeObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onForegroundStart() {
isAppInForeground = true
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onForegroundStop() {
isAppInForeground = false
}
}Step 2. Data Class for notification object.
Step 3: MyFirebaseMessagingService onMessageReceived - Send data using EventBus if App is in foreground
Check - If App is running & on forground, pass data using event-bus. Only If app is in background / killed, show notification
Step 4: Receive Data from Event Bus if App is in foreground.
Step 5: Send notification if app is in background
6. Read data from Notification when app was killed but launched using Notification.
Last updated
Was this helpful?