Let enterprise build be able to use a different notification channel for noisy notification.

This commit is contained in:
Benoit Marty 2026-02-11 09:59:45 +01:00
parent abae0314e2
commit df4c719151
17 changed files with 135 additions and 26 deletions

View file

@ -23,7 +23,9 @@ import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.ContributesBinding
import dev.zacsweers.metro.SingleIn
import io.element.android.appconfig.NotificationConfig
import io.element.android.features.enterprise.api.EnterpriseService
import io.element.android.libraries.di.annotations.ApplicationContext
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.push.impl.R
import io.element.android.services.toolbox.api.strings.StringProvider
@ -47,9 +49,10 @@ interface NotificationChannels {
/**
* Get the channel for messages.
* @param sessionId the session the message belongs to.
* @param noisy true if the notification should have sound and vibration.
*/
fun getChannelIdForMessage(noisy: Boolean): String
fun getChannelIdForMessage(sessionId: SessionId, noisy: Boolean): String
/**
* Get the channel for test notifications.
@ -67,6 +70,7 @@ class DefaultNotificationChannels(
private val stringProvider: StringProvider,
@ApplicationContext
private val context: Context,
private val enterpriseService: EnterpriseService,
) : NotificationChannels {
init {
createNotificationChannels()
@ -115,7 +119,7 @@ class DefaultNotificationChannels(
.setSound(
Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
// Strangely wwe have to provide a "//" before the package name
// Strangely we have to provide a "//" before the package name
.path("//" + context.packageName + "/" + R.raw.message)
.build(),
AudioAttributes.Builder()
@ -186,8 +190,13 @@ class DefaultNotificationChannels(
return if (ring) RINGING_CALL_NOTIFICATION_CHANNEL_ID else CALL_NOTIFICATION_CHANNEL_ID
}
override fun getChannelIdForMessage(noisy: Boolean): String {
return if (noisy) NOISY_NOTIFICATION_CHANNEL_ID else SILENT_NOTIFICATION_CHANNEL_ID
override fun getChannelIdForMessage(sessionId: SessionId, noisy: Boolean): String {
return if (noisy) {
enterpriseService.getNoisyNotificationChannelId(sessionId)
?: NOISY_NOTIFICATION_CHANNEL_ID
} else {
SILENT_NOTIFICATION_CHANNEL_ID
}
}
override fun getChannelIdForTest(): String = NOISY_NOTIFICATION_CHANNEL_ID

View file

@ -151,7 +151,10 @@ class DefaultNotificationCreator(
val channelId = if (containsMissedCall) {
notificationChannels.getChannelForIncomingCall(false)
} else {
notificationChannels.getChannelIdForMessage(noisy = roomInfo.shouldBing)
notificationChannels.getChannelIdForMessage(
sessionId = roomInfo.sessionId,
noisy = roomInfo.shouldBing,
)
}
// A category allows groups of notifications to be ranked and filtered per user or system settings.
// For example, alarm notifications should display before promo notifications, or message from known contact
@ -230,7 +233,10 @@ class DefaultNotificationCreator(
notificationAccountParams: NotificationAccountParams,
inviteNotifiableEvent: InviteNotifiableEvent,
): Notification {
val channelId = notificationChannels.getChannelIdForMessage(inviteNotifiableEvent.noisy)
val channelId = notificationChannels.getChannelIdForMessage(
sessionId = inviteNotifiableEvent.sessionId,
noisy = inviteNotifiableEvent.noisy,
)
return NotificationCompat.Builder(context, channelId)
.setOnlyAlertOnce(true)
.setContentTitle((inviteNotifiableEvent.roomName ?: buildMeta.applicationName).annotateForDebug(5))
@ -270,7 +276,10 @@ class DefaultNotificationCreator(
notificationAccountParams: NotificationAccountParams,
simpleNotifiableEvent: SimpleNotifiableEvent,
): Notification {
val channelId = notificationChannels.getChannelIdForMessage(simpleNotifiableEvent.noisy)
val channelId = notificationChannels.getChannelIdForMessage(
sessionId = simpleNotifiableEvent.sessionId,
noisy = simpleNotifiableEvent.noisy,
)
return NotificationCompat.Builder(context, channelId)
.setOnlyAlertOnce(true)
.setContentTitle(buildMeta.applicationName.annotateForDebug(7))
@ -302,7 +311,10 @@ class DefaultNotificationCreator(
notificationAccountParams: NotificationAccountParams,
fallbackNotifiableEvent: FallbackNotifiableEvent,
): Notification {
val channelId = notificationChannels.getChannelIdForMessage(false)
val channelId = notificationChannels.getChannelIdForMessage(
sessionId = fallbackNotifiableEvent.sessionId,
noisy = false,
)
return NotificationCompat.Builder(context, channelId)
.setOnlyAlertOnce(true)
.setContentTitle(buildMeta.applicationName.annotateForDebug(7))
@ -334,8 +346,11 @@ class DefaultNotificationCreator(
noisy: Boolean,
lastMessageTimestamp: Long,
): Notification {
val channelId = notificationChannels.getChannelIdForMessage(noisy)
val userId = notificationAccountParams.user.userId
val channelId = notificationChannels.getChannelIdForMessage(
sessionId = userId,
noisy = noisy,
)
return NotificationCompat.Builder(context, channelId)
.setOnlyAlertOnce(true)
// used in compat < N, after summary is built based on child notifications