From 29889ad9ecd893e27f5996144ed18e5f4f3e2ec9 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 13 Nov 2025 11:59:58 +0100 Subject: [PATCH] Cleanup NotificationCreator. --- .../factories/NotificationCreator.kt | 89 +++++++------------ 1 file changed, 31 insertions(+), 58 deletions(-) diff --git a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/factories/NotificationCreator.kt b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/factories/NotificationCreator.kt index 8324bd8bba..70d01cbf24 100755 --- a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/factories/NotificationCreator.kt +++ b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/factories/NotificationCreator.kt @@ -142,14 +142,21 @@ class DefaultNotificationCreator( } else { notificationChannels.getChannelIdForMessage(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 + // that can be displayed in not disturb mode if white listed (the later will need compat28.x) + // If any of the events are of rtc notification type it means a missed call, set the category to the right value + val category = if (containsMissedCall) { + NotificationCompat.CATEGORY_MISSED_CALL + } else { + NotificationCompat.CATEGORY_MESSAGE + } val builder = if (existingNotification != null) { NotificationCompat.Builder(context, existingNotification) + // Clear existing actions + .clearActions() } else { NotificationCompat.Builder(context, channelId) - // 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 - // that can be displayed in not disturb mode if white listed (the later will need compat28.x) - .setCategory(NotificationCompat.CATEGORY_MESSAGE) // ID of the corresponding shortcut, for conversation features under API 30+ // Must match those created in the ShortcutInfoCompat.Builder() // for the notification to appear as a "Conversation": @@ -165,7 +172,6 @@ class DefaultNotificationCreator( // Remove notification after opening it or using an action .setAutoCancel(true) } - val messagingStyle = existingNotification?.let { MessagingStyle.extractMessagingStyleFromNotification(it) } ?: createMessagingStyleFromCurrentUser( @@ -175,52 +181,35 @@ class DefaultNotificationCreator( isThread = threadId != null, roomIsGroup = !roomInfo.isDm, ) - messagingStyle.addMessagesFromEvents(events, imageLoader) - return builder + .setCategory(category) .setNumber(events.size) .setOnlyAlertOnce(roomInfo.isUpdated) .setWhen(lastMessageTimestamp) // MESSAGING_STYLE sets title and content for API 16 and above devices. .setStyle(messagingStyle) .configureWith(notificationAccountParams) - // Sets priority for 25 and below. For 26 and above, 'priority' is deprecated for - // 'importance' which is set in the NotificationChannel. The integers representing - // 'priority' are different from 'importance', so make sure you don't mix them. + // Mark room/thread as read + .addAction(markAsReadActionFactory.create(roomInfo, threadId)) + .setContentIntent(openIntent) + .setLargeIcon(largeIcon) + .setDeleteIntent(pendingIntentFactory.createDismissRoomPendingIntent(roomInfo.sessionId, roomInfo.roomId)) .apply { + // Sets priority for 25 and below. For 26 and above, 'priority' is deprecated for + // 'importance' which is set in the NotificationChannel. The integers representing + // 'priority' are different from 'importance', so make sure you don't mix them. if (roomInfo.shouldBing) { - // Compat priority = NotificationCompat.PRIORITY_DEFAULT - /* - vectorPreferences.getNotificationRingTone()?.let { - setSound(it) - } - */ setLights(notificationAccountParams.color, 500, 500) } else { priority = NotificationCompat.PRIORITY_LOW } - // Clear existing actions since we might be updating an existing notification - clearActions() - // Add actions and notification intents - // Mark room/thread as read - addAction(markAsReadActionFactory.create(roomInfo, threadId)) // Quick reply if (!roomInfo.hasSmartReplyError) { val latestEventId = events.lastOrNull()?.eventId addAction(quickReplyActionFactory.create(roomInfo, latestEventId, threadId)) } - if (openIntent != null) { - setContentIntent(openIntent) - } - setLargeIcon(largeIcon) - setDeleteIntent(pendingIntentFactory.createDismissRoomPendingIntent(roomInfo.sessionId, roomInfo.roomId)) - - // If any of the events are of rtc notification type it means a missed call, set the category to the right value - if (events.any { it.type == EventType.RTC_NOTIFICATION }) { - setCategory(NotificationCompat.CATEGORY_MISSED_CALL) - } } .setTicker(tickerText) .build() @@ -237,32 +226,26 @@ class DefaultNotificationCreator( .setContentText(inviteNotifiableEvent.description.annotateForDebug(6)) .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_ALL) .configureWith(notificationAccountParams) + .addAction(rejectInvitationActionFactory.create(inviteNotifiableEvent)) + .addAction(acceptInvitationActionFactory.create(inviteNotifiableEvent)) + // Build the pending intent for when the notification is clicked + .setContentIntent(pendingIntentFactory.createOpenRoomPendingIntent(inviteNotifiableEvent.sessionId, inviteNotifiableEvent.roomId, null)) .apply { - addAction(rejectInvitationActionFactory.create(inviteNotifiableEvent)) - addAction(acceptInvitationActionFactory.create(inviteNotifiableEvent)) - // Build the pending intent for when the notification is clicked - setContentIntent(pendingIntentFactory.createOpenRoomPendingIntent(inviteNotifiableEvent.sessionId, inviteNotifiableEvent.roomId, null)) - if (inviteNotifiableEvent.noisy) { // Compat priority = NotificationCompat.PRIORITY_DEFAULT - /* - vectorPreferences.getNotificationRingTone()?.let { - setSound(it) - } - */ setLights(notificationAccountParams.color, 500, 500) } else { priority = NotificationCompat.PRIORITY_LOW } - setDeleteIntent( - pendingIntentFactory.createDismissInvitePendingIntent( - inviteNotifiableEvent.sessionId, - inviteNotifiableEvent.roomId, - ) - ) - setAutoCancel(true) } + .setDeleteIntent( + pendingIntentFactory.createDismissInvitePendingIntent( + inviteNotifiableEvent.sessionId, + inviteNotifiableEvent.roomId, + ) + ) + .setAutoCancel(true) .build() } @@ -283,11 +266,6 @@ class DefaultNotificationCreator( if (simpleNotifiableEvent.noisy) { // Compat priority = NotificationCompat.PRIORITY_DEFAULT - /* - vectorPreferences.getNotificationRingTone()?.let { - setSound(it) - } - */ setLights(notificationAccountParams.color, 500, 500) } else { priority = NotificationCompat.PRIORITY_LOW @@ -346,11 +324,6 @@ class DefaultNotificationCreator( if (noisy) { // Compat priority = NotificationCompat.PRIORITY_DEFAULT - /* - vectorPreferences.getNotificationRingTone()?.let { - setSound(it) - } - */ setLights(notificationAccountParams.color, 500, 500) } else { // compat