Skip to content

Commit

Permalink
[flutter_local_notifications] suppress deprecation warnings on iOS re…
Browse files Browse the repository at this point in the history
…lating to old notification APIs (#1740)

* suppress deprecation warnings on iOS relating to old notification APIs

* added changelog entry on suppressing iOS deprecation warnings
  • Loading branch information
MaikuB authored Oct 8, 2022
1 parent 6d3e1ab commit 5e89614
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions flutter_local_notifications/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# [12.0.1]

* [Android][iOS] fixed issue [1721](https://github.com/MaikuB/flutter_local_notifications/issues/1721) where a crash occurs upon tapping on a notification action fbut the `onDidReceiveBackgroundNotificationResponse` optional callback hasn't been specified.
* [iOS] suppressed deprecation warnings where plugin was Apple's old notification APIs to support older iOS devices

# [12.0.0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,16 @@ - (void)activeUserNotificationRequests:(FlutterResult _Nonnull)result
}

- (void)pendingLocalNotificationRequests:(FlutterResult _Nonnull)result {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
NSArray *notifications =
[UIApplication sharedApplication].scheduledLocalNotifications;
NSMutableArray<NSDictionary<NSString *, NSObject *> *>
*pendingNotificationRequests =
[[NSMutableArray alloc] initWithCapacity:[notifications count]];
for (int i = 0; i < [notifications count]; i++) {
UILocalNotification *localNotification = [notifications objectAtIndex:i];
#pragma clang diagnostic pop
NSMutableDictionary *pendingNotificationRequest =
[[NSMutableDictionary alloc] init];
pendingNotificationRequest[ID] =
Expand Down Expand Up @@ -489,6 +492,8 @@ - (void)requestPermissionsImpl:(bool)soundPermission
result(@(granted));
}];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIUserNotificationType notificationTypes = 0;
if (soundPermission) {
notificationTypes |= UIUserNotificationTypeSound;
Expand All @@ -504,13 +509,17 @@ - (void)requestPermissionsImpl:(bool)soundPermission
categories:nil];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
#pragma clang diagnostic pop
result(@YES);
}
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (UILocalNotification *)buildStandardUILocalNotification:
(NSDictionary *)arguments {
UILocalNotification *notification = [[UILocalNotification alloc] init];
#pragma clang diagnostic pop
if ([self containsKey:BODY forDictionary:arguments]) {
notification.alertBody = arguments[BODY];
}
Expand Down Expand Up @@ -550,7 +559,10 @@ - (UILocalNotification *)buildStandardUILocalNotification:
}

if (presentSound && notification.soundName == nil) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
notification.soundName = UILocalNotificationDefaultSoundName;
#pragma clang diagnostic pop
}

notification.userInfo = [self buildUserDict:arguments[ID]
Expand All @@ -576,10 +588,13 @@ - (void)show:(NSDictionary *_Nonnull)arguments
result:result
trigger:nil];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *notification =
[self buildStandardUILocalNotification:arguments];
[[UIApplication sharedApplication]
presentLocalNotificationNow:notification];
#pragma clang diagnostic pop
result(nil);
}
}
Expand All @@ -597,8 +612,11 @@ - (void)zonedSchedule:(NSDictionary *_Nonnull)arguments
trigger:trigger];

} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *notification =
[self buildStandardUILocalNotification:arguments];
#pragma clang diagnostic pop
NSString *scheduledDateTime = arguments[SCHEDULED_DATE_TIME];
NSString *timeZoneName = arguments[TIME_ZONE_NAME];
NSNumber *matchDateComponents = arguments[MATCH_DATE_TIME_COMPONENTS];
Expand Down Expand Up @@ -630,7 +648,10 @@ - (void)zonedSchedule:(NSDictionary *_Nonnull)arguments
notification.repeatInterval = NSCalendarUnitYear;
}
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
#pragma clang diagnostic pop
result(nil);
}
}
Expand Down Expand Up @@ -658,11 +679,17 @@ - (void)schedule:(NSDictionary *_Nonnull)arguments
result:result
trigger:trigger];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *notification =
[self buildStandardUILocalNotification:arguments];
#pragma clang diagnostic pop
notification.fireDate = [NSDate
dateWithTimeIntervalSince1970:[secondsSinceEpoch longLongValue]];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
#pragma clang diagnostic pop
result(nil);
}
}
Expand All @@ -679,8 +706,11 @@ - (void)periodicallyShow:(NSDictionary *_Nonnull)arguments
result:result
trigger:trigger];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *notification =
[self buildStandardUILocalNotification:arguments];
#pragma clang diagnostic pop
NSTimeInterval timeInterval = 0;
switch ([arguments[REPEAT_INTERVAL] integerValue]) {
case EveryMinute:
Expand All @@ -701,7 +731,10 @@ - (void)periodicallyShow:(NSDictionary *_Nonnull)arguments
break;
}
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
#pragma clang diagnostic pop
result(nil);
}
}
Expand All @@ -727,16 +760,22 @@ - (void)showDailyAtTime:(NSDictionary *_Nonnull)arguments
result:result
trigger:trigger];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *notification =
[self buildStandardUILocalNotification:arguments];
#pragma clang diagnostic pop
notification.repeatInterval = NSCalendarUnitDay;
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setHour:[hourComponent integerValue]];
[dateComponents setMinute:[minutesComponent integerValue]];
[dateComponents setSecond:[secondsComponent integerValue]];
NSCalendar *calendar = [NSCalendar currentCalendar];
notification.fireDate = [calendar dateFromComponents:dateComponents];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
#pragma clang diagnostic pop
result(nil);
}
}
Expand Down Expand Up @@ -764,8 +803,11 @@ - (void)showWeeklyAtDayAndTime:(NSDictionary *_Nonnull)arguments
result:result
trigger:trigger];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *notification =
[self buildStandardUILocalNotification:arguments];
#pragma clang diagnostic pop
notification.repeatInterval = NSCalendarUnitWeekOfYear;
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setHour:[hourComponent integerValue]];
Expand All @@ -774,7 +816,10 @@ - (void)showWeeklyAtDayAndTime:(NSDictionary *_Nonnull)arguments
[dateComponents setWeekday:[dayOfWeekComponent integerValue]];
NSCalendar *calendar = [NSCalendar currentCalendar];
notification.fireDate = [calendar dateFromComponents:dateComponents];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
#pragma clang diagnostic pop
result(nil);
}
}
Expand All @@ -788,15 +833,24 @@ - (void)cancel:(NSNumber *)id result:(FlutterResult _Nonnull)result {
[center removePendingNotificationRequestsWithIdentifiers:idsToRemove];
[center removeDeliveredNotificationsWithIdentifiers:idsToRemove];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
NSArray *notifications =
[UIApplication sharedApplication].scheduledLocalNotifications;
#pragma clang diagnostic pop
for (int i = 0; i < [notifications count]; i++) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *localNotification = [notifications objectAtIndex:i];
#pragma clang diagnostic pop
NSNumber *userInfoNotificationId =
localNotification.userInfo[NOTIFICATION_ID];
if ([userInfoNotificationId longValue] == [id longValue]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication]
cancelLocalNotification:localNotification];
#pragma clang diagnostic pop
break;
}
}
Expand All @@ -811,7 +865,10 @@ - (void)cancelAll:(FlutterResult _Nonnull)result {
[center removeAllPendingNotificationRequests];
[center removeAllDeliveredNotifications];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] cancelAllLocalNotifications];
#pragma clang diagnostic pop
}
result(nil);
}
Expand Down Expand Up @@ -1177,9 +1234,12 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions != nil) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UILocalNotification *launchNotification =
(UILocalNotification *)[launchOptions
objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
#pragma clang diagnostic pop
_launchingAppFromNotification =
launchNotification != nil &&
[self isAFlutterLocalNotification:launchNotification.userInfo];
Expand All @@ -1197,8 +1257,12 @@ - (BOOL)application:(UIApplication *)application
return YES;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
#pragma clang diagnostic pop
if (@available(iOS 10.0, *)) {
return;
}
Expand Down

0 comments on commit 5e89614

Please sign in to comment.