1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#if __has_include(<EXNotifications/EXNotificationsEmitter.h>)
4
5#import "EXScopedNotificationsEmitter.h"
6#import "EXScopedNotificationsUtils.h"
7#import "EXScopedNotificationSerializer.h"
8
9@interface EXScopedNotificationsEmitter ()
10
11@property (nonatomic, strong) NSString *scopeKey;
12
13@end
14
15@interface EXNotificationsEmitter (Protected)
16
17- (NSDictionary *)serializedNotification:(UNNotification *)notification;
18- (NSDictionary *)serializedNotificationResponse:(UNNotificationResponse *)notificationResponse;
19
20@end
21
22@implementation EXScopedNotificationsEmitter
23
24- (instancetype)initWithScopeKey:(NSString *)scopeKey
25{
26  if (self = [super init]) {
27    _scopeKey = scopeKey;
28  }
29
30  return self;
31}
32
33- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
34{
35  if ([EXScopedNotificationsUtils shouldNotification:response.notification beHandledByExperience:_scopeKey]) {
36    [super userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
37  } else {
38    completionHandler();
39  }
40}
41
42- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
43{
44  if ([EXScopedNotificationsUtils shouldNotification:notification beHandledByExperience:_scopeKey]) {
45    [super userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
46  } else {
47    completionHandler(UNNotificationPresentationOptionNone);
48  }
49}
50
51- (NSDictionary *)serializedNotification:(UNNotification *)notification
52{
53  return [EXScopedNotificationSerializer serializedNotification:notification];
54}
55
56- (NSDictionary *)serializedNotificationResponse:(UNNotificationResponse *)notificationResponse
57{
58  return [EXScopedNotificationSerializer serializedNotificationResponse:notificationResponse];
59}
60
61@end
62
63#endif
64