From 2ee8afed7e27e08e8be8d133f2762c8d07711a4c Mon Sep 17 00:00:00 2001 From: dab246 Date: Fri, 10 May 2024 12:22:59 +0700 Subject: [PATCH] Set silent for all notifications that are not groups --- .../listener/email_change_listener.dart | 27 +++-- .../local_notification_config.dart | 3 +- .../local_notification_manager.dart | 99 +++++++++---------- 3 files changed, 65 insertions(+), 64 deletions(-) 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 9c2cf976d..4839b0a05 100644 --- a/lib/features/push_notification/presentation/listener/email_change_listener.dart +++ b/lib/features/push_notification/presentation/listener/email_change_listener.dart @@ -173,17 +173,19 @@ class EmailChangeListener extends ChangeListener { } } - Future _showLocalNotification( - UserName userName, - PresentationEmail presentationEmail - ) async { - await LocalNotificationManager.instance.showPushNotification( + Future _showLocalNotification({ + required UserName userName, + required PresentationEmail presentationEmail, + bool silent = false, + }) async { + return await LocalNotificationManager.instance.showPushNotification( id: presentationEmail.id?.id.value ?? '', title: presentationEmail.subject ?? '', message: presentationEmail.preview, emailAddress: presentationEmail.firstFromAddress, payload: NotificationPayload(emailId: presentationEmail.id).encodeToString, - groupId: userName.value + groupId: userName.value, + silent: silent ); } @@ -267,11 +269,20 @@ class EmailChangeListener extends ChangeListener { } for (var presentationEmail in emailList) { - await _showLocalNotification(userName, presentationEmail); + await _showLocalNotification( + userName: userName, + presentationEmail: presentationEmail, + silent: PlatformInfo.isAndroid + ); } if (PlatformInfo.isAndroid) { - await LocalNotificationManager.instance.groupPushNotification(groupId: userName.value); + final countNotifications = await LocalNotificationManager.instance + .getCountActiveNotificationByGroupOnAndroid(groupId: userName.value); + + await LocalNotificationManager.instance.groupPushNotificationOnAndroid( + groupId: userName.value, + countNotifications: countNotifications); } _emailsAvailablePushNotification.clear(); diff --git a/lib/features/push_notification/presentation/notification/local_notification_config.dart b/lib/features/push_notification/presentation/notification/local_notification_config.dart index ecd8ab4f3..302976376 100644 --- a/lib/features/push_notification/presentation/notification/local_notification_config.dart +++ b/lib/features/push_notification/presentation/notification/local_notification_config.dart @@ -2,7 +2,6 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart'; class LocalNotificationConfig { static const String NOTIFICATION_CHANNEL = 'New Email'; - static const int MIN_EMAILS_TO_GROUP = 3; static const iosInitializationSettings = DarwinInitializationSettings(); static const androidInitializationSettings = AndroidInitializationSettings('notification_icon'); @@ -16,6 +15,7 @@ class LocalNotificationConfig { NotificationDetails generateNotificationDetails({ StyleInformation? styleInformation, bool setAsGroup = false, + bool silent = false, String? groupId, }) { return NotificationDetails( @@ -28,6 +28,7 @@ class LocalNotificationConfig { priority: Priority.high, setAsGroupSummary: setAsGroup, styleInformation: styleInformation, + silent: silent, groupAlertBehavior: setAsGroup ? GroupAlertBehavior.summary : GroupAlertBehavior.all, 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 d4187b7b6..776efaa58 100644 --- a/lib/features/push_notification/presentation/notification/local_notification_manager.dart +++ b/lib/features/push_notification/presentation/notification/local_notification_manager.dart @@ -100,7 +100,7 @@ class LocalNotificationManager { if (PlatformInfo.isAndroid) { return await _localNotificationsPlugin .resolvePlatformSpecificImplementation() - ?.requestPermission() ?? false; + ?.requestNotificationsPermission() ?? false; } else if (PlatformInfo.isIOS) { return await _localNotificationsPlugin .resolvePlatformSpecificImplementation() @@ -132,79 +132,68 @@ class LocalNotificationManager { Future showPushNotification({ required String id, required String title, + bool silent = false, String? message, EmailAddress? emailAddress, String? payload, - bool isInboxStyle = true, String? groupId, }) async { - if (isInboxStyle) { - final inboxStyleInformation = InboxStyleInformation( - [message?.addBlockTag('p', attribute: 'style="color:#6D7885;"') ?? ''], - htmlFormatLines: true, - contentTitle: title, - htmlFormatContentTitle: true, - summaryText: (emailAddress?.asString() ?? '').addBlockTag('b'), - htmlFormatSummaryText: true, - ); + final inboxStyleInformation = InboxStyleInformation( + [message?.addBlockTag('p', attribute: 'style="color:#6D7885;"') ?? ''], + htmlFormatLines: true, + contentTitle: title, + htmlFormatContentTitle: true, + summaryText: (emailAddress?.asString() ?? '').addBlockTag('b'), + htmlFormatSummaryText: true); - await _localNotificationsPlugin.show( - id.hashCode, - title, - message, - LocalNotificationConfig.instance.generateNotificationDetails( - styleInformation: inboxStyleInformation, - groupId: groupId - ), - payload: payload - ); - } else { - await _localNotificationsPlugin.show( - id.hashCode, - title, - message, - LocalNotificationConfig.instance.generateNotificationDetails( - styleInformation: const DefaultStyleInformation(true, true), - groupId: groupId - ), - payload: payload - ); - } + await _localNotificationsPlugin.show( + id.hashCode, + title, + message, + LocalNotificationConfig.instance.generateNotificationDetails( + styleInformation: inboxStyleInformation, + groupId: groupId, + silent: silent + ), + payload: payload); } Future removeNotification(String id) async { return _localNotificationsPlugin.cancel(id.hashCode); } - Future groupPushNotification({required String groupId}) async { + Future getCountActiveNotificationByGroupOnAndroid({required String groupId}) async { final activeNotifications = await _localNotificationsPlugin .resolvePlatformSpecificImplementation() ?.getActiveNotifications() ?? []; final listActiveNotificationByGroup = activeNotifications - .where((notification) => notification.groupKey == groupId) + .where((notification) => notification.groupKey == groupId && notification.id != groupId.hashCode) .toList(); - log('LocalNotificationManager::groupPushNotification(): groupId = $groupId | activeNotifications = ${activeNotifications.length} | listActiveNotificationByGroup = ${listActiveNotificationByGroup.length}'); - if (listActiveNotificationByGroup.length >= LocalNotificationConfig.MIN_EMAILS_TO_GROUP) { - final inboxStyleInformation = InboxStyleInformation( - [''], - summaryText: currentContext != null - ? AppLocalizations.of(currentContext!).totalNewMessagePushNotification(listActiveNotificationByGroup.length).addBlockTag('b') - : '${listActiveNotificationByGroup.length} new emails'.addBlockTag('b'), - htmlFormatSummaryText: true, - ); + log('LocalNotificationManager::getCountActiveNotificationByGroupOnAndroid(): groupId = $groupId | activeNotifications = ${activeNotifications.length} | listActiveNotificationByGroup = ${listActiveNotificationByGroup.length}'); + return listActiveNotificationByGroup.length; + } - await _localNotificationsPlugin.show( - groupId.hashCode, - null, - null, - LocalNotificationConfig.instance.generateNotificationDetails( - setAsGroup: true, - styleInformation: inboxStyleInformation, - groupId: groupId - ), - ); - } + Future groupPushNotificationOnAndroid({required String groupId, required int countNotifications}) async { + log('LocalNotificationManager::groupPushNotificationOnAndroid:groupId = $groupId'); + final inboxStyleInformation = InboxStyleInformation( + [''], + summaryText: currentContext != null + ? AppLocalizations.of(currentContext!).totalNewMessagePushNotification(countNotifications).addBlockTag('b') + : '$countNotifications new emails'.addBlockTag('b'), + htmlFormatSummaryText: true, + ); + + await _localNotificationsPlugin.show( + groupId.hashCode, + null, + null, + LocalNotificationConfig.instance.generateNotificationDetails( + setAsGroup: true, + styleInformation: inboxStyleInformation, + groupId: groupId + ), + ); } Future removeGroupPushNotification(String groupId) async {