Trying to fix notification not dismissed sometimes when marking it as read

This commit is contained in:
Sylvain Berfini 2021-11-25 10:37:18 +01:00
parent 5119f93365
commit ed59215e9c
2 changed files with 20 additions and 6 deletions

View file

@ -19,6 +19,7 @@
*/ */
package org.linphone.notifications package org.linphone.notifications
import android.app.NotificationManager
import android.app.RemoteInput import android.app.RemoteInput
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
@ -33,13 +34,13 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
val notificationId = intent.getIntExtra(NotificationsManager.INTENT_NOTIF_ID, 0) 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) { 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) { } else if (intent.action == NotificationsManager.INTENT_ANSWER_CALL_NOTIF_ACTION || intent.action == NotificationsManager.INTENT_HANGUP_CALL_NOTIF_ACTION) {
handleCallIntent(intent) 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) val remoteSipAddress = intent.getStringExtra(NotificationsManager.INTENT_REMOTE_ADDRESS)
if (remoteSipAddress == null) { if (remoteSipAddress == null) {
Log.e("[Notification Broadcast Receiver] Remote SIP address is null for notification id $notificationId") Log.e("[Notification Broadcast Receiver] Remote SIP address is null for notification id $notificationId")
@ -84,7 +85,11 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
msg.send() msg.send()
Log.i("[Notification Broadcast Receiver] Reply sent for notif id $notificationId") Log.i("[Notification Broadcast Receiver] Reply sent for notif id $notificationId")
} else { } 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) { if (intent.action == NotificationsManager.INTENT_ANSWER_CALL_NOTIF_ACTION) {
coreContext.answerCall(call) coreContext.answerCall(call)
} else { } 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)
}
} }
} }

View file

@ -93,7 +93,7 @@ class NotificationsManager(private val context: Context) {
private const val SERVICE_NOTIF_ID = 1 private const val SERVICE_NOTIF_ID = 1
private const val MISSED_CALLS_NOTIF_ID = 2 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 CALL_TAG = "Call"
private const val MISSED_CALL_TAG = "Missed call" private const val MISSED_CALL_TAG = "Missed call"
} }
@ -762,20 +762,23 @@ class NotificationsManager(private val context: Context) {
displayChatNotifiable(message.chatRoom, notifiable) displayChatNotifiable(message.chatRoom, notifiable)
} }
fun dismissChatNotification(room: ChatRoom) { fun dismissChatNotification(room: ChatRoom): Boolean {
val address = room.peerAddress.asStringUriOnly() val address = room.peerAddress.asStringUriOnly()
val notifiable: Notifiable? = chatNotificationsMap[address] val notifiable: Notifiable? = chatNotificationsMap[address]
if (notifiable != null) { if (notifiable != null) {
Log.i("[Notifications Manager] Dismissing notification for chat room $room with id ${notifiable.notificationId}") Log.i("[Notifications Manager] Dismissing notification for chat room $room with id ${notifiable.notificationId}")
notifiable.messages.clear() notifiable.messages.clear()
cancel(notifiable.notificationId, CHAT_TAG) cancel(notifiable.notificationId, CHAT_TAG)
return true
} else { } else {
val previousNotificationId = previousChatNotifications.find { id -> id == room.creationTime.toInt() } val previousNotificationId = previousChatNotifications.find { id -> id == room.creationTime.toInt() }
if (previousNotificationId != null) { if (previousNotificationId != null) {
Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId], canceling it") Log.i("[Notifications Manager] Found previous notification with same ID [$previousNotificationId], canceling it")
cancel(previousNotificationId, CHAT_TAG) cancel(previousNotificationId, CHAT_TAG)
return true
} }
} }
return false
} }
fun disableDismissNotificationUponReadForChatRoom(chatRoom: ChatRoom) { fun disableDismissNotificationUponReadForChatRoom(chatRoom: ChatRoom) {