Improve push notifications (#4896)

Besides the refactoring these improvements:
* Track last push distributor and reset settings and subscription on any
incompatible change (ie. uninstall)
* Only update (push) notification settings on server if needed
* Allow to only fetch notifications for one account (the one for which a
push message was received)

This is (also) the revival of
https://github.com/tuskyapp/Tusky/pull/3642

It's not really well tested so far. (Ie. with two or more accounts or
two or more push providers.)
This commit is contained in:
UlrichKu 2025-02-20 12:27:06 +01:00 committed by GitHub
commit 6450af6edb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 274 additions and 127 deletions

View file

@ -27,6 +27,7 @@ import com.keylesspalace.tusky.appstore.NewNotificationsEvent
import com.keylesspalace.tusky.appstore.NotificationsLoadingEvent
import com.keylesspalace.tusky.components.systemnotifications.NotificationService
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.db.entity.AccountEntity
import com.keylesspalace.tusky.entity.Emoji
import com.keylesspalace.tusky.entity.Notification
import com.keylesspalace.tusky.entity.Status
@ -97,10 +98,10 @@ class MainViewModel @Inject constructor(
shareShortcutHelper.updateShortcuts()
setupNotifications()
setupNotifications(activeAccount)
},
{ throwable ->
Log.w(TAG, "Failed to fetch user info.", throwable)
Log.e(TAG, "Failed to fetch user info.", throwable)
}
)
}
@ -160,15 +161,24 @@ class MainViewModel @Inject constructor(
}
}
fun setupNotifications() {
notificationService.createNotificationChannelsForAccount(activeAccount)
fun setupNotifications(account: AccountEntity? = null) {
// TODO this is only called on full app (re) start; so changes in-between (push distributor uninstalled/subscription changed, or
// notifications fully disabled) will get unnoticed; and also an app restart cannot be easily triggered by the user.
if (notificationService.areNotificationsEnabled()) {
if (account != null) {
// TODO it's quite odd to separate channel creation (for an account) from the "is enabled by channels" question below
notificationService.createNotificationChannelsForAccount(account)
}
if (notificationService.areNotificationsEnabledBySystem()) {
viewModelScope.launch {
notificationService.enablePushNotificationsWithFallback()
notificationService.setupNotifications(account)
}
} else {
notificationService.disableAllNotifications()
viewModelScope.launch {
notificationService.disableAllNotifications()
}
}
}