From a7c834876ab3feada79e12b7ee609441416adb5c Mon Sep 17 00:00:00 2001 From: Gergely Morva Date: Wed, 24 Nov 2021 12:55:58 +0100 Subject: [PATCH] Added support for active notifications on iOS --- .../FlutterLocalNotificationsPlugin.java | 72 +++++++++---------- .../example/lib/main.dart | 43 ++++++----- .../Classes/FlutterLocalNotificationsPlugin.m | 48 +++++++++++++ .../lib/flutter_local_notifications.dart | 2 +- .../flutter_local_notifications_plugin.dart | 4 ++ .../platform_flutter_local_notifications.dart | 48 +++++++------ .../android/active_notification.dart | 25 ------- .../FlutterLocalNotificationsPlugin.swift | 21 ++++++ ...roid_flutter_local_notifications_test.dart | 19 +---- .../ios_flutter_local_notifications_test.dart | 8 +++ ...acos_flutter_local_notifications_test.dart | 8 +++ ...ocal_notifications_platform_interface.dart | 6 ++ .../lib/src/types.dart | 33 +++++++++ 13 files changed, 222 insertions(+), 115 deletions(-) delete mode 100644 flutter_local_notifications/lib/src/platform_specifics/android/active_notification.dart diff --git a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java index 71b715442..807ca0710 100644 --- a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java +++ b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java @@ -99,11 +99,11 @@ public class FlutterLocalNotificationsPlugin "deleteNotificationChannelGroup"; private static final String CREATE_NOTIFICATION_CHANNEL_METHOD = "createNotificationChannel"; private static final String DELETE_NOTIFICATION_CHANNEL_METHOD = "deleteNotificationChannel"; - private static final String GET_ACTIVE_NOTIFICATIONS_METHOD = "getActiveNotifications"; private static final String GET_NOTIFICATION_CHANNELS_METHOD = "getNotificationChannels"; private static final String START_FOREGROUND_SERVICE = "startForegroundService"; private static final String STOP_FOREGROUND_SERVICE = "stopForegroundService"; private static final String PENDING_NOTIFICATION_REQUESTS_METHOD = "pendingNotificationRequests"; + private static final String ACTIVE_NOTIFICATIONS_METHOD = "getActiveNotifications"; private static final String SHOW_METHOD = "show"; private static final String CANCEL_METHOD = "cancel"; private static final String CANCEL_ALL_METHOD = "cancelAll"; @@ -1266,6 +1266,9 @@ public void onMethodCall(MethodCall call, Result result) { case PENDING_NOTIFICATION_REQUESTS_METHOD: pendingNotificationRequests(result); break; + case ACTIVE_NOTIFICATIONS_METHOD: + getActiveNotifications(result); + break; case CREATE_NOTIFICATION_CHANNEL_GROUP_METHOD: createNotificationChannelGroup(call, result); break; @@ -1278,9 +1281,6 @@ public void onMethodCall(MethodCall call, Result result) { case DELETE_NOTIFICATION_CHANNEL_METHOD: deleteNotificationChannel(call, result); break; - case GET_ACTIVE_NOTIFICATIONS_METHOD: - getActiveNotifications(result); - break; case GET_NOTIFICATION_CHANNELS_METHOD: getNotificationChannels(result); break; @@ -1311,6 +1311,38 @@ private void pendingNotificationRequests(Result result) { } result.success(pendingNotifications); } + + private void getActiveNotifications(Result result) { + if (VERSION.SDK_INT < VERSION_CODES.M) { + result.error( + GET_ACTIVE_NOTIFICATIONS_ERROR_CODE, GET_ACTIVE_NOTIFICATIONS_ERROR_MESSAGE, null); + return; + } + NotificationManager notificationManager = + (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE); + try { + StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications(); + List> activeNotificationsPayload = new ArrayList<>(); + + for (StatusBarNotification activeNotification : activeNotifications) { + HashMap activeNotificationPayload = new HashMap<>(); + activeNotificationPayload.put("id", activeNotification.getId()); + Notification notification = activeNotification.getNotification(); + if (VERSION.SDK_INT >= VERSION_CODES.O) { + activeNotificationPayload.put("channelId", notification.getChannelId()); + } + + activeNotificationPayload.put("groupKey", notification.getGroup()); + activeNotificationPayload.put( + "title", notification.extras.getCharSequence("android.title")); + activeNotificationPayload.put("body", notification.extras.getCharSequence("android.text")); + activeNotificationsPayload.add(activeNotificationPayload); + } + result.success(activeNotificationsPayload); + } catch (Throwable e) { + result.error(GET_ACTIVE_NOTIFICATIONS_ERROR_CODE, e.getMessage(), e.getStackTrace()); + } + } private void cancel(MethodCall call, Result result) { Map arguments = call.arguments(); @@ -1586,38 +1618,6 @@ private void deleteNotificationChannel(MethodCall call, Result result) { result.success(null); } - private void getActiveNotifications(Result result) { - if (VERSION.SDK_INT < VERSION_CODES.M) { - result.error( - GET_ACTIVE_NOTIFICATIONS_ERROR_CODE, GET_ACTIVE_NOTIFICATIONS_ERROR_MESSAGE, null); - return; - } - NotificationManager notificationManager = - (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE); - try { - StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications(); - List> activeNotificationsPayload = new ArrayList<>(); - - for (StatusBarNotification activeNotification : activeNotifications) { - HashMap activeNotificationPayload = new HashMap<>(); - activeNotificationPayload.put("id", activeNotification.getId()); - Notification notification = activeNotification.getNotification(); - if (VERSION.SDK_INT >= VERSION_CODES.O) { - activeNotificationPayload.put("channelId", notification.getChannelId()); - } - - activeNotificationPayload.put("groupKey", notification.getGroup()); - activeNotificationPayload.put( - "title", notification.extras.getCharSequence("android.title")); - activeNotificationPayload.put("body", notification.extras.getCharSequence("android.text")); - activeNotificationsPayload.add(activeNotificationPayload); - } - result.success(activeNotificationsPayload); - } catch (Throwable e) { - result.error(GET_ACTIVE_NOTIFICATIONS_ERROR_CODE, e.getMessage(), e.getStackTrace()); - } - } - private void getNotificationChannels(Result result) { try { NotificationManagerCompat notificationManagerCompat = diff --git a/flutter_local_notifications/example/lib/main.dart b/flutter_local_notifications/example/lib/main.dart index a462732ea..d1bfa74bf 100644 --- a/flutter_local_notifications/example/lib/main.dart +++ b/flutter_local_notifications/example/lib/main.dart @@ -358,6 +358,12 @@ class _HomePageState extends State { await _checkPendingNotificationRequests(); }, ), + PaddedElevatedButton( + buttonText: 'Get active notifications', + onPressed: () async { + await _getActiveNotifications(); + }, + ), ], PaddedElevatedButton( buttonText: @@ -600,12 +606,6 @@ class _HomePageState extends State { await _getNotificationChannels(); }, ), - PaddedElevatedButton( - buttonText: 'Get active notifications', - onPressed: () async { - await _getActiveNotifications(); - }, - ), PaddedElevatedButton( buttonText: 'Start foreground service', onPressed: () async { @@ -1927,20 +1927,31 @@ class _HomePageState extends State { } Future _getActiveNotificationsDialogContent() async { - final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); - final AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo; - if (!(androidInfo.version.sdkInt >= 23)) { - return const Text( - '"getActiveNotifications" is available only for Android 6.0 or newer', - ); + if (Platform.isAndroid) { + final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); + final AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo; + if (androidInfo.version.sdkInt < 23) { + return const Text( + '"getActiveNotifications" is available only for Android 6.0 or newer', + ); + } + } else if (Platform.isIOS) { + final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); + final IosDeviceInfo iosInfo = await deviceInfo.iosInfo; + final List fullVersion = iosInfo.systemVersion.split('.'); + if (fullVersion.isNotEmpty) { + final int? version = int.tryParse(fullVersion[0]); + if (version != null && version < 10) { + return const Text( + '"getActiveNotifications" returns an empty list before iOS 10', + ); + } + } } try { final List? activeNotifications = - await flutterLocalNotificationsPlugin - .resolvePlatformSpecificImplementation< - AndroidFlutterLocalNotificationsPlugin>()! - .getActiveNotifications(); + await flutterLocalNotificationsPlugin.getActiveNotifications(); return Column( crossAxisAlignment: CrossAxisAlignment.start, diff --git a/flutter_local_notifications/ios/Classes/FlutterLocalNotificationsPlugin.m b/flutter_local_notifications/ios/Classes/FlutterLocalNotificationsPlugin.m index bd2d230e1..b37494b22 100644 --- a/flutter_local_notifications/ios/Classes/FlutterLocalNotificationsPlugin.m +++ b/flutter_local_notifications/ios/Classes/FlutterLocalNotificationsPlugin.m @@ -24,6 +24,8 @@ @implementation FlutterLocalNotificationsPlugin { NSString *const CANCEL_ALL_METHOD = @"cancelAll"; NSString *const PENDING_NOTIFICATIONS_REQUESTS_METHOD = @"pendingNotificationRequests"; +NSString *const ACTIVE_NOTIFICATIONS_METHOD = + @"getActiveNotifications"; NSString *const GET_NOTIFICATION_APP_LAUNCH_DETAILS_METHOD = @"getNotificationAppLaunchDetails"; NSString *const CHANNEL = @"dexterous.com/flutter/local_notifications"; @@ -77,6 +79,9 @@ @implementation FlutterLocalNotificationsPlugin { NSString *const PAYLOAD = @"payload"; NSString *const NOTIFICATION_LAUNCHED_APP = @"notificationLaunchedApp"; +NSString *const GET_ACTIVE_NOTIFICATIONS_ERROR_CODE = @"GET_ACTIVE_NOTIFICATIONS_ERROR_CODE"; +NSString *const GET_ACTIVE_NOTIFICATIONS_ERROR_MESSAGE = @"iOS version must be 10.0 or newer to use getActiveNotifications"; + typedef NS_ENUM(NSInteger, RepeatInterval) { EveryMinute, Hourly, @@ -166,6 +171,9 @@ - (void)handleMethodCall:(FlutterMethodCall *)call } else if ([PENDING_NOTIFICATIONS_REQUESTS_METHOD isEqualToString:call.method]) { [self pendingNotificationRequests:result]; + } else if ([ACTIVE_NOTIFICATIONS_METHOD + isEqualToString:call.method]) { + [self getActiveNotifications:result]; } else { result(FlutterMethodNotImplemented); } @@ -200,6 +208,35 @@ - (void)pendingUserNotificationRequests:(FlutterResult _Nonnull)result }]; } +- (void)activeUserNotificationRequests:(FlutterResult _Nonnull)result + NS_AVAILABLE_IOS(10.0) { + UNUserNotificationCenter *center = + [UNUserNotificationCenter currentNotificationCenter]; + [center getDeliveredNotificationsWithCompletionHandler:^( + NSArray *_Nonnull notifications) { + NSMutableArray *> + *activeNotifications = + [[NSMutableArray alloc] initWithCapacity:[notifications count]]; + for (UNNotification *notification in notifications) { + NSMutableDictionary *activeNotification = + [[NSMutableDictionary alloc] init]; + activeNotification[ID] = + notification.request.content.userInfo[NOTIFICATION_ID]; + if (notification.request.content.title != nil) { + activeNotification[TITLE] = notification.request.content.title; + } + if (notification.request.content.body != nil) { + activeNotification[BODY] = notification.request.content.body; + } + if (notification.request.content.userInfo[PAYLOAD] != [NSNull null]) { + activeNotification[PAYLOAD] = notification.request.content.userInfo[PAYLOAD]; + } + [activeNotifications addObject:activeNotification]; + } + result(activeNotifications); + }]; +} + - (void)pendingLocalNotificationRequests:(FlutterResult _Nonnull)result { NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications; @@ -234,6 +271,17 @@ - (void)pendingNotificationRequests:(FlutterResult _Nonnull)result { } } +- (void)getActiveNotifications:(FlutterResult _Nonnull)result { + if (@available(iOS 10.0, *)) { + [self activeUserNotificationRequests:result]; + } else { + result([FlutterError + errorWithCode:GET_ACTIVE_NOTIFICATIONS_ERROR_CODE + message:GET_ACTIVE_NOTIFICATIONS_ERROR_MESSAGE + details:[NSNull null]]); + } +} + - (void)initialize:(NSDictionary *_Nonnull)arguments result:(FlutterResult _Nonnull)result { if ([self containsKey:DEFAULT_PRESENT_ALERT forDictionary:arguments]) { diff --git a/flutter_local_notifications/lib/flutter_local_notifications.dart b/flutter_local_notifications/lib/flutter_local_notifications.dart index cf13b6734..3e5588b5e 100644 --- a/flutter_local_notifications/lib/flutter_local_notifications.dart +++ b/flutter_local_notifications/lib/flutter_local_notifications.dart @@ -3,6 +3,7 @@ export 'package:flutter_local_notifications_platform_interface/flutter_local_not show SelectNotificationCallback, PendingNotificationRequest, + ActiveNotification, RepeatInterval, NotificationAppLaunchDetails; @@ -11,7 +12,6 @@ export 'src/initialization_settings.dart'; export 'src/notification_details.dart'; export 'src/platform_flutter_local_notifications.dart' hide MethodChannelFlutterLocalNotificationsPlugin; -export 'src/platform_specifics/android/active_notification.dart'; export 'src/platform_specifics/android/bitmap.dart'; export 'src/platform_specifics/android/enums.dart' hide AndroidBitmapSource, AndroidIconSource, AndroidNotificationSoundSource; diff --git a/flutter_local_notifications/lib/src/flutter_local_notifications_plugin.dart b/flutter_local_notifications/lib/src/flutter_local_notifications_plugin.dart index fed4725ef..fa26fe7c0 100644 --- a/flutter_local_notifications/lib/src/flutter_local_notifications_plugin.dart +++ b/flutter_local_notifications/lib/src/flutter_local_notifications_plugin.dart @@ -475,4 +475,8 @@ class FlutterLocalNotificationsPlugin { /// Returns a list of notifications pending to be delivered/shown. Future> pendingNotificationRequests() => FlutterLocalNotificationsPlatform.instance.pendingNotificationRequests(); + + /// Returns a list of notifications that are already delivered/shown. + Future> getActiveNotifications() => + FlutterLocalNotificationsPlatform.instance.getActiveNotifications(); } diff --git a/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart b/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart index b481265a2..5fd9c0ba8 100644 --- a/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart +++ b/flutter_local_notifications/lib/src/platform_flutter_local_notifications.dart @@ -8,7 +8,6 @@ import 'package:flutter_local_notifications_platform_interface/flutter_local_not import 'package:timezone/timezone.dart'; import 'helpers.dart'; -import 'platform_specifics/android/active_notification.dart'; import 'platform_specifics/android/enums.dart'; import 'platform_specifics/android/initialization_settings.dart'; import 'platform_specifics/android/method_channel_mappers.dart'; @@ -65,6 +64,33 @@ class MethodChannelFlutterLocalNotificationsPlugin .toList() ?? []; } + + /// Returns the list of active notifications shown by the application that + /// haven't been dismissed/removed. + /// + /// This method is only applicable to Android 6.0 or newer and will throw an + /// [PlatformException] when called on a device with an incompatible Android + /// version. + /// + /// This method is only applicable to iOS 10.0 or newer and will return an + /// empty list when called on a device with an incompatible iOS version. + @override + Future> getActiveNotifications() async { + final List>? activeNotifications = + await _channel.invokeListMethod('getActiveNotifications'); + return activeNotifications + // ignore: always_specify_types + ?.map((p) => ActiveNotification( + id: p['id'], + channelId: p['channelId'], + groupKey: p['groupKey'], + title: p['title'], + body: p['body'], + payload: p['payload'], + )) + .toList() ?? + []; + } } /// Android implementation of the local notifications plugin. @@ -387,26 +413,6 @@ class AndroidFlutterLocalNotificationsPlugin Future deleteNotificationChannel(String channelId) => _channel.invokeMethod('deleteNotificationChannel', channelId); - /// Returns the list of active notifications shown by the application that - /// haven't been dismissed/removed. - /// - /// This method is only applicable to Android 6.0 or newer and will throw an - /// [PlatformException] when called on a device with an incompatible Android - /// version. - Future?> getActiveNotifications() async { - final List>? activeNotifications = - await _channel.invokeListMethod('getActiveNotifications'); - return activeNotifications - // ignore: always_specify_types - ?.map((a) => ActiveNotification( - a['id'], - a['channelId'], - a['title'], - a['body'], - )) - .toList(); - } - /// Returns the list of all notification channels. /// /// This method is only applicable on Android 8.0 or newer. On older versions, diff --git a/flutter_local_notifications/lib/src/platform_specifics/android/active_notification.dart b/flutter_local_notifications/lib/src/platform_specifics/android/active_notification.dart deleted file mode 100644 index bd2b2f869..000000000 --- a/flutter_local_notifications/lib/src/platform_specifics/android/active_notification.dart +++ /dev/null @@ -1,25 +0,0 @@ -/// Contains details on active Android notification that is currently displayed -/// within the status bar. -class ActiveNotification { - /// Constructs an instance of [ActiveNotification]. - const ActiveNotification( - this.id, - this.channelId, - this.title, - this.body, - ); - - /// The notification's id. - final int id; - - /// The notification channel's id. - /// - /// Returned only on Android 8.0 or newer. - final String? channelId; - - /// The notification's title. - final String? title; - - /// The notification's content. - final String? body; -} diff --git a/flutter_local_notifications/macos/Classes/FlutterLocalNotificationsPlugin.swift b/flutter_local_notifications/macos/Classes/FlutterLocalNotificationsPlugin.swift index aa86d3826..b1d3adaa6 100644 --- a/flutter_local_notifications/macos/Classes/FlutterLocalNotificationsPlugin.swift +++ b/flutter_local_notifications/macos/Classes/FlutterLocalNotificationsPlugin.swift @@ -134,6 +134,8 @@ public class FlutterLocalNotificationsPlugin: NSObject, FlutterPlugin, UNUserNot cancelAll(result) case "pendingNotificationRequests": pendingNotificationRequests(result) + case "getActiveNotifications": + getActiveNotifications(result) case "show": show(call, result) case "zonedSchedule": @@ -241,6 +243,25 @@ public class FlutterLocalNotificationsPlugin: NSObject, FlutterPlugin, UNUserNot } } + func getActiveNotifications(_ result: @escaping FlutterResult) { + if #available(OSX 10.14, *) { + UNUserNotificationCenter.current().getDeliveredNotifications { (requests) in + var requestDictionaries: [[String: Any?]] = [] + for request in requests { + requestDictionaries.append([MethodCallArguments.id: Int(request.request.identifier) as Any, MethodCallArguments.title: request.request.content.title, MethodCallArguments.body: request.request.content.body, MethodCallArguments.payload: request.request.content.userInfo[MethodCallArguments.payload]]) + } + result(requestDictionaries) + } + } else { + var requestDictionaries: [[String: Any?]] = [] + let center = NSUserNotificationCenter.default + for scheduledNotification in center.scheduledNotifications { + requestDictionaries.append([MethodCallArguments.id: Int(scheduledNotification.identifier!) as Any, MethodCallArguments.title: scheduledNotification.title, MethodCallArguments.body: scheduledNotification.informativeText, MethodCallArguments.payload: scheduledNotification.userInfo![MethodCallArguments.payload]]) + } + result(requestDictionaries) + } + } + func show(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { if #available(OSX 10.14, *) { do { diff --git a/flutter_local_notifications/test/android_flutter_local_notifications_test.dart b/flutter_local_notifications/test/android_flutter_local_notifications_test.dart index 8a8189502..0d00b6857 100644 --- a/flutter_local_notifications/test/android_flutter_local_notifications_test.dart +++ b/flutter_local_notifications/test/android_flutter_local_notifications_test.dart @@ -28,10 +28,10 @@ void main() { log.add(methodCall); if (methodCall.method == 'pendingNotificationRequests') { return >[]; - } else if (methodCall.method == 'getNotificationAppLaunchDetails') { - return null; } else if (methodCall.method == 'getActiveNotifications') { return >[]; + } else if (methodCall.method == 'getNotificationAppLaunchDetails') { + return null; } }); }); @@ -2085,15 +2085,6 @@ void main() { ]); }); - test('getActiveNotifications', () async { - await flutterLocalNotificationsPlugin - .resolvePlatformSpecificImplementation< - AndroidFlutterLocalNotificationsPlugin>()! - .getActiveNotifications(); - expect(log, - [isMethodCall('getActiveNotifications', arguments: null)]); - }); - test('cancel', () async { await flutterLocalNotificationsPlugin.cancel(1); expect(log, [ @@ -2127,11 +2118,7 @@ void main() { }); test('getActiveNotifications', () async { - debugDefaultTargetPlatformOverride = TargetPlatform.android; - await flutterLocalNotificationsPlugin - .resolvePlatformSpecificImplementation< - AndroidFlutterLocalNotificationsPlugin>()! - .getActiveNotifications(); + await flutterLocalNotificationsPlugin.getActiveNotifications(); expect(log, [isMethodCall('getActiveNotifications', arguments: null)]); }); diff --git a/flutter_local_notifications/test/ios_flutter_local_notifications_test.dart b/flutter_local_notifications/test/ios_flutter_local_notifications_test.dart index f6b70a8fb..346b9c1b0 100644 --- a/flutter_local_notifications/test/ios_flutter_local_notifications_test.dart +++ b/flutter_local_notifications/test/ios_flutter_local_notifications_test.dart @@ -25,6 +25,8 @@ void main() { log.add(methodCall); if (methodCall.method == 'pendingNotificationRequests') { return ?>[]; + } else if (methodCall.method == 'getActiveNotifications') { + return ?>[]; } else if (methodCall.method == 'getNotificationAppLaunchDetails') { return null; } @@ -440,6 +442,12 @@ void main() { ]); }); + test('getActiveNotifications', () async { + await flutterLocalNotificationsPlugin.getActiveNotifications(); + expect(log, + [isMethodCall('getActiveNotifications', arguments: null)]); + }); + test('getNotificationAppLaunchDetails', () async { await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails(); expect(log, [ diff --git a/flutter_local_notifications/test/macos_flutter_local_notifications_test.dart b/flutter_local_notifications/test/macos_flutter_local_notifications_test.dart index 18b100a29..a937fffec 100644 --- a/flutter_local_notifications/test/macos_flutter_local_notifications_test.dart +++ b/flutter_local_notifications/test/macos_flutter_local_notifications_test.dart @@ -24,6 +24,8 @@ void main() { log.add(methodCall); if (methodCall.method == 'pendingNotificationRequests') { return ?>[]; + } else if (methodCall.method == 'getActiveNotifications') { + return ?>[]; } else if (methodCall.method == 'getNotificationAppLaunchDetails') { return null; } @@ -435,6 +437,12 @@ void main() { ]); }); + test('getActiveNotifications', () async { + await flutterLocalNotificationsPlugin.getActiveNotifications(); + expect(log, + [isMethodCall('getActiveNotifications', arguments: null)]); + }); + test('getNotificationAppLaunchDetails', () async { await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails(); expect(log, [ diff --git a/flutter_local_notifications_platform_interface/lib/flutter_local_notifications_platform_interface.dart b/flutter_local_notifications_platform_interface/lib/flutter_local_notifications_platform_interface.dart index 6935062ee..80c3d0612 100644 --- a/flutter_local_notifications_platform_interface/lib/flutter_local_notifications_platform_interface.dart +++ b/flutter_local_notifications_platform_interface/lib/flutter_local_notifications_platform_interface.dart @@ -70,4 +70,10 @@ abstract class FlutterLocalNotificationsPlatform extends PlatformInterface { throw UnimplementedError( 'pendingNotificationRequest() has not been implemented'); } + + /// Returns a list of notifications already delivered + Future> getActiveNotifications() { + throw UnimplementedError( + 'getActiveNotifications() has not been implemented'); + } } diff --git a/flutter_local_notifications_platform_interface/lib/src/types.dart b/flutter_local_notifications_platform_interface/lib/src/types.dart index a4018dc7f..45ba3070b 100644 --- a/flutter_local_notifications_platform_interface/lib/src/types.dart +++ b/flutter_local_notifications_platform_interface/lib/src/types.dart @@ -31,3 +31,36 @@ class PendingNotificationRequest { /// The notification's payload. final String? payload; } + +/// Details of an active notification. +class ActiveNotification { + /// Constructs an instance of [ActiveNotification]. + const ActiveNotification({ + required this.id, + this.groupKey, + this.channelId, + this.title, + this.body, + this.payload, + }); + + /// The notification's id. + final int id; + + /// The notification's channel id. + /// + /// Returned only on Android 8.0 or newer. + final String? channelId; + + /// The notification's group. + final String? groupKey; + + /// The notification's title. + final String? title; + + /// The notification's body. + final String? body; + + /// The notification's payload. + final String? payload; +}