From cc147ceb79c5774e36d7ce7dcdfa3323d0b27734 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Wed, 14 Feb 2024 10:32:11 -0800 Subject: [PATCH] add example for capturing initial notification in push notification manager (#42687) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/42687 Changelog: [Internal] adding an example for if you want to use `getInitialNotification`. Reviewed By: ingridwang Differential Revision: D52931618 fbshipit-source-id: 7552c358e5bc98228b7ae74ea1adf450f7b075e6 --- .../Libraries/AppDelegate/RCTAppDelegate.mm | 2 ++ packages/rn-tester/RNTester/AppDelegate.mm | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm index 4f8ce14bfce..a53a42303d7 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm @@ -198,6 +198,7 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri } #pragma mark - UISceneDelegate + - (void)windowScene:(UIWindowScene *)windowScene didUpdateCoordinateSpace:(id)previousCoordinateSpace interfaceOrientation:(UIInterfaceOrientation)previousInterfaceOrientation @@ -207,6 +208,7 @@ static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabri } #pragma mark - RCTCxxBridgeDelegate + - (std::unique_ptr)jsExecutorFactoryForBridge:(RCTBridge *)bridge { _runtimeScheduler = std::make_shared(RCTRuntimeExecutorFromBridge(bridge)); diff --git a/packages/rn-tester/RNTester/AppDelegate.mm b/packages/rn-tester/RNTester/AppDelegate.mm index d0d725da6df..fcb6360c724 100644 --- a/packages/rn-tester/RNTester/AppDelegate.mm +++ b/packages/rn-tester/RNTester/AppDelegate.mm @@ -116,12 +116,20 @@ static NSString *kBundlePath = @"js/RNTesterApp.ios"; // Required for the remoteNotificationReceived and localNotificationReceived events // Called when a notification is tapped from background. (Foreground notification will not be shown per -// the presentation option selected above.) +// the presentation option selected above). - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { - [RCTPushNotificationManager didReceiveNotification:response.notification]; + UNNotification *notification = response.notification; + + // This condition will be true if tapping the notification launched the app. + if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) { + // This can be retrieved with getInitialNotification. + [RCTPushNotificationManager setInitialNotification:notification]; + } + + [RCTPushNotificationManager didReceiveNotification:notification]; completionHandler(); }