From 18b270a15ccbc4303d238368e85b5aa394c856ca Mon Sep 17 00:00:00 2001 From: krille-chan Date: Sun, 6 Aug 2023 21:13:37 +0200 Subject: [PATCH] fix: Cancel notifications for read rooms and clear app badge on iOS --- lib/pages/chat/chat.dart | 3 ++ lib/utils/background_push.dart | 40 +++++++++---------- .../local_notifications_extension.dart | 13 ------ 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/lib/pages/chat/chat.dart b/lib/pages/chat/chat.dart index 9a467ba7..c57eef06 100644 --- a/lib/pages/chat/chat.dart +++ b/lib/pages/chat/chat.dart @@ -359,6 +359,9 @@ class ChatController extends State { _setReadMarkerFuture = timeline.setReadMarker(eventId: eventId).then((_) { _setReadMarkerFuture = null; }); + if (eventId == timeline.events.first.eventId) { + Matrix.of(context).backgroundPush?.cancelNotification(roomId); + } } @override diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart index 8876eaf6..d4dacbb6 100644 --- a/lib/utils/background_push.dart +++ b/lib/utils/background_push.dart @@ -25,6 +25,7 @@ import 'dart:ui'; import 'package:flutter/material.dart'; +import 'package:flutter_app_badger/flutter_app_badger.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:http/http.dart' as http; @@ -114,6 +115,25 @@ class BackgroundPush { return instance; } + Future cancelNotification(String roomId) async { + Logs().v('Cancel notification for room', roomId); + final id = await mapRoomIdToInt(roomId); + await FlutterLocalNotificationsPlugin().cancel(id); + + // Workaround for app icon badge not updating + if (Platform.isIOS) { + final unreadCount = client.rooms + .where((room) => room.isUnreadOrInvited && room.id != roomId) + .length; + if (unreadCount == 0) { + FlutterAppBadger.removeBadge(); + } else { + FlutterAppBadger.updateBadgeCount(unreadCount); + } + return; + } + } + StreamSubscription? onRoomSync; Future setupPusher({ @@ -390,26 +410,6 @@ class BackgroundPush { ); } - Future mapRoomIdToInt(String roomId) async { - await _loadIdMap(); - int? currentInt; - try { - currentInt = idMap[roomId]; - } catch (_) { - currentInt = null; - } - if (currentInt != null) { - return currentInt; - } - var nCurrentInt = 0; - while (idMap.values.contains(currentInt)) { - nCurrentInt++; - } - idMap[roomId] = nCurrentInt; - await store.setItem(SettingKeys.notificationCurrentIds, json.encode(idMap)); - return nCurrentInt; - } - bool _clearingPushLock = false; Future _onClearingPush({bool getFromServer = true}) async { if (_clearingPushLock) { diff --git a/lib/widgets/local_notifications_extension.dart b/lib/widgets/local_notifications_extension.dart index 41b4c50f..d7a74b1d 100644 --- a/lib/widgets/local_notifications_extension.dart +++ b/lib/widgets/local_notifications_extension.dart @@ -4,7 +4,6 @@ import 'package:flutter/foundation.dart'; import 'package:desktop_lifecycle/desktop_lifecycle.dart'; import 'package:desktop_notifications/desktop_notifications.dart'; -import 'package:flutter_app_badger/flutter_app_badger.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:http/http.dart' as http; import 'package:matrix/matrix.dart'; @@ -33,18 +32,6 @@ extension LocalNotificationsExtension on MatrixState { } if (room.notificationCount == 0) return; - // Workaround for app icon badge not updating - if (Platform.isIOS) { - final unreadCount = - client.rooms.where((room) => room.isUnreadOrInvited).length; - if (unreadCount == 0) { - FlutterAppBadger.removeBadge(); - } else { - FlutterAppBadger.updateBadgeCount(unreadCount); - } - return; - } - final event = Event.fromJson(eventUpdate.content, room); final title = room.getLocalizedDisplayname(MatrixLocals(L10n.of(widget.context)!));