Sending Firebase Push Notifications via Spring Boot Application — Part 2: Detecting Kotlin Application’s State via LifeCycleObserver

Previous article: Setting Up the Android Application

Hello, in the previous article we’ve set up an Android application that can receive push notifications. However, at this point, the application is only able to log these remote messages. There are two ways of displaying these remote messages as notifications:

  • Notifications shown in the notification bar
  • Notifications shown in the application itself (in-app)

To display a notification in one of these ways, the application must keep track of whether it’s running in the foreground or the background.

So this article will focus on how to detect the application’s state.

Checking if the application is in the background

Androidx has lifecycle-aware components that can keep track of whether the application is in the foreground or not. To use these features, we’ll need to create a custom Application class that implements LifeCycleObserver.

The official documentation defines this component as follows:

Marks a class as a LifecycleObserver. It does not have any methods, instead, relies on OnLifecycleEvent annotated methods.

A simple implementation is as follows:

package com.tahaburak.firebasetestapp

import android.app.Application
import androidx.lifecycle.LifecycleObserver

/**
* Created by burak on 23.12.2020
*/
class FirebaseTestApp : Application(), LifecycleObserver {

//..

}

Make sure to add this class to the application’s AndroidManifest.xml file.

<application
android:name=".FirebaseTestApp"
..
>..
</application>

We’ll need to add this class as an observer to ProcessLifecycleOwner in the onCreate method so that it can access lifecycle events.

// implementation "androidx.lifecycle:lifecycle-process:2.2.0"class FirebaseTestApp : Application(), LifecycleObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
}

Now we’ll be updating a static variable on designated lifecycle events.

class FirebaseTestApp : Application(), LifecycleObserver {

var isForeground = false

override fun
onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onLifecycleEventStart() {
isForeground = true
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onLifecycleEventStop() {
isForeground = false
}

}

With this implementation “isForeground” variable is now able to tell the application’s state. However, to access this singleton and its variable we need to create a companion object.

companion object {
@SuppressLint("StaticFieldLeak")
lateinit var instance: FirebaseTestApp
private set
}

The code block above will provide an instance of the FirebaseTestApp.kt class.

You can find the final version of the “FirebaseTestApp.kt” class on Github.

Conclusion

Now that we’re able to access this information we can move on to displaying these notifications. The next article will focus on this matter.

You can find the source code at Github.

Sincerely,
Burak.