TF-1898 Fix there is no notification badge only on IOS

(cherry picked from commit c8c8a03267846df5c5d2032553988fd393fbc02d)
This commit is contained in:
dab246
2023-07-10 13:51:24 +07:00
committed by Dat Vu
parent 356a6b8095
commit b939c59fb0
9 changed files with 52 additions and 3 deletions
@@ -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);
@@ -15,6 +15,7 @@ class FcmConfiguration {
static void _initMessageListener() {
FcmReceiver.instance.onForegroundMessage();
FcmReceiver.instance.onBackgroundMessage();
FcmReceiver.instance.onMessageOpenedApp();
FcmReceiver.instance.getFcmToken();
FcmReceiver.instance.onRefreshFcmToken();
}
@@ -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
@@ -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<void> removeNotification(String id) {
Future<void> 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<void> setNotificationBadgeForIOS() async {
log("LocalNotificationManager::setNotificationBadgeForIOS:");
if (PlatformInfo.isIOS) {
await FlutterAppBadger.updateBadgeCount(1);
}
}
Future<void> removeNotificationBadgeForIOS() async {
log("LocalNotificationManager::removeNotificationBadgeForIOS:");
if (PlatformInfo.isIOS) {
await FlutterAppBadger.removeBadge();
}
}
}
@@ -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);
@@ -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) {