1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#import "EXScopedNotificationPresentationModule.h"
4#import "EXScopedNotificationsUtils.h"
5#import "EXScopedNotificationSerializer.h"
6#import "EXScopedNotificationsUtils.h"
7
8@interface EXScopedNotificationPresentationModule ()
9
10@property (nonatomic, strong) NSString *scopeKey;
11
12@end
13
14@implementation EXScopedNotificationPresentationModule
15
16- (instancetype)initWithScopeKey:(NSString *)scopeKey
17{
18  if (self = [super init]) {
19    _scopeKey = scopeKey;
20  }
21
22  return self;
23}
24
25- (NSArray * _Nonnull)serializeNotifications:(NSArray<UNNotification *> * _Nonnull)notifications
26{
27  NSMutableArray *serializedNotifications = [NSMutableArray new];
28  for (UNNotification *notification in notifications) {
29    if ([EXScopedNotificationsUtils shouldNotification:notification beHandledByExperience:_scopeKey]) {
30      [serializedNotifications addObject:[EXScopedNotificationSerializer serializedNotification:notification]];
31    }
32  }
33  return serializedNotifications;
34}
35
36- (void)dismissNotificationWithIdentifier:(NSString *)identifier resolve:(EXPromiseResolveBlock)resolve reject:(EXPromiseRejectBlock)reject
37{
38  __block NSString *scopeKey = _scopeKey;
39  [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
40    for (UNNotification *notification in notifications) {
41      if ([EXScopedNotificationsUtils shouldNotification:notification beHandledByExperience:scopeKey]) {
42        // Usually we would scope the input ID and then check equality, but remote notifications do not
43        // have the scoping prefix, so instead let's remove the scope if there is one, then check for
44        // equality against the input
45        NSString *unscopedIdentifier = [EXScopedNotificationsUtils getScopeAndIdentifierFromScopedIdentifier:notification.request.identifier].identifier;
46        if ([unscopedIdentifier isEqualToString:identifier]) {
47          [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notification.request.identifier]];
48        }
49        break;
50      }
51    }
52    resolve(nil);
53  }];
54}
55
56- (void)dismissAllNotificationsWithResolver:(EXPromiseResolveBlock)resolve reject:(EXPromiseRejectBlock)reject
57{
58  __block NSString *scopeKey = _scopeKey;
59  [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
60    NSMutableArray<NSString *> *toDismiss = [NSMutableArray new];
61    for (UNNotification *notification in notifications) {
62      if ([EXScopedNotificationsUtils shouldNotification:notification beHandledByExperience:scopeKey]) {
63        [toDismiss addObject:notification.request.identifier];
64      }
65    }
66    [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:toDismiss];
67    resolve(nil);
68  }];
69}
70
71@end
72