|
1 | 1 | #import "AppDelegate.h" |
2 | 2 | #import "RNNCustomViewController.h" |
3 | 3 | #import <ReactNativeNavigation/ReactNativeNavigation.h> |
| 4 | +#import <React/RCTBridge.h> |
| 5 | +#import <React/RCTLinkingManager.h> |
| 6 | +#import <React/RCTRootView.h> |
| 7 | +#import <UserNotifications/UserNotifications.h> |
| 8 | + |
| 9 | +// URLs that arrive (notification tap, openURL) before the JS bridge is |
| 10 | +// ready are queued here and flushed when the bridge finishes loading. |
| 11 | +// Without this, cold-start notifications would post `RCTOpenURLNotification` |
| 12 | +// into the void because RCTLinkingManager hasn't subscribed yet. |
| 13 | +static NSMutableArray<NSURL *> *gPendingDeepLinkURLs = nil; |
| 14 | +static BOOL gJavaScriptDidLoad = NO; |
4 | 15 |
|
5 | 16 | #if !RNN_RN_VERSION_79_OR_NEWER |
6 | | -@interface AppDelegate () <RCTBridgeDelegate> |
| 17 | +@interface AppDelegate () <RCTBridgeDelegate, UNUserNotificationCenterDelegate> |
7 | 18 | @end |
8 | 19 | #else |
9 | | -@interface AppDelegate () |
| 20 | +@interface AppDelegate () <UNUserNotificationCenterDelegate> |
10 | 21 | @end |
11 | 22 |
|
12 | 23 | @interface ReactNativeDelegate : RCTDefaultReactNativeFactoryDelegate |
@@ -51,10 +62,109 @@ - (BOOL)application:(UIApplication *)application |
51 | 62 | callback:^UIViewController *(NSDictionary *props, RCTHost *host) { |
52 | 63 | return [[RNNCustomViewController alloc] initWithProps:props]; |
53 | 64 | }]; |
54 | | - |
| 65 | + |
| 66 | + // Receive notification taps (Detox sendUserNotification & real push taps) |
| 67 | + [UNUserNotificationCenter currentNotificationCenter].delegate = self; |
| 68 | + |
| 69 | + // Flush deep links that arrived before the JS bridge was up. |
| 70 | + // In legacy mode `RCTJavaScriptDidLoadNotification` fires after the |
| 71 | + // bridge loads JS. In bridgeless/new-arch that notification does NOT |
| 72 | + // fire, so we also listen for `RCTContentDidAppearNotification` which |
| 73 | + // is posted by Fabric's root view once content has rendered — by which |
| 74 | + // point RCTLinkingManager is already instantiated and listening. |
| 75 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 76 | + selector:@selector(handleJavaScriptDidLoad:) |
| 77 | + name:RCTJavaScriptDidLoadNotification |
| 78 | + object:nil]; |
| 79 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 80 | + selector:@selector(handleJavaScriptDidLoad:) |
| 81 | + name:RCTContentDidAppearNotification |
| 82 | + object:nil]; |
| 83 | + |
55 | 84 | return YES; |
56 | 85 | } |
57 | 86 |
|
| 87 | +#pragma mark - Deep linking |
| 88 | + |
| 89 | +// Forward foreground URL openings (custom schemes & universal links) |
| 90 | +// to React Native's Linking module so JS can handle them. |
| 91 | +- (BOOL)application:(UIApplication *)application |
| 92 | + openURL:(NSURL *)url |
| 93 | + options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options { |
| 94 | + [self dispatchDeepLinkURL:url]; |
| 95 | + return YES; |
| 96 | +} |
| 97 | + |
| 98 | +// Dispatch a deep link URL. If RCTLinkingManager hasn't subscribed yet |
| 99 | +// (cold-start, JS bridge still loading), queue it for replay after |
| 100 | +// RCTJavaScriptDidLoadNotification fires. |
| 101 | +- (void)dispatchDeepLinkURL:(NSURL *)url { |
| 102 | + if (url == nil) { return; } |
| 103 | + if (gJavaScriptDidLoad) { |
| 104 | + [RCTLinkingManager application:[UIApplication sharedApplication] |
| 105 | + openURL:url |
| 106 | + options:@{}]; |
| 107 | + return; |
| 108 | + } |
| 109 | + if (gPendingDeepLinkURLs == nil) { |
| 110 | + gPendingDeepLinkURLs = [NSMutableArray array]; |
| 111 | + } |
| 112 | + [gPendingDeepLinkURLs addObject:url]; |
| 113 | +} |
| 114 | + |
| 115 | +- (void)handleJavaScriptDidLoad:(NSNotification *)notification { |
| 116 | + gJavaScriptDidLoad = YES; |
| 117 | + NSArray<NSURL *> *pending = [gPendingDeepLinkURLs copy]; |
| 118 | + [gPendingDeepLinkURLs removeAllObjects]; |
| 119 | + for (NSURL *url in pending) { |
| 120 | + [RCTLinkingManager application:[UIApplication sharedApplication] |
| 121 | + openURL:url |
| 122 | + options:@{}]; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +- (BOOL)application:(UIApplication *)application |
| 127 | +continueUserActivity:(NSUserActivity *)userActivity |
| 128 | + restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> *))restorationHandler { |
| 129 | + return [RCTLinkingManager application:application |
| 130 | + continueUserActivity:userActivity |
| 131 | + restorationHandler:restorationHandler]; |
| 132 | +} |
| 133 | + |
| 134 | +#pragma mark - UNUserNotificationCenterDelegate |
| 135 | + |
| 136 | +// Surface notifications while the app is in the foreground so the user |
| 137 | +// (and Detox) can see them before the tap is simulated. |
| 138 | +// |
| 139 | +// NOTE: `UNNotificationPresentationOptionAlert` is intentionally included |
| 140 | +// alongside the iOS 14+ `.banner`/`.list` flags because Detox checks |
| 141 | +// `options.contains(.alert)` in DetoxUserNotificationDispatcher before |
| 142 | +// invoking the tap path. Dropping `.alert` would break e2e notification |
| 143 | +// tests on Detox even though iOS 14+ otherwise prefers banner/list. |
| 144 | +- (void)userNotificationCenter:(UNUserNotificationCenter *)center |
| 145 | + willPresentNotification:(UNNotification *)notification |
| 146 | + withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { |
| 147 | + completionHandler(UNNotificationPresentationOptionAlert | |
| 148 | + UNNotificationPresentationOptionBanner | |
| 149 | + UNNotificationPresentationOptionList | |
| 150 | + UNNotificationPresentationOptionSound); |
| 151 | +} |
| 152 | + |
| 153 | +// Notification tap → if payload carries `url`, route it through the |
| 154 | +// existing Linking pipeline so deep linking reacts the same way regardless |
| 155 | +// of whether the URL came from the OS or a notification. |
| 156 | +- (void)userNotificationCenter:(UNUserNotificationCenter *)center |
| 157 | +didReceiveNotificationResponse:(UNNotificationResponse *)response |
| 158 | + withCompletionHandler:(void (^)(void))completionHandler { |
| 159 | + NSDictionary *userInfo = response.notification.request.content.userInfo; |
| 160 | + NSString *urlString = userInfo[@"url"]; |
| 161 | + if ([urlString isKindOfClass:[NSString class]]) { |
| 162 | + NSURL *url = [NSURL URLWithString:urlString]; |
| 163 | + [self dispatchDeepLinkURL:url]; |
| 164 | + } |
| 165 | + completionHandler(); |
| 166 | +} |
| 167 | + |
58 | 168 | #if !RNN_RN_VERSION_79_OR_NEWER |
59 | 169 | #pragma mark - RCTBridgeDelegate |
60 | 170 | - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge |
|
0 commit comments