amplitude / Amplitude-Kotlin

Amplitude Kotlin SDK
MIT License
27 stars 10 forks source link

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

Open AlbertKobyakov opened 1 year ago

AlbertKobyakov commented 1 year ago

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(???)
     }
kelvinharron commented 1 year ago

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.