From b939c59fb0689d163b3d041303beaa4d5dd62eeb Mon Sep 17 00:00:00 2001 From: dab246 Date: Mon, 10 Jul 2023 13:51:24 +0700 Subject: [PATCH] TF-1898 Fix there is no notification badge only on IOS (cherry picked from commit c8c8a03267846df5c5d2032553988fd393fbc02d) --- ios/Podfile.lock | 6 +++++ .../mailbox_dashboard_controller.dart | 3 ++- .../config/fcm_configuration.dart | 1 + .../listener/email_change_listener.dart | 3 ++- .../local_notification_manager.dart | 22 ++++++++++++++++++- .../presentation/services/fcm_receiver.dart | 4 ++++ .../presentation/services/fcm_service.dart | 6 +++++ pubspec.lock | 8 +++++++ pubspec.yaml | 2 ++ 9 files changed, 52 insertions(+), 3 deletions(-) diff --git a/ios/Podfile.lock b/ios/Podfile.lock index cada0c7e5..ad37c6eb7 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -85,6 +85,8 @@ PODS: - fk_user_agent (2.0.0): - Flutter - Flutter (1.0.0) + - flutter_app_badger (1.3.0): + - Flutter - flutter_appauth (0.0.1): - AppAuth (= 1.6.0) - Flutter @@ -186,6 +188,7 @@ DEPENDENCIES: - firebase_messaging (from `.symlinks/plugins/firebase_messaging/ios`) - fk_user_agent (from `.symlinks/plugins/fk_user_agent/ios`) - Flutter (from `Flutter`) + - flutter_app_badger (from `.symlinks/plugins/flutter_app_badger/ios`) - flutter_appauth (from `.symlinks/plugins/flutter_appauth/ios`) - flutter_downloader (from `.symlinks/plugins/flutter_downloader/ios`) - flutter_image_compress (from `.symlinks/plugins/flutter_image_compress/ios`) @@ -245,6 +248,8 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/fk_user_agent/ios" Flutter: :path: Flutter + flutter_app_badger: + :path: ".symlinks/plugins/flutter_app_badger/ios" flutter_appauth: :path: ".symlinks/plugins/flutter_appauth/ios" flutter_downloader: @@ -295,6 +300,7 @@ SPEC CHECKSUMS: FirebaseMessaging: e345b219fd15d325f0cf2fef28cb8ce00d851b3f fk_user_agent: 1f47ec39291e8372b1d692b50084b0d54103c545 Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854 + flutter_app_badger: b87fc231847b03b92ce1412aa351842e7e97932f flutter_appauth: 525652bda90e43ca5eb7ed748ecc106b79d1f5a1 flutter_downloader: b7301ae057deadd4b1650dc7c05375f10ff12c39 flutter_image_compress: 5a5e9aee05b6553048b8df1c3bc456d0afaac433 diff --git a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart index 9fa1775f3..c15f2e0a4 100644 --- a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart +++ b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart @@ -1537,7 +1537,8 @@ class MailboxDashBoardController extends ReloadableController { _handleMessageFromNotification(response?.payload); } - void _handleMessageFromNotification(String? payload, {bool onForeground = true}) { + void _handleMessageFromNotification(String? payload, {bool onForeground = true}) async { + await LocalNotificationManager.instance.removeNotificationBadgeForIOS(); log('MailboxDashBoardController::_handleMessageFromNotification():payload: $payload'); if (payload == null || payload.isEmpty) { dispatchRoute(DashboardRoutes.thread); diff --git a/lib/features/push_notification/presentation/config/fcm_configuration.dart b/lib/features/push_notification/presentation/config/fcm_configuration.dart index fa5ecb863..77a9a7217 100644 --- a/lib/features/push_notification/presentation/config/fcm_configuration.dart +++ b/lib/features/push_notification/presentation/config/fcm_configuration.dart @@ -15,6 +15,7 @@ class FcmConfiguration { static void _initMessageListener() { FcmReceiver.instance.onForegroundMessage(); FcmReceiver.instance.onBackgroundMessage(); + FcmReceiver.instance.onMessageOpenedApp(); FcmReceiver.instance.getFcmToken(); FcmReceiver.instance.onRefreshFcmToken(); } diff --git a/lib/features/push_notification/presentation/listener/email_change_listener.dart b/lib/features/push_notification/presentation/listener/email_change_listener.dart index 298e4da45..8621ba2b7 100644 --- a/lib/features/push_notification/presentation/listener/email_change_listener.dart +++ b/lib/features/push_notification/presentation/listener/email_change_listener.dart @@ -199,7 +199,7 @@ class EmailChangeListener extends ChangeListener { ); } - void _showLocalNotificationForIOS(jmap.State newState, AccountId accountId) { + void _showLocalNotificationForIOS(jmap.State newState, AccountId accountId) async { final notificationPayload = NotificationPayload(newState: newState); log('EmailChangeListener::_showLocalNotificationForIOS():notificationPayload: $notificationPayload'); LocalNotificationManager.instance.showPushNotification( @@ -212,6 +212,7 @@ class EmailChangeListener extends ChangeListener { : LocalNotificationConfig.notificationMessage, payload: notificationPayload.encodeToString, ); + await LocalNotificationManager.instance.setNotificationBadgeForIOS(); } @override diff --git a/lib/features/push_notification/presentation/notification/local_notification_manager.dart b/lib/features/push_notification/presentation/notification/local_notification_manager.dart index c137a4d88..abf2f2c2e 100644 --- a/lib/features/push_notification/presentation/notification/local_notification_manager.dart +++ b/lib/features/push_notification/presentation/notification/local_notification_manager.dart @@ -4,10 +4,13 @@ import 'dart:io'; import 'package:core/presentation/extensions/html_extension.dart'; import 'package:core/utils/app_logger.dart'; +import 'package:core/utils/platform_info.dart'; +import 'package:flutter_app_badger/flutter_app_badger.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:jmap_dart_client/jmap/mail/email/email_address.dart'; import 'package:model/extensions/email_address_extension.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_config.dart'; +import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; @@ -162,7 +165,10 @@ class LocalNotificationManager { } } - Future removeNotification(String id) { + Future removeNotification(String id) async { + if (id.startsWith(FcmUtils.instance.platformOS)) { + await removeNotificationBadgeForIOS(); + } return _localNotificationsPlugin.cancel(id.hashCode); } @@ -217,4 +223,18 @@ class LocalNotificationManager { void closeStream() { localNotificationsController.close(); } + + Future setNotificationBadgeForIOS() async { + log("LocalNotificationManager::setNotificationBadgeForIOS:"); + if (PlatformInfo.isIOS) { + await FlutterAppBadger.updateBadgeCount(1); + } + } + + Future removeNotificationBadgeForIOS() async { + log("LocalNotificationManager::removeNotificationBadgeForIOS:"); + if (PlatformInfo.isIOS) { + await FlutterAppBadger.removeBadge(); + } + } } \ No newline at end of file diff --git a/lib/features/push_notification/presentation/services/fcm_receiver.dart b/lib/features/push_notification/presentation/services/fcm_receiver.dart index e3daf22ad..dd8c0b83a 100644 --- a/lib/features/push_notification/presentation/services/fcm_receiver.dart +++ b/lib/features/push_notification/presentation/services/fcm_receiver.dart @@ -26,6 +26,10 @@ class FcmReceiver { FirebaseMessaging.onBackgroundMessage(handleFirebaseBackgroundMessage); } + void onMessageOpenedApp() { + FirebaseMessaging.onMessageOpenedApp.listen(FcmService.instance.handleFirebaseMessageOpenedApp); + } + void getFcmToken() async { try { final currentToken = await FirebaseMessaging.instance.getToken(vapidKey: AppUtils.fcmVapidPublicKey); diff --git a/lib/features/push_notification/presentation/services/fcm_service.dart b/lib/features/push_notification/presentation/services/fcm_service.dart index 97b204c03..b1d99131c 100644 --- a/lib/features/push_notification/presentation/services/fcm_service.dart +++ b/lib/features/push_notification/presentation/services/fcm_service.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'package:core/utils/app_logger.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_message_controller.dart'; +import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_manager.dart'; class FcmService { @@ -43,6 +44,11 @@ class FcmService { } } + void handleFirebaseMessageOpenedApp(RemoteMessage newRemoteMessage) async { + log("FcmService::handleFirebaseMessageOpenedApp:"); + await LocalNotificationManager.instance.removeNotificationBadgeForIOS(); + } + void handleGetToken(String? currentToken) async { log('FcmService::handleGetToken():currentToken: $currentToken'); if (fcmTokenStreamController.isClosed) { diff --git a/pubspec.lock b/pubspec.lock index 8894259f1..4362cc0b2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -557,6 +557,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_app_badger: + dependency: "direct main" + description: + name: flutter_app_badger + sha256: "64d4a279bab862ed28850431b9b446b9820aaae0bf363322d51077419f930fa8" + url: "https://pub.dev" + source: hosted + version: "1.5.0" flutter_appauth: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 702c1bd03..c38477d31 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -201,6 +201,8 @@ dependencies: extended_text: 10.0.1 + flutter_app_badger: 1.5.0 + dev_dependencies: flutter_test: sdk: flutter