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

chore: initialize native android sdk in flutter #159

Merged
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
83 changes: 33 additions & 50 deletions android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import androidx.annotation.NonNull
import io.customer.customer_io.constant.Keys
import io.customer.customer_io.messaginginapp.CustomerIOInAppMessaging
import io.customer.customer_io.messagingpush.CustomerIOPushMessaging
import io.customer.messaginginapp.MessagingInAppModuleConfig
import io.customer.messaginginapp.ModuleMessagingInApp
import io.customer.messaginginapp.type.InAppEventListener
import io.customer.messaginginapp.type.InAppMessage
import io.customer.messagingpush.MessagingPushModuleConfig
import io.customer.messagingpush.ModuleMessagingPushFCM
import io.customer.messagingpush.config.PushClickBehavior
import io.customer.sdk.CustomerIO
import io.customer.sdk.CustomerIOBuilder
import io.customer.sdk.core.di.SDKComponent
import io.customer.sdk.core.util.CioLogLevel
import io.customer.sdk.core.util.Logger
import io.customer.sdk.data.model.Region
import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand Down Expand Up @@ -239,57 +237,42 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
*/
}

private fun initialize(configData: Map<String, Any>) {
// TODO: Fix initialize implementation
/*
private fun initialize(args: Map<String, Any>): kotlin.Result<Unit> = runCatching {
val application: Application = context.applicationContext as Application
val siteId = configData.getString(Keys.Environment.SITE_ID)
val apiKey = configData.getString(Keys.Environment.API_KEY)
val region = configData.getProperty<String>(
Keys.Environment.REGION
)?.takeIfNotBlank()
val enableInApp = configData.getProperty<Boolean>(
Keys.Environment.ENABLE_IN_APP
)
val cdpApiKey = requireNotNull(args.getAsTypeOrNull<String>("cdpApiKey")) {
"CDP API Key is required to initialize Customer.io"
}

// Checks if SDK was initialized before, which means lifecycle callbacks are already
// registered as well
val isLifecycleCallbacksRegistered = kotlin.runCatching { CustomerIO.instance() }.isSuccess
val logLevelRawValue = args.getAsTypeOrNull<String>("logLevel")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used hardcoded strings for keys instead because the constants aren't reused outside this function, so I felt they weren't adding much value. That said, I'm open to moving them back to constants if anyone thinks it would improve clarity or maintainability.

val regionRawValue = args.getAsTypeOrNull<String>("region")
val givenRegion = regionRawValue.let { Region.getRegion(it) }

val customerIO = CustomerIO.Builder(
siteId = siteId,
apiKey = apiKey,
region = Region.getRegion(region),
appContext = application,
config = configData
CustomerIOBuilder(
applicationContext = application,
cdpApiKey = cdpApiKey
).apply {
addCustomerIOModule(module = configureModuleMessagingPushFCM(configData))
if (enableInApp == true) {
addCustomerIOModule(
module = ModuleMessagingInApp(
config = MessagingInAppModuleConfig.Builder()
.setEventListener(CustomerIOInAppEventListener { method, args ->
[email protected]?.get()?.runOnUiThread {
flutterCommunicationChannel.invokeMethod(method, args)
}
}).build(),
)
)
}
logLevelRawValue?.let { logLevel(CioLogLevel.getLogLevel(it)) }
regionRawValue?.let { region(givenRegion) }

args.getAsTypeOrNull<String>("migrationSiteId")?.let(::migrationSiteId)
args.getAsTypeOrNull<Boolean>("autoTrackDeviceAttributes")
?.let(::autoTrackDeviceAttributes)
args.getAsTypeOrNull<Boolean>("trackApplicationLifecycleEvents")
?.let(::trackApplicationLifecycleEvents)

args.getAsTypeOrNull<Int>("flushAt")?.let(::flushAt)
args.getAsTypeOrNull<Int>("flushInterval")?.let(::flushInterval)

args.getAsTypeOrNull<String>("apiHost")?.let(::apiHost)
args.getAsTypeOrNull<String>("cdnHost")?.let(::cdnHost)

// TODO: Initialize in-app module with given config
// TODO: Initialize push module with given config
}.build()
logger.info("Customer.io instance initialized successfully")

// 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) {
activity?.get()?.let { activity ->
logger.info("Requesting delayed activity lifecycle events")
val lifecycleCallbacks = customerIO.diGraph.activityLifecycleCallbacks
lifecycleCallbacks.postDelayedEventsForNonNativeActivity(activity)
}
}
*/

logger.info("Customer.io instance initialized successfully from app")
}.onFailure { ex ->
logger.error("Failed to initialize Customer.io instance from app, ${ex.message}")
}

private fun configureModuleMessagingPushFCM(config: Map<String, Any?>?): ModuleMessagingPushFCM {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,4 @@ internal object Keys {
const val DELIVERY_TOKEN = "deliveryToken"
const val METRIC_EVENT = "metricEvent"
}

object Environment {
const val SITE_ID = "siteId"
const val API_KEY = "apiKey"
const val REGION = "region"
const val ENABLE_IN_APP = "enableInApp"
}
}
Loading