Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: push opened metrics tracked on Android 12 #119

Merged
merged 7 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ customerio.reactnative.kotlinVersion=1.7.21
customerio.reactnative.compileSdkVersion=30
customerio.reactnative.targetSdkVersion=30
customerio.reactnative.minSdkVersion=21
customerio.reactnative.cioSDKVersionAndroid=[3.3,4.0)
customerio.reactnative.cioSDKVersionAndroid=[3.4.1,4.0)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ class CustomerIOReactNativeModule(
get() = CustomerIOShared.instance().diStaticGraph.logger
private lateinit var customerIO: CustomerIO

init {
// If the SDK was already initialized from CIO service using context, initialize the local
// reference with SDK instance so it represents the actual state of SDK.
// SDK instance may only be initialized before when a notification was received while the
// app was in terminated state. Capturing the same instance helps us identify the initial
// state of SDK initialization.
// If the SDK was not initialized before, `CustomerIO.instance()` will throw an exception
// and local variable will also not be initialized.
kotlin.runCatching {
customerIO = CustomerIO.instance()
}
}

override fun getName(): String {
return MODULE_NAME
}
Expand All @@ -39,6 +52,10 @@ class CustomerIOReactNativeModule(
configuration: ReadableMap? = null,
packageConfiguration: ReadableMap? = null,
) {
// Checks if SDK was initialized before, which means lifecycle callbacks are already
// registered as well
val isLifecycleCallbacksRegistered = ::customerIO.isInitialized

if (isInstanceValid()) {
logger.info("Customer.io instance already initialized, reinitializing")
}
Expand All @@ -56,6 +73,16 @@ class CustomerIOReactNativeModule(
inAppEventListener = inAppMessagingModule,
)
logger.info("Customer.io instance initialized successfully from app")
// Request lifecycle events for first initialization only as relaunching app
// in wrapper SDKs may result in reinitialization of SDK and lifecycle listener
// will already be attached in this case as they are registered to application object.
if (!isLifecycleCallbacksRegistered) {
currentActivity?.let { activity ->
logger.info("Requesting delayed activity lifecycle events")
val lifecycleCallbacks = customerIO.diGraph.activityLifecycleCallbacks
lifecycleCallbacks.postDelayedEventsForNonNativeActivity(activity)
}
}
} catch (ex: Exception) {
logger.error("Failed to initialize Customer.io instance from app, ${ex.message}")
}
Expand Down