From ed59215e9c6e2ce62f44d280f2a6ae4e80ef3656 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 25 Nov 2021 10:37:18 +0100 Subject: [PATCH] Trying to fix notification not dismissed sometimes when marking it as read --- .../NotificationBroadcastReceiver.kt | 19 +++++++++++++++---- .../notifications/NotificationsManager.kt | 7 +++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.kt b/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.kt index f0161a4c3..5215bdf2d 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationBroadcastReceiver.kt @@ -19,6 +19,7 @@ */ package org.linphone.notifications +import android.app.NotificationManager import android.app.RemoteInput import android.content.BroadcastReceiver import android.content.Context @@ -33,13 +34,13 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { val notificationId = intent.getIntExtra(NotificationsManager.INTENT_NOTIF_ID, 0) if (intent.action == NotificationsManager.INTENT_REPLY_NOTIF_ACTION || intent.action == NotificationsManager.INTENT_MARK_AS_READ_ACTION) { - handleChatIntent(intent, notificationId) + handleChatIntent(context, intent, notificationId) } else if (intent.action == NotificationsManager.INTENT_ANSWER_CALL_NOTIF_ACTION || intent.action == NotificationsManager.INTENT_HANGUP_CALL_NOTIF_ACTION) { handleCallIntent(intent) } } - private fun handleChatIntent(intent: Intent, notificationId: Int) { + private fun handleChatIntent(context: Context, intent: Intent, notificationId: Int) { val remoteSipAddress = intent.getStringExtra(NotificationsManager.INTENT_REMOTE_ADDRESS) if (remoteSipAddress == null) { Log.e("[Notification Broadcast Receiver] Remote SIP address is null for notification id $notificationId") @@ -84,7 +85,11 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { msg.send() Log.i("[Notification Broadcast Receiver] Reply sent for notif id $notificationId") } else { - coreContext.notificationsManager.dismissChatNotification(room) + if (!coreContext.notificationsManager.dismissChatNotification(room)) { + Log.w("[Notification Broadcast Receiver] Notifications Manager failed to cancel notification") + val notificationManager = context.getSystemService(NotificationManager::class.java) + notificationManager.cancel(NotificationsManager.CHAT_TAG, notificationId) + } } } @@ -107,7 +112,13 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { if (intent.action == NotificationsManager.INTENT_ANSWER_CALL_NOTIF_ACTION) { coreContext.answerCall(call) } else { - if (call.state == Call.State.IncomingReceived || call.state == Call.State.IncomingEarlyMedia) coreContext.declineCall(call) else coreContext.terminateCall(call) + if (call.state == Call.State.IncomingReceived || + call.state == Call.State.IncomingEarlyMedia + ) { + coreContext.declineCall(call) + } else { + coreContext.terminateCall(call) + } } } diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index b7b58b63e..f2100c6a8 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -93,7 +93,7 @@ class NotificationsManager(private val context: Context) { private const val SERVICE_NOTIF_ID = 1 private const val MISSED_CALLS_NOTIF_ID = 2 - private const val CHAT_TAG = "Chat" + const val CHAT_TAG = "Chat" private const val CALL_TAG = "Call" private const val MISSED_CALL_TAG = "Missed call" } @@ -762,20 +762,23 @@ class NotificationsManager(private val context: Context) { displayChatNotifiable(message.chatRoom, notifiable) } - fun dismissChatNotification(room: ChatRoom) { + fun dismissChatNotification(room: ChatRoom): Boolean { val address = room.peerAddress.asStringUriOnly() val notifiable: Notifiable? = chatNotificationsMap[address] if (notifiable != null) { Log.i("[Notifications Manager] Dismissing notification for chat room $room with id ${notifiable.notificationId}") notifiable.messages.clear() cancel(notifiable.notificationId, CHAT_TAG) + return true } else { val previousNotificationId = previousChatNotifications.find { id -> id == room.creationTime.toInt() } if (previousNotificationId != null) { Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId], canceling it") cancel(previousNotificationId, CHAT_TAG) + return true } } + return false } fun disableDismissNotificationUponReadForChatRoom(chatRoom: ChatRoom) {