Skip to content

Commit

Permalink
added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Oct 2, 2023
1 parent 0061baa commit e0c279e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NotificationClickReceiverActivity : Activity(), TrackableScreen {
logger.error("SDK is not initialized, cannot handle notification intent")
} else {
sdkInstance.diGraph.pushMessageProcessor.processNotificationClick(
activity = this,
activityContext = this,
intent = data
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,22 @@ interface PushMessageProcessor {
* @param deliveryToken received in push payload
*/
fun processRemoteMessageDeliveredMetrics(deliveryId: String, deliveryToken: String)
fun processNotificationClick(activity: Context, intent: Intent)

/**
* Executes the necessary actions when a notification is clicked by the user.
*
* This method performs the following tasks:
* 1. Tracks 'opened' metrics for the notification.
* 2. Resolves the deep link, if available in the notification payload, and navigates to the corresponding screen.
* 3. If no deep link is provided, opens the default launcher screen.
*
* This method may only be called from `onCreate` or `onNewIntent` methods of notification handler activity.
*
* @param activityContext context should be an activity context and not application context as
* this will be used to start desired activity
* @param intent intent received by the activity
*/
fun processNotificationClick(activityContext: Context, intent: Intent)

companion object {
// Count of messages stored in memory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,24 @@ internal open class PushMessageProcessorImpl(
}
}

override fun processNotificationClick(activity: Context, intent: Intent) {
override fun processNotificationClick(activityContext: Context, intent: Intent) {
kotlin.runCatching {
val payload: CustomerIOParsedPushPayload? =
intent.extras?.parcelable(NotificationClickReceiverActivity.NOTIFICATION_PAYLOAD_EXTRA)
if (payload == null) {
logger.error("Payload is null, cannot handle notification intent")
} else {
processNotificationIntent(activity = activity, payload = payload)
processNotificationIntent(activityContext, payload)
}
}.onFailure { ex ->
logger.error("Failed to process notification intent: ${ex.message}")
}
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun processNotificationIntent(activity: Context, payload: CustomerIOParsedPushPayload) {
fun processNotificationIntent(activityContext: Context, payload: CustomerIOParsedPushPayload) {
trackMetrics(payload)
handleDeepLink(activity, payload)
handleDeepLink(activityContext, payload)
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
Expand All @@ -127,28 +127,31 @@ internal open class PushMessageProcessorImpl(
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun handleDeepLink(activity: Context, payload: CustomerIOParsedPushPayload) {
fun handleDeepLink(activityContext: Context, payload: CustomerIOParsedPushPayload) {
val deepLink = payload.deepLink?.takeIfNotBlank()

// check if host app overrides the handling of deeplink
val notificationCallback = moduleConfig.notificationCallback
val taskStackFromPayload =
notificationCallback?.createTaskStackFromPayload(activity, payload)
val taskStackFromPayload = notificationCallback?.createTaskStackFromPayload(
context = activityContext,
payload = payload
)
if (taskStackFromPayload != null) {
logger.info("Notification target overridden by createTaskStackFromPayload, starting new stack for link $deepLink")
taskStackFromPayload.startActivities()
return
}

// Get the default intent for the host app
val defaultHostAppIntent = deepLinkUtil.createDefaultHostAppIntent(context = activity)
val defaultHostAppIntent =
deepLinkUtil.createDefaultHostAppIntent(context = activityContext)
// Check if the deep links are handled within the host app
val deepLinkHostAppIntent = deepLink?.let { link ->
deepLinkUtil.createDeepLinkHostAppIntent(context = activity, link = link)
deepLinkUtil.createDeepLinkHostAppIntent(context = activityContext, link = link)
}
// Check if the deep links can be opened outside the host app
val deepLinkExternalIntent = deepLink?.let { link ->
deepLinkUtil.createDeepLinkExternalIntent(context = activity, link = link)
deepLinkUtil.createDeepLinkExternalIntent(context = activityContext, link = link)
}
val deepLinkIntent: Intent = deepLinkHostAppIntent
?: deepLinkExternalIntent
Expand All @@ -162,7 +165,7 @@ internal open class PushMessageProcessorImpl(

when (moduleConfig.pushClickBehavior) {
PushClickBehavior.RESET_TASK_STACK -> {
val taskStackBuilder = TaskStackBuilder.create(activity).apply {
val taskStackBuilder = TaskStackBuilder.create(activityContext).apply {
addNextIntentWithParentStack(deepLinkIntent)
}
taskStackBuilder.startActivities()
Expand All @@ -171,11 +174,11 @@ internal open class PushMessageProcessorImpl(
PushClickBehavior.ACTIVITY_PREVENT_RESTART -> {
deepLinkIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_SINGLE_TOP
activity.startActivity(deepLinkIntent)
activityContext.startActivity(deepLinkIntent)
}

PushClickBehavior.ACTIVITY_NO_FLAGS -> {
activity.startActivity(deepLinkIntent)
activityContext.startActivity(deepLinkIntent)
}
}
}
Expand Down

0 comments on commit e0c279e

Please sign in to comment.