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