Skip to content

Commit 0513465

Browse files
committed
Notification: ensure that a notification is displayed when a Push cannot be resolved.
Previously the error was logged and added to push history but no notification was shown, so the user fully miss the new message.
1 parent df33cf2 commit 0513465

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/notification/NotificationData.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ sealed interface NotificationContent {
6868
) : MessageLike
6969

7070
data object RoomEncrypted : MessageLike
71+
data object UnableToResolve : MessageLike
7172
data class RoomMessage(
7273
val senderId: UserId,
7374
val messageType: MessageType

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/RustNotificationService.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.element.android.libraries.core.extensions.runCatchingExceptions
1212
import io.element.android.libraries.matrix.api.core.EventId
1313
import io.element.android.libraries.matrix.api.core.RoomId
1414
import io.element.android.libraries.matrix.api.core.SessionId
15+
import io.element.android.libraries.matrix.api.notification.NotificationContent
1516
import io.element.android.libraries.matrix.api.notification.NotificationData
1617
import io.element.android.libraries.matrix.api.notification.NotificationService
1718
import io.element.android.services.toolbox.api.systemclock.SystemClock
@@ -24,7 +25,7 @@ class RustNotificationService(
2425
private val sessionId: SessionId,
2526
private val notificationClient: NotificationClient,
2627
private val dispatchers: CoroutineDispatchers,
27-
clock: SystemClock,
28+
private val clock: SystemClock,
2829
) : NotificationService {
2930
private val notificationMapper: NotificationMapper = NotificationMapper(clock)
3031

@@ -43,11 +44,32 @@ class RustNotificationService(
4344
val eventIds = requests.flatMap { it.eventIds }
4445
for (eventId in eventIds) {
4546
val item = items[eventId]
47+
val roomId = RoomId(requests.find { it.eventIds.contains(eventId) }?.roomId!!)
4648
if (item != null) {
47-
val roomId = RoomId(requests.find { it.eventIds.contains(eventId) }?.roomId!!)
4849
put(EventId(eventId), notificationMapper.map(sessionId, EventId(eventId), roomId, item))
4950
} else {
5051
Timber.e("Could not retrieve event for notification with $eventId")
52+
put(
53+
EventId(eventId),
54+
NotificationData(
55+
sessionId = sessionId,
56+
eventId = EventId(eventId),
57+
threadId = null,
58+
roomId = roomId,
59+
senderAvatarUrl = null,
60+
senderDisplayName = null,
61+
senderIsNameAmbiguous = false,
62+
roomAvatarUrl = null,
63+
roomDisplayName = null,
64+
isDirect = false,
65+
isDm = false,
66+
isEncrypted = false,
67+
isNoisy = false,
68+
timestamp = clock.epochMillis(),
69+
content = NotificationContent.MessageLike.UnableToResolve,
70+
hasMention = false
71+
)
72+
)
5173
}
5274
}
5375
}

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/DefaultNotifiableEventResolver.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ class DefaultNotifiableEventResolver @Inject constructor(
225225
val fallbackNotifiableEvent = fallbackNotifiableEvent(userId, roomId, eventId)
226226
ResolvedPushEvent.Event(fallbackNotifiableEvent)
227227
}
228+
NotificationContent.MessageLike.UnableToResolve -> {
229+
Timber.tag(loggerTag.value).w("Unable to resolve notification -> fallback")
230+
val fallbackNotifiableEvent = fallbackNotifiableEvent(userId, roomId, eventId)
231+
ResolvedPushEvent.Event(fallbackNotifiableEvent)
232+
}
228233
is NotificationContent.MessageLike.RoomRedaction -> {
229234
// Note: this case will be handled below
230235
val redactedEventId = content.redactedEventId

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/DefaultPushHandler.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.element.android.libraries.push.impl.history.onUnableToRetrieveSession
2525
import io.element.android.libraries.push.impl.notifications.NotificationEventRequest
2626
import io.element.android.libraries.push.impl.notifications.NotificationResolverQueue
2727
import io.element.android.libraries.push.impl.notifications.channels.NotificationChannels
28+
import io.element.android.libraries.push.impl.notifications.model.FallbackNotifiableEvent
2829
import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent
2930
import io.element.android.libraries.push.impl.notifications.model.NotifiableRingingCallEvent
3031
import io.element.android.libraries.push.impl.notifications.model.ResolvedPushEvent
@@ -87,13 +88,24 @@ class DefaultPushHandler @Inject constructor(
8788
} else {
8889
result.fold(
8990
onSuccess = {
90-
pushHistoryService.onSuccess(
91-
providerInfo = request.providerInfo,
92-
eventId = request.eventId,
93-
roomId = request.roomId,
94-
sessionId = request.sessionId,
95-
comment = "Push handled successfully",
96-
)
91+
if (it is ResolvedPushEvent.Event && it.notifiableEvent is FallbackNotifiableEvent) {
92+
pushHistoryService.onUnableToResolveEvent(
93+
providerInfo = request.providerInfo,
94+
eventId = request.eventId,
95+
roomId = request.roomId,
96+
sessionId = request.sessionId,
97+
reason = "Unable to resolve event, showing fallback notification",
98+
)
99+
mutableBatteryOptimizationStore.showBatteryOptimizationBanner()
100+
} else {
101+
pushHistoryService.onSuccess(
102+
providerInfo = request.providerInfo,
103+
eventId = request.eventId,
104+
roomId = request.roomId,
105+
sessionId = request.sessionId,
106+
comment = "Push handled successfully",
107+
)
108+
}
97109
},
98110
onFailure = { exception ->
99111
pushHistoryService.onUnableToResolveEvent(

0 commit comments

Comments
 (0)