TF-1217 Create local notification manager
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
|||||||
|
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 iosInitializationSettings = DarwinInitializationSettings();
|
||||||
|
|
||||||
|
static const androidInitializationSettings = AndroidInitializationSettings('background');
|
||||||
|
|
||||||
|
static const androidNotificationChannel = AndroidNotificationChannel(
|
||||||
|
channelId,
|
||||||
|
channelName,
|
||||||
|
description: channelDescription,
|
||||||
|
importance: Importance.max,
|
||||||
|
enableLights: 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,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
+101
@@ -0,0 +1,101 @@
|
|||||||
|
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:core/utils/app_logger.dart';
|
||||||
|
import 'package:flutter_local_notifications/flutter_local_notifications.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();
|
||||||
|
|
||||||
|
static final LocalNotificationManager _instance = LocalNotificationManager._internal();
|
||||||
|
|
||||||
|
static LocalNotificationManager get instance => _instance;
|
||||||
|
|
||||||
|
final _localNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
||||||
|
bool notificationsEnabled = false;
|
||||||
|
|
||||||
|
Future<void> setUp() async {
|
||||||
|
try {
|
||||||
|
await _initLocalNotification();
|
||||||
|
_checkLocalNotificationPermission();
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
await _createAndroidNotificationChannels();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logError('LocalNotificationManager::setUp(): ERROR: ${e.toString()}');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool?> _initLocalNotification() async {
|
||||||
|
return await _localNotificationsPlugin.initialize(
|
||||||
|
const InitializationSettings(
|
||||||
|
android: LocalNotificationConfig.androidInitializationSettings,
|
||||||
|
iOS: LocalNotificationConfig.iosInitializationSettings
|
||||||
|
),
|
||||||
|
onDidReceiveNotificationResponse: _handleReceiveNotificationResponse,
|
||||||
|
onDidReceiveBackgroundNotificationResponse: _handleReceiveBackgroundNotificationResponse
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _handleReceiveNotificationResponse(NotificationResponse response) {
|
||||||
|
log('LocalNotificationManager::handleReceiveNotificationResponse(): $response');
|
||||||
|
}
|
||||||
|
|
||||||
|
void _checkLocalNotificationPermission() async {
|
||||||
|
if (notificationsEnabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
final granted = await _isAndroidPermissionGranted();
|
||||||
|
if (!granted) {
|
||||||
|
notificationsEnabled = await _requestPermissions();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
notificationsEnabled = await _requestPermissions();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _isAndroidPermissionGranted() async {
|
||||||
|
return await _localNotificationsPlugin
|
||||||
|
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
||||||
|
?.areNotificationsEnabled() ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _requestPermissions() async {
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
return await _localNotificationsPlugin
|
||||||
|
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
||||||
|
?.requestPermission() ?? false;
|
||||||
|
} else {
|
||||||
|
return await _localNotificationsPlugin
|
||||||
|
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
|
||||||
|
?.requestPermissions(alert: true, badge: true, sound: true) ?? false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _createAndroidNotificationChannels() 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);
|
||||||
|
|
||||||
|
await _localNotificationsPlugin.show(
|
||||||
|
generateNotificationId,
|
||||||
|
title ?? LocalNotificationConfig.channelName,
|
||||||
|
message ?? LocalNotificationConfig.channelDescription,
|
||||||
|
LocalNotificationConfig.pushNotificationDetails,
|
||||||
|
payload: payload
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -623,6 +623,27 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.4"
|
version: "1.0.4"
|
||||||
|
flutter_local_notifications:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "12.0.3"
|
||||||
|
flutter_local_notifications_linux:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications_linux
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.0"
|
||||||
|
flutter_local_notifications_platform_interface:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications_platform_interface
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "6.0.0"
|
||||||
flutter_localizations:
|
flutter_localizations:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description: flutter
|
description: flutter
|
||||||
@@ -1386,6 +1407,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.2.2"
|
version: "3.2.2"
|
||||||
|
timezone:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: timezone
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.9.0"
|
||||||
timing:
|
timing:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -198,6 +198,7 @@ dependencies:
|
|||||||
url_launcher: 6.1.5
|
url_launcher: 6.1.5
|
||||||
firebase_core: 2.2.0
|
firebase_core: 2.2.0
|
||||||
firebase_messaging: 14.0.4
|
firebase_messaging: 14.0.4
|
||||||
|
flutter_local_notifications: 12.0.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user