Skip to content

Commit

Permalink
Migrate cancel local notification methods to UserNotifications
Browse files Browse the repository at this point in the history
Differential Revision: D50275540

fbshipit-source-id: f5b970fb82d7d7d7df65e379967fe9f422a3f8bc
  • Loading branch information
Ingrid Wang authored and facebook-github-bot committed Oct 17, 2023
1 parent 26b4145 commit 62d2f2b
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 62d2f2b

Please sign in to comment.