1// Copyright 2018-present 650 Industries. All rights reserved. 2 3#import "EXScopedNotificationSchedulerModule.h" 4#import "EXScopedNotificationsUtils.h" 5#import "EXScopedNotificationSerializer.h" 6 7@interface EXScopedNotificationSchedulerModule () 8 9@property (nonatomic, strong) NSString *experienceId; 10 11@end 12 13// TODO: (@lukmccall) experiences may break one another by trying to schedule notifications of the same identifier. 14// See https://github.com/expo/expo/pull/8361#discussion_r429153429. 15@implementation EXScopedNotificationSchedulerModule 16 17- (instancetype)initWithExperienceId:(NSString *)experienceId 18{ 19 if (self = [super init]) { 20 _experienceId = experienceId; 21 } 22 23 return self; 24} 25 26- (NSArray * _Nonnull)serializeNotificationRequests:(NSArray<UNNotificationRequest *> * _Nonnull) requests; 27{ 28 NSMutableArray *serializedRequests = [NSMutableArray new]; 29 for (UNNotificationRequest *request in requests) { 30 if ([EXScopedNotificationsUtils shouldNotificationRequest:request beHandledByExperience:_experienceId]) { 31 [serializedRequests addObject:[EXScopedNotificationSerializer serializedNotificationRequest:request]]; 32 } 33 } 34 return serializedRequests; 35} 36 37- (void)cancelNotification:(NSString *)identifier resolve:(UMPromiseResolveBlock)resolve rejecting:(UMPromiseRejectBlock)reject 38{ 39 __block NSString *experienceId = _experienceId; 40 [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { 41 for (UNNotificationRequest *request in requests) { 42 if ([request.identifier isEqual:identifier]) { 43 if ([EXScopedNotificationsUtils shouldNotificationRequest:request beHandledByExperience:experienceId]) { 44 [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[identifier]]; 45 } 46 break; 47 } 48 } 49 resolve(nil); 50 }]; 51} 52 53- (void)cancelAllNotificationsWithResolver:(UMPromiseResolveBlock)resolve rejecting:(UMPromiseRejectBlock)reject 54{ 55 __block NSString *experienceId = _experienceId; 56 [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { 57 NSMutableArray<NSString *> *toRemove = [NSMutableArray new]; 58 for (UNNotificationRequest *request in requests) { 59 if ([EXScopedNotificationsUtils shouldNotificationRequest:request beHandledByExperience:experienceId]) { 60 [toRemove addObject:request.identifier]; 61 } 62 } 63 [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:toRemove]; 64 resolve(nil); 65 }]; 66} 67 68@end 69