|
| 1 | +// |
| 2 | +// Copyright © Batch.com. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +#import "BatchBridgeNotificationCenterDelegate.h" |
| 6 | + |
| 7 | +#import <Batch/BatchPush.h> |
| 8 | + |
| 9 | +@implementation BatchBridgeNotificationCenterDelegate |
| 10 | +{ |
| 11 | + __weak __nullable id<UNUserNotificationCenterDelegate> _previousDelegate; |
| 12 | +} |
| 13 | + |
| 14 | +static BOOL _batBridgeNotifDelegateShouldAutomaticallyRegister = true; |
| 15 | + |
| 16 | + |
| 17 | ++ (BatchBridgeNotificationCenterDelegate *)sharedInstance |
| 18 | +{ |
| 19 | + static BatchBridgeNotificationCenterDelegate *sharedInstance = nil; |
| 20 | + static dispatch_once_t onceToken; |
| 21 | + dispatch_once(&onceToken, ^{ |
| 22 | + sharedInstance = [[BatchBridgeNotificationCenterDelegate alloc] init]; |
| 23 | + }); |
| 24 | + |
| 25 | + return sharedInstance; |
| 26 | +} |
| 27 | + |
| 28 | ++ (void)registerAsDelegate |
| 29 | +{ |
| 30 | + UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter]; |
| 31 | + BatchBridgeNotificationCenterDelegate *instance = [self sharedInstance]; |
| 32 | + instance.previousDelegate = notifCenter.delegate; |
| 33 | + notifCenter.delegate = instance; |
| 34 | +} |
| 35 | + |
| 36 | ++ (BOOL)automaticallyRegister |
| 37 | +{ |
| 38 | + return _batBridgeNotifDelegateShouldAutomaticallyRegister; |
| 39 | +} |
| 40 | + |
| 41 | ++ (void)setAutomaticallyRegister:(BOOL)automaticallyRegister |
| 42 | +{ |
| 43 | + _batBridgeNotifDelegateShouldAutomaticallyRegister = automaticallyRegister; |
| 44 | +} |
| 45 | + |
| 46 | +- (nullable id<UNUserNotificationCenterDelegate>)previousDelegate |
| 47 | +{ |
| 48 | + return _previousDelegate; |
| 49 | +} |
| 50 | + |
| 51 | +- (void)setPreviousDelegate:(nullable id<UNUserNotificationCenterDelegate>)delegate |
| 52 | +{ |
| 53 | + // Do not register ourserlves as previous delegate to avoid |
| 54 | + // an infinite loop |
| 55 | + if (delegate == self || [delegate isKindOfClass:[self class]]) { |
| 56 | + _previousDelegate = nil; |
| 57 | + } else { |
| 58 | + _previousDelegate = delegate; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +- (instancetype)init |
| 63 | +{ |
| 64 | + self = [super init]; |
| 65 | + if (self) { |
| 66 | + _showForegroundNotifications = true; |
| 67 | + _shouldUseChainedCompletionHandlerResponse = true; |
| 68 | + } |
| 69 | + return self; |
| 70 | +} |
| 71 | + |
| 72 | +- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler |
| 73 | +{ |
| 74 | + [BatchPush handleUserNotificationCenter:center willPresentNotification:notification willShowSystemForegroundAlert:self.showForegroundNotifications]; |
| 75 | + |
| 76 | + id<UNUserNotificationCenterDelegate> chainDelegate = self.previousDelegate; |
| 77 | + // It's the chain delegate's responsibility to call the completionHandler |
| 78 | + if ([chainDelegate respondsToSelector:@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:)]) { |
| 79 | + //returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...}; |
| 80 | + void (^chainCompletionHandler)(UNNotificationPresentationOptions); |
| 81 | + |
| 82 | + if (self.shouldUseChainedCompletionHandlerResponse) { |
| 83 | + // Set iOS' completion handler as the one we give to the method, as we don't want to override the result |
| 84 | + chainCompletionHandler = completionHandler; |
| 85 | + } else { |
| 86 | + // Set ourselves as the chained completion handler so we can wait for the implementation but rewrite the response |
| 87 | + chainCompletionHandler = ^(UNNotificationPresentationOptions ignored) { |
| 88 | + [self performPresentCompletionHandler:completionHandler]; |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + [chainDelegate userNotificationCenter:center |
| 93 | + willPresentNotification:notification |
| 94 | + withCompletionHandler:chainCompletionHandler]; |
| 95 | + } else { |
| 96 | + [self performPresentCompletionHandler:completionHandler]; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler |
| 101 | +{ |
| 102 | + [BatchPush handleUserNotificationCenter:center didReceiveNotificationResponse:response]; |
| 103 | + |
| 104 | + id<UNUserNotificationCenterDelegate> chainDelegate = self.previousDelegate; |
| 105 | + // It's the chain delegate's responsibility to call the completionHandler |
| 106 | + if ([chainDelegate respondsToSelector:@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:)]) { |
| 107 | + [chainDelegate userNotificationCenter:center |
| 108 | + didReceiveNotificationResponse:response |
| 109 | + withCompletionHandler:completionHandler]; |
| 110 | + } else { |
| 111 | + if (completionHandler) { |
| 112 | + completionHandler(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +- (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification |
| 119 | +{ |
| 120 | + if (@available(iOS 12.0, *)) { |
| 121 | + id<UNUserNotificationCenterDelegate> chainDelegate = self.previousDelegate; |
| 122 | + if ([chainDelegate respondsToSelector:@selector(userNotificationCenter:openSettingsForNotification:)]) { |
| 123 | + [self.previousDelegate userNotificationCenter:center |
| 124 | + openSettingsForNotification:notification]; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/// Call iOS back on the "present" completion handler with Batch controlled presentation options |
| 130 | +- (void)performPresentCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { |
| 131 | + UNNotificationPresentationOptions options = UNNotificationPresentationOptionNone; |
| 132 | + if (self.showForegroundNotifications) { |
| 133 | + options = UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound; |
| 134 | + |
| 135 | +#ifdef __IPHONE_14_0 |
| 136 | + if (@available(iOS 14.0, *)) { |
| 137 | + options = options | UNNotificationPresentationOptionList | UNNotificationPresentationOptionBanner; |
| 138 | + } else { |
| 139 | + options = options | UNNotificationPresentationOptionAlert; |
| 140 | + } |
| 141 | +#else |
| 142 | + options = options | UNNotificationPresentationOptionAlert; |
| 143 | +#endif |
| 144 | + } |
| 145 | + |
| 146 | + if (completionHandler) { |
| 147 | + completionHandler(options); |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +@end |
0 commit comments