android, desktop: notifications improvement (missed call, hidden user) (#4389)

* android, desktop: notifications improvement (missed call, hidden user)

* change

* change
This commit is contained in:
Stanislav Dmitrenko
2024-07-03 00:19:43 +07:00
committed by GitHub
parent 85af368371
commit ecff3c6ee5
9 changed files with 52 additions and 10 deletions
@@ -179,6 +179,7 @@ class SimplexApp: Application(), LifecycleEventObserver {
override fun notifyCallInvitation(invitation: RcvCallInvitation): Boolean = NtfManager.notifyCallInvitation(invitation)
override fun hasNotificationsForChat(chatId: String): Boolean = NtfManager.hasNotificationsForChat(chatId)
override fun cancelNotificationsForChat(chatId: String) = NtfManager.cancelNotificationsForChat(chatId)
override fun cancelNotificationsForUser(userId: Long) = NtfManager.cancelNotificationsForUser(userId)
override fun displayNotification(user: UserLike, chatId: String, displayName: String, msgText: String, image: String?, actions: List<Pair<NotificationAction, () -> Unit>>) = NtfManager.displayNotification(user, chatId, displayName, msgText, image, actions.map { it.first })
override fun androidCreateNtfChannelsMaybeShowAlert() = NtfManager.createNtfChannelsMaybeShowAlert()
override fun cancelCallNotification() = NtfManager.cancelCallNotification()
@@ -48,7 +48,8 @@ object NtfManager {
}
private val manager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private var prevNtfTime = mutableMapOf<String, Long>()
// (UserId, ChatId) -> Time
private var prevNtfTime = mutableMapOf<Pair<Long, ChatId>, Long>()
private val msgNtfTimeoutMs = 30000L
init {
@@ -72,7 +73,8 @@ object NtfManager {
}
fun cancelNotificationsForChat(chatId: String) {
prevNtfTime.remove(chatId)
val key = prevNtfTime.keys.firstOrNull { it.second == chatId }
prevNtfTime.remove(key)
manager.cancel(chatId.hashCode())
val msgNtfs = manager.activeNotifications.filter { ntf ->
ntf.notification.channelId == MessageChannel
@@ -83,12 +85,26 @@ object NtfManager {
}
}
fun cancelNotificationsForUser(userId: Long) {
prevNtfTime.keys.filter { it.first == userId }.forEach {
prevNtfTime.remove(it)
manager.cancel(it.second.hashCode())
}
val msgNtfs = manager.activeNotifications.filter { ntf ->
ntf.notification.channelId == MessageChannel
}
if (msgNtfs.size <= 1) {
// Have a group notification with no children so cancel it
manager.cancel(0)
}
}
fun displayNotification(user: UserLike, chatId: String, displayName: String, msgText: String, image: String? = null, actions: List<NotificationAction> = emptyList()) {
if (!user.showNotifications) return
Log.d(TAG, "notifyMessageReceived $chatId")
val now = Clock.System.now().toEpochMilliseconds()
val recentNotification = (now - prevNtfTime.getOrDefault(chatId, 0) < msgNtfTimeoutMs)
prevNtfTime[chatId] = now
val recentNotification = (now - prevNtfTime.getOrDefault(user.userId to chatId, 0) < msgNtfTimeoutMs)
prevNtfTime[user.userId to chatId] = now
val previewMode = appPreferences.notificationPreviewMode.get()
val title = if (previewMode == NotificationPreviewMode.HIDDEN.name) generalGetString(MR.strings.notification_preview_somebody) else displayName
val content = if (previewMode != NotificationPreviewMode.MESSAGE.name) generalGetString(MR.strings.notification_preview_new_message) else msgText