Skip to content

Commit

Permalink
Migrate cancel local notification methods to UserNotifications (faceb…
Browse files Browse the repository at this point in the history
…ook#40949)

Summary:
Pull Request resolved: facebook#40949

# Changelog

[Internal] Migrating cancelLocalNotifications and cancelAllLocalNotifications off of deprecated UILocalNotification methods

Reviewed By: philIip, cipolleschi

Differential Revision: D50275540

fbshipit-source-id: 3728e53b410322bbb0e269ac1407d197b8d5979f
  • Loading branch information
Ingrid Wang authored and facebook-github-bot committed Oct 17, 2023
1 parent b816fa7 commit 81ae532
Showing 1 changed file with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -434,28 +434,43 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification

RCT_EXPORT_METHOD(cancelAllLocalNotifications)
{
[RCTSharedApplication() cancelAllLocalNotifications];
[[UNUserNotificationCenter currentNotificationCenter]
getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> *requests) {
NSMutableArray<NSString *> *notificationIdentifiersToCancel = [NSMutableArray new];
for (UNNotificationRequest *request in requests) {
[notificationIdentifiersToCancel addObject:request.identifier];
}
[[UNUserNotificationCenter currentNotificationCenter]
removePendingNotificationRequestsWithIdentifiers:notificationIdentifiersToCancel];
}];
}

RCT_EXPORT_METHOD(cancelLocalNotifications : (NSDictionary<NSString *, id> *)userInfo)
{
for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) {
__block BOOL matchesAll = YES;
NSDictionary<NSString *, id> *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<UNNotificationRequest *> *_Nonnull requests) {
NSMutableArray<NSString *> *notificationIdentifiersToCancel = [NSMutableArray new];
for (UNNotificationRequest *request in requests) {
NSDictionary<NSString *, id> *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
Expand Down

0 comments on commit 81ae532

Please sign in to comment.