TF-438 Handle show local notification when type is EmailDelivery on background or terminate

This commit is contained in:
dab246
2022-11-24 14:35:05 +07:00
committed by Dat H. Pham
parent 552398006d
commit d42756b4ea
10 changed files with 274 additions and 64 deletions
@@ -1,38 +1,56 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class LocalNotificationConfig {
static const channelId = 'team_mail_channel_id';
static const channelKey = 'team_mail_channel_key';
static const channelName = 'Team Mail notifications';
static const channelDescription = 'Team Mail notifications';
static const _groupId = 'team_mail_notification_group_id';
static const _groupName = 'team_mail_notification_group_name';
static const _groupDescription = 'Team Mail group notifications';
static const _channelId = 'team_mail_notification_channel_id';
static const _channelName = 'Team Mail notifications';
static const _channelDescription = 'Team Mail notifications';
static const iosInitializationSettings = DarwinInitializationSettings();
static const androidInitializationSettings = AndroidInitializationSettings('background');
static const androidInitializationSettings = AndroidInitializationSettings('ic_notification');
static const androidNotificationChannel = AndroidNotificationChannel(
channelId,
channelName,
description: channelDescription,
_channelId,
_channelName,
description: _channelDescription,
groupId: _groupId,
importance: Importance.max,
enableLights: true,
showBadge: true
);
static const pushNotificationDetails = NotificationDetails(
android: AndroidNotificationDetails(
channelId,
channelName,
channelDescription: channelDescription,
visibility: NotificationVisibility.public,
importance: Importance.max,
ledOnMs: 100,
ledOffMs: 1000,
category: AndroidNotificationCategory.message,
),
iOS: DarwinNotificationDetails(
presentSound: true,
presentAlert: true,
presentBadge: true,
),
static const androidNotificationChannelGroup = AndroidNotificationChannelGroup(
_groupId,
_groupName,
description: _groupDescription
);
LocalNotificationConfig._internal();
static final LocalNotificationConfig _instance = LocalNotificationConfig._internal();
static LocalNotificationConfig get instance => _instance;
NotificationDetails generateNotificationDetails({StyleInformation? styleInformation, bool setAsGroup = false}) {
return NotificationDetails(
android: AndroidNotificationDetails(
androidNotificationChannel.id,
androidNotificationChannel.name,
channelDescription: androidNotificationChannel.description,
groupKey: androidNotificationChannel.groupId,
visibility: NotificationVisibility.public,
importance: Importance.max,
priority: Priority.high,
setAsGroupSummary: setAsGroup,
styleInformation: styleInformation
),
iOS: const DarwinNotificationDetails(
presentSound: true,
presentAlert: true,
presentBadge: true,
),
);
}
}
@@ -1,15 +1,13 @@
import 'dart:io';
import 'package:core/presentation/extensions/html_extension.dart';
import 'package:core/utils/app_logger.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:jmap_dart_client/jmap/mail/email/email_address.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_config.dart';
@pragma('vm:entry-point')
void _handleReceiveBackgroundNotificationResponse(NotificationResponse notificationResponse) {
log('LocalNotificationManager::_handleReceiveBackgroundNotificationResponse():notificationResponse: $notificationResponse');
}
class LocalNotificationManager {
LocalNotificationManager._internal();
@@ -26,7 +24,8 @@ class LocalNotificationManager {
await _initLocalNotification();
_checkLocalNotificationPermission();
if (Platform.isAndroid) {
await _createAndroidNotificationChannels();
await _createAndroidNotificationChannelGroup();
await _createAndroidNotificationChannel();
}
} catch (e) {
logError('LocalNotificationManager::setUp(): ERROR: ${e.toString()}');
@@ -39,8 +38,7 @@ class LocalNotificationManager {
android: LocalNotificationConfig.androidInitializationSettings,
iOS: LocalNotificationConfig.iosInitializationSettings
),
onDidReceiveNotificationResponse: _handleReceiveNotificationResponse,
onDidReceiveBackgroundNotificationResponse: _handleReceiveBackgroundNotificationResponse
onDidReceiveNotificationResponse: _handleReceiveNotificationResponse
);
}
@@ -81,21 +79,56 @@ class LocalNotificationManager {
}
}
Future<void> _createAndroidNotificationChannels() async {
Future<void> _createAndroidNotificationChannel() async {
return await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(LocalNotificationConfig.androidNotificationChannel);
}
void showPushNotification({String? title, String? message, String? payload}) async {
final generateNotificationId = DateTime.now().millisecondsSinceEpoch.remainder(100000);
Future<void> _createAndroidNotificationChannelGroup() async {
await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
.createNotificationChannelGroup(LocalNotificationConfig.androidNotificationChannelGroup);
}
void showPushNotification({
required String id,
required String title,
String? message,
EmailAddress? emailAddress,
String? payload
}) async {
final inboxStyleInformation = InboxStyleInformation(
[title, message ?? ''],
contentTitle: (emailAddress?.asString() ?? '').addBlockTag('b'),
summaryText: (emailAddress?.asString() ?? '').addBlockTag('b'),
htmlFormatTitle: true,
htmlFormatContent: true,
htmlFormatContentTitle: true,
htmlFormatSummaryText: true,
);
await _localNotificationsPlugin.show(
generateNotificationId,
title ?? LocalNotificationConfig.channelName,
message ?? LocalNotificationConfig.channelDescription,
LocalNotificationConfig.pushNotificationDetails,
id.hashCode,
title.addBlockTag('b'),
message ?? '',
LocalNotificationConfig.instance.generateNotificationDetails(styleInformation: inboxStyleInformation),
payload: payload
);
}
void groupPushNotification() async {
final activeNotifications = await _localNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.getActiveNotifications();
if (activeNotifications != null && activeNotifications.isNotEmpty) {
await _localNotificationsPlugin.show(
1995,
'',
'',
LocalNotificationConfig.instance.generateNotificationDetails(setAsGroup: true)
);
}
}
}