Skip to content

Commit

Permalink
Adjust ChatNotificationSender consider the CBM feature flag
Browse files Browse the repository at this point in the history
Use the old link when it's off. Use a separate inbox or a conversation
specific link depending on if the notification itself contains the
conversationId in there or not.

Also replace the TaskStackBuilder with a simple
PendingIntent.getActivity since we simply have one activity to open, and
we pass it a deep link instead.
Inspiration from:
https://github.com/android/nowinandroid/blob/85129e4660f7a27c7081f4ac21169d19db89fbb6/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt#L154-L166
  • Loading branch information
StylianosGakis committed Jul 24, 2024
1 parent d59dbc0 commit 96c0bcb
Showing 1 changed file with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,37 @@ package com.hedvig.android.app.notification.senders

import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.MessagingStyle
import androidx.core.app.Person
import androidx.core.app.TaskStackBuilder
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.IconCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner
import com.benasher44.uuid.Uuid
import com.google.firebase.messaging.RemoteMessage
import com.hedvig.android.app.notification.getMutablePendingIntentFlags
import com.hedvig.android.core.buildconstants.HedvigBuildConstants
import com.hedvig.android.core.common.android.notification.setupNotificationChannel
import com.hedvig.android.feature.chat.navigation.ChatDestinations
import com.hedvig.android.feature.claim.details.navigation.ClaimDetailDestinations
import com.hedvig.android.feature.home.home.navigation.HomeDestination
import com.hedvig.android.featureflags.FeatureManager
import com.hedvig.android.featureflags.flags.Feature
import com.hedvig.android.logger.LogPriority.ERROR
import com.hedvig.android.logger.logcat
import com.hedvig.android.navigation.core.AppDestination
import com.hedvig.android.navigation.core.HedvigDeepLinkContainer
import com.hedvig.android.notification.core.NotificationSender
import com.hedvig.android.notification.core.sendHedvigNotification
import com.kiwi.navigationcompose.typed.createRoutePattern
import hedvig.resources.R
import kotlinx.coroutines.flow.first

/**
* An in-memory storage of the current route, used to *not* show the chat notification if we are in a select list of
Expand All @@ -52,6 +58,8 @@ private val listOfDestinationsWhichShouldNotShowChatNotification = setOf(
class ChatNotificationSender(
private val context: Context,
private val hedvigDeepLinkContainer: HedvigDeepLinkContainer,
private val featureManager: FeatureManager,
private val buildConstants: HedvigBuildConstants,
) : NotificationSender {
override fun createChannel() {
setupNotificationChannel(
Expand All @@ -62,7 +70,7 @@ class ChatNotificationSender(
)
}

override fun sendNotification(type: String, remoteMessage: RemoteMessage) {
override suspend fun sendNotification(type: String, remoteMessage: RemoteMessage) {
val isAppForegrounded = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)
val currentDestination = CurrentDestinationInMemoryStorage.currentDestination
if (currentDestination in listOfDestinationsWhichShouldNotShowChatNotification && isAppForegrounded) {
Expand Down Expand Up @@ -104,8 +112,10 @@ class ChatNotificationSender(
}

sendChatNotificationInner(
context,
messagingStyle,
context = context,
style = messagingStyle,
remoteMessage = remoteMessage,
isCbmEnabled = featureManager.isFeatureEnabled(Feature.ENABLE_CBM).first(),
)
}

Expand All @@ -116,15 +126,36 @@ class ChatNotificationSender(

private fun sendChatNotificationInner(
context: Context,
style: NotificationCompat.MessagingStyle,
alertOnlyOnce: Boolean = false,
style: MessagingStyle,
remoteMessage: RemoteMessage,
isCbmEnabled: Boolean,
) {
val chatIntent = Intent(Intent.ACTION_VIEW, Uri.parse(hedvigDeepLinkContainer.chat))
val intentUri = Uri.parse(
if (isCbmEnabled) {
val conversationId = remoteMessage.data.conversationIdFromCustomerIoData()
val isValidUuid = conversationId.isValidUuid()
if (conversationId != null && isValidUuid) {
hedvigDeepLinkContainer.conversation.replace("{conversationId}", conversationId)
} else {
hedvigDeepLinkContainer.inbox
}
} else {
hedvigDeepLinkContainer.chat
},
)
logcat { "ChatNotificationSender sending notification with isCbmEnabled:$isCbmEnabled to uri:$intentUri" }
val chatIntent = Intent().apply {
action = Intent.ACTION_VIEW
data = intentUri
component = ComponentName(buildConstants.appId, MainActivityFullyQualifiedName)
}

val pendingIntent: PendingIntent? = TaskStackBuilder
.create(context)
.addNextIntent(chatIntent)
.getPendingIntent(0, getMutablePendingIntentFlags())
val chatPendingIntent: PendingIntent? = PendingIntent.getActivity(
/* context = */ context,
/* requestCode = */ 0,
/* intent = */ chatIntent,
/* flags = */ getMutablePendingIntentFlags(),
)

val notification = NotificationCompat
.Builder(
Expand All @@ -138,8 +169,7 @@ class ChatNotificationSender(
.setChannelId(CHAT_CHANNEL_ID)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setContentIntent(pendingIntent)
.setOnlyAlertOnce(alertOnlyOnce)
.setContentIntent(chatPendingIntent)
.build()

sendHedvigNotification(
Expand All @@ -163,7 +193,24 @@ class ChatNotificationSender(
.setKey(YOU_PERSON_KEY)
.build()

private fun String?.isValidUuid(): Boolean {
if (this == null) {
return false
}
return try {
Uuid.fromString(this)
true
} catch (e: IllegalArgumentException) {
logcat(ERROR) {
"ChatNotificationSender got a conersationId which was not a UUID: $this"
}
false
}
}

companion object {
private const val MainActivityFullyQualifiedName = "com.hedvig.android.app.MainActivity"

private const val CHAT_CHANNEL_ID = "hedvig-chat"
private const val CHAT_NOTIFICATION_ID = 1
private const val HEDVIG_PERSON_KEY = "HEDVIG"
Expand Down

0 comments on commit 96c0bcb

Please sign in to comment.