diff --git a/fcm_config/CHANGELOG.md b/fcm_config/CHANGELOG.md index 5d01510..2645533 100644 --- a/fcm_config/CHANGELOG.md +++ b/fcm_config/CHANGELOG.md @@ -1,3 +1,11 @@ +## [3.6.0] + +* Added call back for notification details for android,darwin(IOS,MACOS),Linux +* Added onDidReceiveBackgroundNotificationResponse for callback,which willbe fired when the user + selects a notification or notification action that should show the + application/user interface + + ## [3.5.3] * Update dependencies diff --git a/fcm_config/README.md b/fcm_config/README.md index c14468d..8f1c49d 100644 --- a/fcm_config/README.md +++ b/fcm_config/README.md @@ -8,6 +8,13 @@ * Notification is an object # Setup +```yaml +fcm_config: + git: + url: https://github.com/Shreemanarjun/fcm_config.git + path: fcm_config +``` + ## Android > - Follow steps here https://firebase.flutter.dev/docs/installation/android diff --git a/fcm_config/example/lib/main.dart b/fcm_config/example/lib/main.dart index 8d02731..8e06d51 100644 --- a/fcm_config/example/lib/main.dart +++ b/fcm_config/example/lib/main.dart @@ -51,7 +51,7 @@ class MyHomePage extends StatefulWidget { const MyHomePage({Key? key, this.locale}) : super(key: key); @override - _MyHomePageState createState() => _MyHomePageState(); + State createState() => _MyHomePageState(); } class _MyHomePageState extends State diff --git a/fcm_config/lib/src/fcm_config.dart b/fcm_config/lib/src/fcm_config.dart index bf9627f..793a60a 100644 --- a/fcm_config/lib/src/fcm_config.dart +++ b/fcm_config/lib/src/fcm_config.dart @@ -46,6 +46,21 @@ class FCMConfig extends FCMConfigInterface { /// Not work on the web BackgroundMessageHandler? onBackgroundMessage, + /// + /// The [onDidReceiveNotificationResponse] callback is fired when the user + /// selects a notification or notification action that should show the + /// application/user interface. + /// application was running. To handle when a notification launched an + /// application, use [getNotificationAppLaunchDetails]. For notification + /// actions that don't show the application/user interface, the + /// [onDidReceiveBackgroundNotificationResponse] callback is invoked on + /// a background isolate. Functions passed to the + /// [onDidReceiveBackgroundNotificationResponse] + /// callback need to be annotated with the `@pragma('vm:entry-point')` + /// annotation to ensure they are not stripped out by the Dart compiler. + void Function(NotificationResponse)? + onDidReceiveBackgroundNotificationResponse, + /// Drawable icon works only in foreground String defaultAndroidForegroundIcon = '@mipmap/ic_launcher', @@ -104,6 +119,17 @@ class FCMConfig extends FCMConfigInterface { /// default action name for linux String linuxActionName = 'fcm_config', + + //Callbacks for Notification + ///Android + final AndroidNotificationDetailsCallback? + androidNotificationDetailsCallback, + + ///IOS,MACOS + final DarwinNotificationDetailsCallback? darwinNotificationDetailsCallback, + + ///Linux + final LinuxNotificationDetailsCallback linuxNotificationDetailsCallback, }) async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(name: name, options: options); @@ -138,6 +164,11 @@ class FCMConfig extends FCMConfigInterface { onRemoteMessage: onMessage, tapSink: _onTapController.sink, linuxActionName: linuxActionName, + onDidReceiveBackgroundNotificationResponse: + onDidReceiveBackgroundNotificationResponse, + androidNotificationDetailsCallback: androidNotificationDetailsCallback, + darwinNotificationDetailsCallback: darwinNotificationDetailsCallback, + linuxNotificationDetailsCallback: linuxNotificationDetailsCallback, ); await _localeNotification!.init(); diff --git a/fcm_config/lib/src/fcm_config_interface.dart b/fcm_config/lib/src/fcm_config_interface.dart index cb7bd5f..1e92636 100644 --- a/fcm_config/lib/src/fcm_config_interface.dart +++ b/fcm_config/lib/src/fcm_config_interface.dart @@ -4,12 +4,31 @@ import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart' show - DarwinNotificationDetails, + AndroidNotificationChannel, AndroidNotificationDetails, - AndroidNotificationChannel; + DarwinNotificationDetails, + LinuxNotificationDetails; import 'details.dart'; +typedef AndroidNotificationDetailsCallback + = Future? Function( + AndroidNotificationDetails androidNotificationDetails, + RemoteMessage remoteMessage, +)?; + +typedef DarwinNotificationDetailsCallback = Future? + Function( + DarwinNotificationDetails darwinNotificationDetails, + RemoteMessage remoteMessage, +)?; + +typedef LinuxNotificationDetailsCallback = Future? + Function( + LinuxNotificationDetails darwinNotificationDetails, + RemoteMessage remoteMessage, +)?; + abstract class FCMConfigInterface { Future getInitialMessage(); Stream get onMessage; @@ -92,7 +111,23 @@ abstract class LocaleNotificationInterface { DarwinNotificationDetails? macOS, }); - Future displayNotificationFrom(RemoteMessage message); + Future displayNotificationFrom( + RemoteMessage message, + + /// callback for mutate [AndroidNotificationDetails] + /// which will get android notification details using [_getAndroidDetails] from [remoteMessage] + /// callback for mutate [AndroidNotificationDetails] + /// which will get android notification details using [_getAndroidDetails] from [remoteMessage] + AndroidNotificationDetailsCallback? onAndroidNotification, + + /// callback for mutate [DarwinNotificationDetails] + /// which will get [Darwin] notification details using [_getDarwinDetails] from [remoteMessage] + DarwinNotificationDetailsCallback? onIosNotification, + + /// callback for mutate [LinuxNotificationDetails] + /// which will get [Linux] notification details using [_getLinuxDetails] from [remoteMessage] + LinuxNotificationDetailsCallback? onLinuxNotification, + ); Future close(); } diff --git a/fcm_config/lib/src/io_notifications_Manager.dart b/fcm_config/lib/src/io_notifications_Manager.dart index e4b8d93..a6b546c 100644 --- a/fcm_config/lib/src/io_notifications_Manager.dart +++ b/fcm_config/lib/src/io_notifications_Manager.dart @@ -39,6 +39,32 @@ class NotificationManager implements LocaleNotificationInterface { /// ios notification sound final bool iosPresentSound; final String linuxActionName; + + /// + /// The [onDidReceiveNotificationResponse] callback is fired when the user + /// selects a notification or notification action that should show the + /// application/user interface. + /// application was running. To handle when a notification launched an + /// application, use [getNotificationAppLaunchDetails]. For notification + /// actions that don't show the application/user interface, the + /// [onDidReceiveBackgroundNotificationResponse] callback is invoked on + /// a background isolate. Functions passed to the + /// [onDidReceiveBackgroundNotificationResponse] + /// callback need to be annotated with the `@pragma('vm:entry-point')` + /// annotation to ensure they are not stripped out by the Dart compiler. + void Function(NotificationResponse)? + onDidReceiveBackgroundNotificationResponse; + + //Callbacks for Notification + ///Android + final AndroidNotificationDetailsCallback? androidNotificationDetailsCallback; + + ///IOS,MACOS + final DarwinNotificationDetailsCallback? darwinNotificationDetailsCallback; + + ///Linux + final LinuxNotificationDetailsCallback? linuxNotificationDetailsCallback; + NotificationManager({ required this.androidNotificationChannel, required this.appAndroidIcon, @@ -49,6 +75,10 @@ class NotificationManager implements LocaleNotificationInterface { required this.iosPresentSound, required this.iosPresentAlert, required this.linuxActionName, + this.onDidReceiveBackgroundNotificationResponse, + this.androidNotificationDetailsCallback, + this.darwinNotificationDetailsCallback, + this.linuxNotificationDetailsCallback, }); @override @@ -83,6 +113,8 @@ class NotificationManager implements LocaleNotificationInterface { await _localeNotification.initialize( initializationSettings, onDidReceiveNotificationResponse: _onPayLoad, + onDidReceiveBackgroundNotificationResponse: + onDidReceiveBackgroundNotificationResponse, ); await _remoteSubscription?.cancel(); //Listen to messages @@ -90,7 +122,12 @@ class NotificationManager implements LocaleNotificationInterface { _remoteSubscription = onRemoteMessage.listen((notification) { if (notification.notification != null && displayInForeground(notification)) { - displayNotificationFrom(notification); + displayNotificationFrom( + notification, + androidNotificationDetailsCallback, + darwinNotificationDetailsCallback, + linuxNotificationDetailsCallback, + ); } }); } @@ -262,6 +299,13 @@ class NotificationManager implements LocaleNotificationInterface { channelShowBadge: android?.channelShowBadge ?? androidNotificationChannel.showBadge, visibility: android?.visibility ?? _getVisibility(notification), + actions: android?.actions, + audioAttributesUsage: + android?.audioAttributesUsage ?? AudioAttributesUsage.notification, + chronometerCountDown: android?.chronometerCountDown ?? false, + colorized: android?.colorized ?? false, + number: android?.number, + when: android?.when, ); } @@ -324,7 +368,12 @@ class NotificationManager implements LocaleNotificationInterface { } @override - Future displayNotificationFrom(RemoteMessage message) async { + Future displayNotificationFrom( + RemoteMessage message, + AndroidNotificationDetailsCallback? onAndroidNotification, + DarwinNotificationDetailsCallback? onIosNotification, + LinuxNotificationDetailsCallback? onLinuxNotification, + ) async { if (message.notification == null) return; String? largeIconPath; @@ -343,11 +392,31 @@ class NotificationManager implements LocaleNotificationInterface { hideExpandedLargeIcon: true, ); } + late AndroidNotificationDetails android; + if (onAndroidNotification != null) { + android = (await onAndroidNotification( + await _getAndroidDetails(message: message), message)) ?? + await _getAndroidDetails(message: message); + } else { + android = await _getAndroidDetails(message: message); + } + late DarwinNotificationDetails darwin; + if (onIosNotification != null) { + darwin = await onIosNotification( + await _getDarwinDetails(message: message), message) ?? + await _getDarwinDetails(message: message); + } else { + darwin = await _getDarwinDetails(message: message); + } + late LinuxNotificationDetails linux; + if (onLinuxNotification != null) { + linux = await onLinuxNotification( + await _getLinuxDetails(message: message), message) ?? + await _getLinuxDetails(message: message); + } else { + linux = await _getLinuxDetails(message: message); + } - var android = await _getAndroidDetails(message: message); - var darwin = await _getDarwinDetails(message: message); - - var linux = await _getLinuxDetails(message: message); var details = NotificationDetails( android: android, iOS: darwin, diff --git a/fcm_config/lib/src/web_notification_manager.dart b/fcm_config/lib/src/web_notification_manager.dart index d5eb402..73282e6 100644 --- a/fcm_config/lib/src/web_notification_manager.dart +++ b/fcm_config/lib/src/web_notification_manager.dart @@ -101,7 +101,11 @@ class NotificationManager implements LocaleNotificationInterface { } @override - Future displayNotificationFrom(RemoteMessage message) { + Future displayNotificationFrom( + RemoteMessage message, + AndroidNotificationDetailsCallback? onAndroidNotification, + DarwinNotificationDetailsCallback? onIosNotification, + LinuxNotificationDetailsCallback? onLinuxNotification) { return displayNotification( id: int.tryParse(message.messageId ?? '') ?? message.hashCode, title: message.notification?.title, diff --git a/fcm_config/pubspec.yaml b/fcm_config/pubspec.yaml index 042210f..5b1ff50 100644 --- a/fcm_config/pubspec.yaml +++ b/fcm_config/pubspec.yaml @@ -1,11 +1,11 @@ name: fcm_config description: This flutter package is made to merge between firebase_messaging package and flutter_local_notifications -version: 3.5.3 +version: 3.6.1 homepage: https://github.com/mo-ah-dawood/fcm_config/tree/master/fcm_config environment: - sdk: ">=2.17.0 <4.0.0" + sdk: ">=3.0.0 <4.0.0" flutter: ">=1.17.0" dependencies: