From ccff2bb8d19b2db244f30293b4e8d68a524c2059 Mon Sep 17 00:00:00 2001 From: Ingrid Wang Date: Wed, 24 Jan 2024 09:29:41 -0800 Subject: [PATCH] Migrate RNTester onto new RN notification callbacks (#42406) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/42406 ## Changelog: [iOS][Changed] Migrated RNTester/AppDelegate to the new notification callback Reviewed By: philIip Differential Revision: D52883367 fbshipit-source-id: 0afcd81f4ad4ee0c49b264785fa261789603a072 --- packages/rn-tester/RNTester/AppDelegate.mm | 30 +++++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/rn-tester/RNTester/AppDelegate.mm b/packages/rn-tester/RNTester/AppDelegate.mm index ddda5db3aa56a5..d0d725da6df193 100644 --- a/packages/rn-tester/RNTester/AppDelegate.mm +++ b/packages/rn-tester/RNTester/AppDelegate.mm @@ -7,6 +7,8 @@ #import "AppDelegate.h" +#import + #import #import #import @@ -22,6 +24,9 @@ static NSString *kBundlePath = @"js/RNTesterApp.ios"; +@interface AppDelegate () +@end + @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions @@ -31,6 +36,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( // They will be passed down to the ViewController used by React Native. self.initialProps = [self prepareInitialProps]; + [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; + return [super application:application didFinishLaunchingWithOptions:launchOptions]; } @@ -96,17 +103,26 @@ - (void)application:(__unused UIApplication *)application [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error]; } -// Required for the remoteNotificationReceived event. -- (void)application:(__unused UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification +#pragma mark - UNUserNotificationCenterDelegate + +// Required for the remoteNotificationReceived and localNotificationReceived events +- (void)userNotificationCenter:(UNUserNotificationCenter *)center + willPresentNotification:(UNNotification *)notification + withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { - [RCTPushNotificationManager didReceiveRemoteNotification:notification]; + [RCTPushNotificationManager didReceiveNotification:notification]; + completionHandler(UNNotificationPresentationOptionNone); } -// Required for the localNotificationReceived event. -- (void)application:(__unused UIApplication *)application - didReceiveLocalNotification:(UILocalNotification *)notification +// 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.) +- (void)userNotificationCenter:(UNUserNotificationCenter *)center + didReceiveNotificationResponse:(UNNotificationResponse *)response + withCompletionHandler:(void (^)(void))completionHandler { - [RCTPushNotificationManager didReceiveLocalNotification:notification]; + [RCTPushNotificationManager didReceiveNotification:response.notification]; + completionHandler(); } #pragma mark - New Arch Enabled settings