amplitude/Amplitude-Kotlin

Go to Kotlin SDK. enableForegroundTracking(Application app) -> onEnterForeground (timestamp: Long)

AlbertKobyakov opened this issue · 1 comments

Summary

In the old version we used enableForegroundTracking(application). Kotlin SDK has an onEnterForeground(timestamp: Long) method. This method is not described anywhere, I don't understand what should be passed to the timestamp?

Old version:

Amplitude.getInstance()
         .trackSessionEvents(true)
         .initialize(applicationContext, apiKey)
         .enableForegroundTracking(application)

Kotlin SDK:

Amplitude(
         Configuration(
             apiKey = apiKey,
             context = application.applicationContext
         ).apply {
             trackingSessionEvents = true
         }
     ).apply {
         onEnterForeground(???)
     }

I wasn't aware of this functionality as I hadn't previously used the old SDK. I suspect an approach to this could be providing usage of the AndroidX Lifecycle dependency (androidx.lifecycle:lifecycle-process) and listening for resume events there. Something like this (assuming you can inject the Amplitude SDK to a service with Hilt or equivalent):

@Singleton
class MyLifecycleObserver @Inject constructor(
    private val amplitude: Amplitude
) : DefaultLifecycleObserver {

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)

        amplitude.onEnterForeground( // provide something like `Instant.now().toEpochMilli()`? )
    }
}

We use this approach to record an app open event without using onEnterForeground.

Some documentation could be nice here, like a default value created by Amplitude with the option of passing our own.