Set silent for all notifications that are not groups

This commit is contained in:
dab246
2024-05-10 12:22:59 +07:00
committed by Dat H. Pham
parent bcac253efa
commit 2ee8afed7e
3 changed files with 65 additions and 64 deletions
@@ -173,17 +173,19 @@ class EmailChangeListener extends ChangeListener {
} }
} }
Future<void> _showLocalNotification( Future<void> _showLocalNotification({
UserName userName, required UserName userName,
PresentationEmail presentationEmail required PresentationEmail presentationEmail,
) async { bool silent = false,
await LocalNotificationManager.instance.showPushNotification( }) async {
return await LocalNotificationManager.instance.showPushNotification(
id: presentationEmail.id?.id.value ?? '', id: presentationEmail.id?.id.value ?? '',
title: presentationEmail.subject ?? '', title: presentationEmail.subject ?? '',
message: presentationEmail.preview, message: presentationEmail.preview,
emailAddress: presentationEmail.firstFromAddress, emailAddress: presentationEmail.firstFromAddress,
payload: NotificationPayload(emailId: presentationEmail.id).encodeToString, 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) { for (var presentationEmail in emailList) {
await _showLocalNotification(userName, presentationEmail); await _showLocalNotification(
userName: userName,
presentationEmail: presentationEmail,
silent: PlatformInfo.isAndroid
);
} }
if (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(); _emailsAvailablePushNotification.clear();
@@ -2,7 +2,6 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class LocalNotificationConfig { class LocalNotificationConfig {
static const String NOTIFICATION_CHANNEL = 'New Email'; static const String NOTIFICATION_CHANNEL = 'New Email';
static const int MIN_EMAILS_TO_GROUP = 3;
static const iosInitializationSettings = DarwinInitializationSettings(); static const iosInitializationSettings = DarwinInitializationSettings();
static const androidInitializationSettings = AndroidInitializationSettings('notification_icon'); static const androidInitializationSettings = AndroidInitializationSettings('notification_icon');
@@ -16,6 +15,7 @@ class LocalNotificationConfig {
NotificationDetails generateNotificationDetails({ NotificationDetails generateNotificationDetails({
StyleInformation? styleInformation, StyleInformation? styleInformation,
bool setAsGroup = false, bool setAsGroup = false,
bool silent = false,
String? groupId, String? groupId,
}) { }) {
return NotificationDetails( return NotificationDetails(
@@ -28,6 +28,7 @@ class LocalNotificationConfig {
priority: Priority.high, priority: Priority.high,
setAsGroupSummary: setAsGroup, setAsGroupSummary: setAsGroup,
styleInformation: styleInformation, styleInformation: styleInformation,
silent: silent,
groupAlertBehavior: setAsGroup groupAlertBehavior: setAsGroup
? GroupAlertBehavior.summary ? GroupAlertBehavior.summary
: GroupAlertBehavior.all, : GroupAlertBehavior.all,
@@ -100,7 +100,7 @@ class LocalNotificationManager {
if (PlatformInfo.isAndroid) { if (PlatformInfo.isAndroid) {
return await _localNotificationsPlugin return await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>() .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.requestPermission() ?? false; ?.requestNotificationsPermission() ?? false;
} else if (PlatformInfo.isIOS) { } else if (PlatformInfo.isIOS) {
return await _localNotificationsPlugin return await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>() .resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
@@ -132,79 +132,68 @@ class LocalNotificationManager {
Future<void> showPushNotification({ Future<void> showPushNotification({
required String id, required String id,
required String title, required String title,
bool silent = false,
String? message, String? message,
EmailAddress? emailAddress, EmailAddress? emailAddress,
String? payload, String? payload,
bool isInboxStyle = true,
String? groupId, String? groupId,
}) async { }) async {
if (isInboxStyle) { final inboxStyleInformation = InboxStyleInformation(
final inboxStyleInformation = InboxStyleInformation( [message?.addBlockTag('p', attribute: 'style="color:#6D7885;"') ?? ''],
[message?.addBlockTag('p', attribute: 'style="color:#6D7885;"') ?? ''], htmlFormatLines: true,
htmlFormatLines: true, contentTitle: title,
contentTitle: title, htmlFormatContentTitle: true,
htmlFormatContentTitle: true, summaryText: (emailAddress?.asString() ?? '').addBlockTag('b'),
summaryText: (emailAddress?.asString() ?? '').addBlockTag('b'), htmlFormatSummaryText: true);
htmlFormatSummaryText: true,
);
await _localNotificationsPlugin.show( await _localNotificationsPlugin.show(
id.hashCode, id.hashCode,
title, title,
message, message,
LocalNotificationConfig.instance.generateNotificationDetails( LocalNotificationConfig.instance.generateNotificationDetails(
styleInformation: inboxStyleInformation, styleInformation: inboxStyleInformation,
groupId: groupId groupId: groupId,
), silent: silent
payload: payload ),
); payload: payload);
} else {
await _localNotificationsPlugin.show(
id.hashCode,
title,
message,
LocalNotificationConfig.instance.generateNotificationDetails(
styleInformation: const DefaultStyleInformation(true, true),
groupId: groupId
),
payload: payload
);
}
} }
Future<void> removeNotification(String id) async { Future<void> removeNotification(String id) async {
return _localNotificationsPlugin.cancel(id.hashCode); return _localNotificationsPlugin.cancel(id.hashCode);
} }
Future<void> groupPushNotification({required String groupId}) async { Future<int> getCountActiveNotificationByGroupOnAndroid({required String groupId}) async {
final activeNotifications = await _localNotificationsPlugin final activeNotifications = await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>() .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.getActiveNotifications() ?? []; ?.getActiveNotifications() ?? [];
final listActiveNotificationByGroup = activeNotifications final listActiveNotificationByGroup = activeNotifications
.where((notification) => notification.groupKey == groupId) .where((notification) => notification.groupKey == groupId && notification.id != groupId.hashCode)
.toList(); .toList();
log('LocalNotificationManager::groupPushNotification(): groupId = $groupId | activeNotifications = ${activeNotifications.length} | listActiveNotificationByGroup = ${listActiveNotificationByGroup.length}'); log('LocalNotificationManager::getCountActiveNotificationByGroupOnAndroid(): groupId = $groupId | activeNotifications = ${activeNotifications.length} | listActiveNotificationByGroup = ${listActiveNotificationByGroup.length}');
if (listActiveNotificationByGroup.length >= LocalNotificationConfig.MIN_EMAILS_TO_GROUP) { return listActiveNotificationByGroup.length;
final inboxStyleInformation = InboxStyleInformation( }
[''],
summaryText: currentContext != null
? AppLocalizations.of(currentContext!).totalNewMessagePushNotification(listActiveNotificationByGroup.length).addBlockTag('b')
: '${listActiveNotificationByGroup.length} new emails'.addBlockTag('b'),
htmlFormatSummaryText: true,
);
await _localNotificationsPlugin.show( Future<void> groupPushNotificationOnAndroid({required String groupId, required int countNotifications}) async {
groupId.hashCode, log('LocalNotificationManager::groupPushNotificationOnAndroid:groupId = $groupId');
null, final inboxStyleInformation = InboxStyleInformation(
null, [''],
LocalNotificationConfig.instance.generateNotificationDetails( summaryText: currentContext != null
setAsGroup: true, ? AppLocalizations.of(currentContext!).totalNewMessagePushNotification(countNotifications).addBlockTag('b')
styleInformation: inboxStyleInformation, : '$countNotifications new emails'.addBlockTag('b'),
groupId: groupId htmlFormatSummaryText: true,
), );
);
} await _localNotificationsPlugin.show(
groupId.hashCode,
null,
null,
LocalNotificationConfig.instance.generateNotificationDetails(
setAsGroup: true,
styleInformation: inboxStyleInformation,
groupId: groupId
),
);
} }
Future<void> removeGroupPushNotification(String groupId) async { Future<void> removeGroupPushNotification(String groupId) async {