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