Skip to content

Commit 108c1eb

Browse files
ingridwangSaadnajmi
authored andcommitted
Migrate cancel local notification methods to UserNotifications (facebook#40949)
Summary: Pull Request resolved: facebook#40949 [Internal] Migrating cancelLocalNotifications and cancelAllLocalNotifications off of deprecated UILocalNotification methods Reviewed By: philIip, cipolleschi Differential Revision: D50275540 fbshipit-source-id: 3728e53b410322bbb0e269ac1407d197b8d5979f
1 parent 3e16f99 commit 108c1eb

1 file changed

Lines changed: 31 additions & 32 deletions

File tree

packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -540,44 +540,43 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
540540

541541
RCT_EXPORT_METHOD(cancelAllLocalNotifications)
542542
{
543-
#if !TARGET_OS_OSX // [macOS]
544-
[RCTSharedApplication() cancelAllLocalNotifications];
545-
#else // [macOS
546-
for (NSUserNotification *notif in [NSUserNotificationCenter defaultUserNotificationCenter].scheduledNotifications) {
547-
[[NSUserNotificationCenter defaultUserNotificationCenter] removeScheduledNotification:notif];
548-
}
549-
#endif // macOS]
543+
[[UNUserNotificationCenter currentNotificationCenter]
544+
getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> *requests) {
545+
NSMutableArray<NSString *> *notificationIdentifiersToCancel = [NSMutableArray new];
546+
for (UNNotificationRequest *request in requests) {
547+
[notificationIdentifiersToCancel addObject:request.identifier];
548+
}
549+
[[UNUserNotificationCenter currentNotificationCenter]
550+
removePendingNotificationRequestsWithIdentifiers:notificationIdentifiersToCancel];
551+
}];
550552
}
551553

552554
RCT_EXPORT_METHOD(cancelLocalNotifications : (NSDictionary<NSString *, id> *)userInfo)
553555
{
554-
#if !TARGET_OS_OSX // [macOS]
555-
for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) {
556-
#else // [macOS
557-
for (NSUserNotification *notification in [NSUserNotificationCenter defaultUserNotificationCenter].scheduledNotifications) {
558-
#endif // macOS]
559-
__block BOOL matchesAll = YES;
560-
NSDictionary<NSString *, id> *notificationInfo = notification.userInfo;
561-
// Note: we do this with a loop instead of just `isEqualToDictionary:`
562-
// because we only require that all specified userInfo values match the
563-
// notificationInfo values - notificationInfo may contain additional values
564-
// which we don't care about.
565-
[userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
566-
if (![notificationInfo[key] isEqual:obj]) {
567-
matchesAll = NO;
568-
*stop = YES;
556+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
557+
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> *_Nonnull requests) {
558+
NSMutableArray<NSString *> *notificationIdentifiersToCancel = [NSMutableArray new];
559+
for (UNNotificationRequest *request in requests) {
560+
NSDictionary<NSString *, id> *notificationInfo = request.content.userInfo;
561+
// Note: we do this with a loop instead of just `isEqualToDictionary:`
562+
// because we only require that all specified userInfo values match the
563+
// notificationInfo values - notificationInfo may contain additional values
564+
// which we don't care about.
565+
__block BOOL shouldCancel = YES;
566+
[userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
567+
if (![notificationInfo[key] isEqual:obj]) {
568+
shouldCancel = NO;
569+
*stop = YES;
570+
}
571+
}];
572+
573+
if (shouldCancel) {
574+
[notificationIdentifiersToCancel addObject:request.identifier];
569575
}
570-
}];
571-
#if !TARGET_OS_OSX // [macOS]
572-
if (matchesAll) {
573-
[RCTSharedApplication() cancelLocalNotification:notification];
574-
}
575-
#else // [macOS
576-
if ([notification.identifier isEqualToString:userInfo[@"identifier"]] || matchesAll) {
577-
[[NSUserNotificationCenter defaultUserNotificationCenter] removeScheduledNotification:notification];
578576
}
579-
#endif // macOS]
580-
}
577+
578+
[center removePendingNotificationRequestsWithIdentifiers:notificationIdentifiersToCancel];
579+
}];
581580
}
582581

583582
RCT_EXPORT_METHOD(getInitialNotification

0 commit comments

Comments
 (0)