diff --git a/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt b/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt index 7fb8bf3..8f19e66 100644 --- a/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt +++ b/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt @@ -9,7 +9,6 @@ import io.customer.customer_io.messaginginapp.CustomerIOInAppMessaging import io.customer.customer_io.messagingpush.CustomerIOPushMessaging import io.customer.messaginginapp.type.InAppEventListener import io.customer.messaginginapp.type.InAppMessage -import io.customer.messagingpush.ModuleMessagingPushFCM import io.customer.sdk.CustomerIO import io.customer.sdk.CustomerIOBuilder import io.customer.sdk.core.di.SDKComponent @@ -277,7 +276,14 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { args.getAsTypeOrNull("cdnHost")?.let(::cdnHost) // TODO: Initialize in-app module with given config - // TODO: Initialize push module with given config + + // Configure push messaging module based on config provided by customer app + args.getAsTypeOrNull>(key = "push").let { pushConfig -> + CustomerIOPushMessaging.addNativeModuleFromConfig( + builder = this, + config = pushConfig ?: emptyMap() + ) + } }.build() logger.info("Customer.io instance initialized successfully from app") @@ -285,30 +291,6 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { logger.error("Failed to initialize Customer.io instance from app, ${ex.message}") } - private fun configureModuleMessagingPushFCM(config: Map?): ModuleMessagingPushFCM { - return ModuleMessagingPushFCM( - // TODO: Fix push module configuration - /* - config = MessagingPushModuleConfig.Builder().apply { - config?.getProperty(CustomerIOConfig.Companion.Keys.AUTO_TRACK_PUSH_EVENTS) - ?.let { value -> - setAutoTrackPushEvents(autoTrackPushEvents = value) - } - config?.getProperty(CustomerIOConfig.Companion.Keys.PUSH_CLICK_BEHAVIOR_ANDROID) - ?.takeIfNotBlank() - ?.let { value -> - val behavior = kotlin.runCatching { - enumValueOf(value) - }.getOrNull() - if (behavior != null) { - setPushClickBehavior(pushClickBehavior = behavior) - } - } - }.build(), - */ - ) - } - override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { flutterCommunicationChannel.setMethodCallHandler(null) diff --git a/android/src/main/kotlin/io/customer/customer_io/messagingpush/CustomerIOPushMessaging.kt b/android/src/main/kotlin/io/customer/customer_io/messagingpush/CustomerIOPushMessaging.kt index cb8cc88..0e6c560 100644 --- a/android/src/main/kotlin/io/customer/customer_io/messagingpush/CustomerIOPushMessaging.kt +++ b/android/src/main/kotlin/io/customer/customer_io/messagingpush/CustomerIOPushMessaging.kt @@ -6,7 +6,11 @@ import io.customer.customer_io.constant.Keys import io.customer.customer_io.getAsTypeOrNull import io.customer.customer_io.invokeNative import io.customer.messagingpush.CustomerIOFirebaseMessagingService +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.Logger import io.flutter.embedding.engine.plugins.FlutterPlugin @@ -34,6 +38,7 @@ internal class CustomerIOPushMessaging( return@invokeNative getRegisteredDeviceToken() } } + Keys.Methods.ON_MESSAGE_RECEIVED -> { call.invokeNative(result) { args -> return@invokeNative onMessageReceived( @@ -82,4 +87,37 @@ internal class CustomerIOPushMessaging( throw ex } } + + companion object { + /** + * Adds push messaging module to native Android SDK based on the configuration provided by + * customer app. + * + * @param builder instance of CustomerIOBuilder to add push messaging module. + * @param config configuration provided by customer app for push messaging module. + */ + internal fun addNativeModuleFromConfig( + builder: CustomerIOBuilder, + config: Map + ) { + val androidConfig = + config.getAsTypeOrNull>(key = "android") ?: emptyMap() + // Prefer `android` object for push configurations as it's more specific to Android + // For common push configurations, use `config` object instead of `android` + + // Default push click behavior is to prevent restart of activity in Flutter apps + val pushClickBehavior = androidConfig.getAsTypeOrNull("pushClickBehavior") + ?.takeIf { it.isNotBlank() } + ?.let { value -> + runCatching { enumValueOf(value) }.getOrNull() + } ?: PushClickBehavior.ACTIVITY_PREVENT_RESTART + + val module = ModuleMessagingPushFCM( + moduleConfig = MessagingPushModuleConfig.Builder().apply { + setPushClickBehavior(pushClickBehavior = pushClickBehavior) + }.build(), + ) + builder.addCustomerIOModule(module) + } + } }