Skip to content

Commit

Permalink
chore: configure android push module (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 authored Nov 20, 2024
1 parent 24108dd commit a18bce7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -277,38 +276,21 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
args.getAsTypeOrNull<String>("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<Map<String, Any>>(key = "push").let { pushConfig ->
CustomerIOPushMessaging.addNativeModuleFromConfig(
builder = this,
config = pushConfig ?: emptyMap()
)
}
}.build()

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 {
return ModuleMessagingPushFCM(
// TODO: Fix push module configuration
/*
config = MessagingPushModuleConfig.Builder().apply {
config?.getProperty<Boolean>(CustomerIOConfig.Companion.Keys.AUTO_TRACK_PUSH_EVENTS)
?.let { value ->
setAutoTrackPushEvents(autoTrackPushEvents = value)
}
config?.getProperty<String>(CustomerIOConfig.Companion.Keys.PUSH_CLICK_BEHAVIOR_ANDROID)
?.takeIfNotBlank()
?.let { value ->
val behavior = kotlin.runCatching {
enumValueOf<PushClickBehavior>(value)
}.getOrNull()
if (behavior != null) {
setPushClickBehavior(pushClickBehavior = behavior)
}
}
}.build(),
*/
)
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
flutterCommunicationChannel.setMethodCallHandler(null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,6 +38,7 @@ internal class CustomerIOPushMessaging(
return@invokeNative getRegisteredDeviceToken()
}
}

Keys.Methods.ON_MESSAGE_RECEIVED -> {
call.invokeNative(result) { args ->
return@invokeNative onMessageReceived(
Expand Down Expand Up @@ -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<String, Any>
) {
val androidConfig =
config.getAsTypeOrNull<Map<String, Any>>(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<String>("pushClickBehavior")
?.takeIf { it.isNotBlank() }
?.let { value ->
runCatching { enumValueOf<PushClickBehavior>(value) }.getOrNull()
} ?: PushClickBehavior.ACTIVITY_PREVENT_RESTART

val module = ModuleMessagingPushFCM(
moduleConfig = MessagingPushModuleConfig.Builder().apply {
setPushClickBehavior(pushClickBehavior = pushClickBehavior)
}.build(),
)
builder.addCustomerIOModule(module)
}
}
}

0 comments on commit a18bce7

Please sign in to comment.