From 62d2f2bdf148024d200e00cffc6fa491e0d88e2e Mon Sep 17 00:00:00 2001 From: Ingrid Wang Date: Tue, 17 Oct 2023 11:59:28 -0700 Subject: [PATCH] Migrate cancel local notification methods to UserNotifications Differential Revision: D50275540 fbshipit-source-id: f5b970fb82d7d7d7df65e379967fe9f422a3f8bc --- .../RCTPushNotificationManager.mm | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm index fce0ad95d79595..6237cfdaec384f 100644 --- a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm +++ b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm @@ -434,28 +434,43 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification RCT_EXPORT_METHOD(cancelAllLocalNotifications) { - [RCTSharedApplication() cancelAllLocalNotifications]; + [[UNUserNotificationCenter currentNotificationCenter] + getPendingNotificationRequestsWithCompletionHandler:^(NSArray *requests) { + NSMutableArray *notificationIdentifiersToCancel = [NSMutableArray new]; + for (UNNotificationRequest *request in requests) { + [notificationIdentifiersToCancel addObject:request.identifier]; + } + [[UNUserNotificationCenter currentNotificationCenter] + removePendingNotificationRequestsWithIdentifiers:notificationIdentifiersToCancel]; + }]; } RCT_EXPORT_METHOD(cancelLocalNotifications : (NSDictionary *)userInfo) { - for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) { - __block BOOL matchesAll = YES; - NSDictionary *notificationInfo = notification.userInfo; - // Note: we do this with a loop instead of just `isEqualToDictionary:` - // because we only require that all specified userInfo values match the - // notificationInfo values - notificationInfo may contain additional values - // which we don't care about. - [userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { - if (![notificationInfo[key] isEqual:obj]) { - matchesAll = NO; - *stop = YES; + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray *_Nonnull requests) { + NSMutableArray *notificationIdentifiersToCancel = [NSMutableArray new]; + for (UNNotificationRequest *request in requests) { + NSDictionary *notificationInfo = request.content.userInfo; + // Note: we do this with a loop instead of just `isEqualToDictionary:` + // because we only require that all specified userInfo values match the + // notificationInfo values - notificationInfo may contain additional values + // which we don't care about. + __block BOOL shouldCancel = YES; + [userInfo enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { + if (![notificationInfo[key] isEqual:obj]) { + shouldCancel = NO; + *stop = YES; + } + }]; + + if (shouldCancel) { + [notificationIdentifiersToCancel addObject:request.identifier]; } - }]; - if (matchesAll) { - [RCTSharedApplication() cancelLocalNotification:notification]; } - } + + [center removePendingNotificationRequestsWithIdentifiers:notificationIdentifiersToCancel]; + }]; } RCT_EXPORT_METHOD(getInitialNotification