Skip to content

Commit 17a8587

Browse files
authored
[flutter_local_notifications] Change plugin to use platform interface (#444)
* progress on using platform interface * move more code to use platform interface * connect onSelectNotification callback * shuffle more types, periodicallyShow and showDailyAtTime * reference new version of platform interface, move more methods to platform specific implementations of plugin * fix periodcallyShow to pass platform specifics * hide method channel implmentation of plugin, update tests * remove unused imports * add more API docs * reorganise methods, update changelog and pubspec for new release * update changelog to mention change on how iOS launch notifications are processed * remove notification channel methods from Android implementation
1 parent 76d874d commit 17a8587

16 files changed

+627
-453
lines changed

flutter_local_notifications/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# [1.1.0]
2+
* Updated plugin to make use of `flutter_local_notifications_platform_interface` version 1.0.1. This allows for platform-specific
3+
implementations of the platform interface to now be accessible.
4+
* **BREAKING CHANGE** Plugin callbacks are no longer publicly accessible
5+
* **BREAKING CHANGE** [iOS] Local notifications that launched the app should now only be processed by the plugin if they were created by the plugin.
6+
17
# [1.0.0]
28
* **BREAKING CHANGE** [iOS] Added checks to ensure callbacks are only invoked for notifications originating from the plugin to improve compatibility with other notification plugins.
39
* [Android] Bump Gradle plugin to 3.5.3

flutter_local_notifications/analysis_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include: package:pedantic/analysis_options.yaml
1+
include: package:pedantic/analysis_options.1.8.0.yaml
22

33
linter:
44
rules:

flutter_local_notifications/ios/Classes/FlutterLocalNotificationsPlugin.m

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
@implementation FlutterLocalNotificationsPlugin{
88
FlutterMethodChannel* _channel;
9-
bool displayAlert;
10-
bool playSound;
11-
bool updateBadge;
12-
bool initialized;
13-
bool launchingAppFromNotification;
14-
NSUserDefaults *persistentState;
9+
bool _displayAlert;
10+
bool _playSound;
11+
bool _updateBadge;
12+
bool _initialized;
13+
bool _launchingAppFromNotification;
14+
NSUserDefaults *_persistentState;
1515
NSObject<FlutterPluginRegistrar> *_registrar;
16-
NSString *launchPayload;
17-
UILocalNotification *launchNotification;
16+
NSString *_launchPayload;
17+
UILocalNotification *_launchNotification;
1818
}
1919

2020
NSString *const INITIALIZE_METHOD = @"initialize";
@@ -85,7 +85,7 @@ - (instancetype)initWithChannel:(FlutterMethodChannel *)channel registrar:(NSObj
8585
if (self) {
8686
_channel = channel;
8787
_registrar = registrar;
88-
persistentState = [NSUserDefaults standardUserDefaults];
88+
_persistentState = [NSUserDefaults standardUserDefaults];
8989
}
9090

9191
return self;
@@ -137,13 +137,13 @@ - (void)pendingNotificationRequests:(FlutterResult _Nonnull)result {
137137
- (void)initialize:(FlutterMethodCall * _Nonnull)call result:(FlutterResult _Nonnull)result {
138138
NSDictionary *arguments = [call arguments];
139139
if(arguments[DEFAULT_PRESENT_ALERT] != [NSNull null]) {
140-
displayAlert = [[arguments objectForKey:DEFAULT_PRESENT_ALERT] boolValue];
140+
_displayAlert = [[arguments objectForKey:DEFAULT_PRESENT_ALERT] boolValue];
141141
}
142142
if(arguments[DEFAULT_PRESENT_SOUND] != [NSNull null]) {
143-
playSound = [[arguments objectForKey:DEFAULT_PRESENT_SOUND] boolValue];
143+
_playSound = [[arguments objectForKey:DEFAULT_PRESENT_SOUND] boolValue];
144144
}
145145
if(arguments[DEFAULT_PRESENT_BADGE] != [NSNull null]) {
146-
updateBadge = [[arguments objectForKey:DEFAULT_PRESENT_BADGE] boolValue];
146+
_updateBadge = [[arguments objectForKey:DEFAULT_PRESENT_BADGE] boolValue];
147147
}
148148
bool requestedSoundPermission = false;
149149
bool requestedAlertPermission = false;
@@ -172,8 +172,8 @@ - (void)initialize:(FlutterMethodCall * _Nonnull)call result:(FlutterResult _Non
172172
authorizationOptions += UNAuthorizationOptionBadge;
173173
}
174174
[center requestAuthorizationWithOptions:(authorizationOptions) completionHandler:^(BOOL granted, NSError * _Nullable error) {
175-
if(self->launchPayload != nil) {
176-
[self handleSelectNotification:self->launchPayload];
175+
if(self->_launchPayload != nil) {
176+
[self handleSelectNotification:self->_launchPayload];
177177
}
178178
result(@(granted));
179179
}];
@@ -190,13 +190,13 @@ - (void)initialize:(FlutterMethodCall * _Nonnull)call result:(FlutterResult _Non
190190
}
191191
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
192192
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
193-
if(launchNotification != nil && [self isAFlutterLocalNotification:launchNotification.userInfo]) {
194-
NSString *payload = launchNotification.userInfo[PAYLOAD];
193+
if(_launchNotification != nil && [self isAFlutterLocalNotification:_launchNotification.userInfo]) {
194+
NSString *payload = _launchNotification.userInfo[PAYLOAD];
195195
[self handleSelectNotification:payload];
196196
}
197197
result(@YES);
198198
}
199-
initialized = true;
199+
_initialized = true;
200200
}
201201

202202
- (void)showNotification:(FlutterMethodCall * _Nonnull)call result:(FlutterResult _Nonnull)result {
@@ -209,9 +209,9 @@ - (void)showNotification:(FlutterMethodCall * _Nonnull)call result:(FlutterResul
209209
notificationDetails.body = call.arguments[BODY];
210210
}
211211
notificationDetails.payload = call.arguments[PAYLOAD];
212-
notificationDetails.presentAlert = displayAlert;
213-
notificationDetails.presentSound = playSound;
214-
notificationDetails.presentBadge = updateBadge;
212+
notificationDetails.presentAlert = _displayAlert;
213+
notificationDetails.presentSound = _playSound;
214+
notificationDetails.presentBadge = _updateBadge;
215215
if(call.arguments[PLATFORM_SPECIFICS] != [NSNull null]) {
216216
NSDictionary *platformSpecifics = call.arguments[PLATFORM_SPECIFICS];
217217

@@ -299,12 +299,12 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
299299
[self cancelAllNotifications:result];
300300
} else if([GET_NOTIFICATION_APP_LAUNCH_DETAILS_METHOD isEqualToString:call.method]) {
301301
NSString *payload;
302-
if(launchNotification != nil) {
303-
payload = launchNotification.userInfo[PAYLOAD];
302+
if(_launchNotification != nil) {
303+
payload = _launchNotification.userInfo[PAYLOAD];
304304
} else {
305-
payload = launchPayload;
305+
payload = _launchPayload;
306306
}
307-
NSDictionary *notificationAppLaunchDetails = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:launchingAppFromNotification], NOTIFICATION_LAUNCHED_APP, payload, PAYLOAD, nil];
307+
NSDictionary *notificationAppLaunchDetails = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:_launchingAppFromNotification], NOTIFICATION_LAUNCHED_APP, payload, PAYLOAD, nil];
308308
result(notificationAppLaunchDetails);
309309
} else if([PENDING_NOTIFICATIONS_REQUESTS_METHOD isEqualToString:call.method]) {
310310
[self pendingNotificationRequests:result];
@@ -476,11 +476,11 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNot
476476
presentationOptions |= UNNotificationPresentationOptionBadge;
477477
}
478478

479-
480479
completionHandler(presentationOptions);
481480
}
482481

483482
- (void)handleSelectNotification:(NSString *)payload {
483+
NSLog(@"handleSelectNotification");
484484
[_channel invokeMethod:@"selectNotification" arguments:payload];
485485
}
486486

@@ -489,19 +489,22 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
489489
withCompletionHandler:(void (^)(void))completionHandler NS_AVAILABLE_IOS(10.0) {
490490
if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier] && [self isAFlutterLocalNotification:response.notification.request.content.userInfo]) {
491491
NSString *payload = (NSString *) response.notification.request.content.userInfo[PAYLOAD];
492-
if(initialized) {
492+
if(_initialized) {
493493
[self handleSelectNotification:payload];
494494
} else {
495-
launchPayload = payload;
496-
launchingAppFromNotification = true;
495+
_launchPayload = payload;
496+
_launchingAppFromNotification = true;
497497
}
498498
}
499499
}
500500
- (BOOL)application:(UIApplication *)application
501501
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
502502
if (launchOptions != nil) {
503-
launchNotification = (UILocalNotification *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
504-
launchingAppFromNotification = launchNotification != nil;
503+
UILocalNotification *launchNotification = (UILocalNotification *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
504+
_launchingAppFromNotification = launchNotification != nil && [self isAFlutterLocalNotification:launchNotification.userInfo];
505+
if(_launchingAppFromNotification) {
506+
_launchNotification = launchNotification;
507+
}
505508
}
506509

507510
return YES;

flutter_local_notifications/lib/flutter_local_notifications.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
library flutter_local_notifications;
1+
export 'package:flutter_local_notifications_platform_interface/flutter_local_notifications_platform_interface.dart'
2+
show PendingNotificationRequest, RepeatInterval;
23

34
export 'src/platform_specifics/android/styles/style_information.dart';
45
export 'src/platform_specifics/android/styles/default_style_information.dart';
@@ -15,6 +16,8 @@ export 'src/platform_specifics/ios/initialization_settings.dart';
1516
export 'src/platform_specifics/ios/notification_details.dart';
1617
export 'src/notification_details.dart';
1718
export 'src/initialization_settings.dart';
18-
export 'src/flutter_local_notifications.dart';
19-
export 'src/notification_app_launch_details.dart';
20-
export 'src/pending_notification_request.dart';
19+
export 'src/flutter_local_notifications_plugin.dart';
20+
export 'src/platform_flutter_local_notifications.dart'
21+
hide MethodChannelFlutterLocalNotificationsPlugin;
22+
export 'src/types.dart';
23+
export 'src/typedefs.dart';

0 commit comments

Comments
 (0)