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(
UserName userName,
PresentationEmail presentationEmail
) async {
await LocalNotificationManager.instance.showPushNotification(
Future<void> _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();
@@ -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,
@@ -100,7 +100,7 @@ class LocalNotificationManager {
if (PlatformInfo.isAndroid) {
return await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.requestPermission() ?? false;
?.requestNotificationsPermission() ?? false;
} else if (PlatformInfo.isIOS) {
return await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
@@ -132,79 +132,68 @@ class LocalNotificationManager {
Future<void> 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<void> removeNotification(String id) async {
return _localNotificationsPlugin.cancel(id.hashCode);
}
Future<void> groupPushNotification({required String groupId}) async {
Future<int> getCountActiveNotificationByGroupOnAndroid({required String groupId}) async {
final activeNotifications = await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.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<void> 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<void> removeGroupPushNotification(String groupId) async {