From 98dc45eff2ab4c623f88a2a71caf110052523b98 Mon Sep 17 00:00:00 2001 From: Ingrid Wang Date: Mon, 22 Jan 2024 14:52:35 -0800 Subject: [PATCH] [skip ci] Introduce new notification callback in RCTPushNotificationManager (#42405) Summary: ## Changelog: [iOS][Deprecated] Deprecating RCTPushNotificationManager's didReceiveLocalNotification: and didReceiveRemoteNotification: Reviewed By: philIip Differential Revision: D52375779 --- .../RCTPushNotificationManager.h | 30 +++++++++++++++-- .../RCTPushNotificationManager.mm | 33 +++++++++++++++---- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.h b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.h index 546b78021c1740..8423a81ad3d30d 100644 --- a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.h +++ b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.h @@ -14,10 +14,34 @@ extern NSString *const RCTRemoteNotificationReceived; typedef void (^RCTRemoteNotificationCallback)(UIBackgroundFetchResult result); + (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; -+ (void)didReceiveRemoteNotification:(NSDictionary *)notification; ++ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; +/** + * Triggers remoteNotificationReceived or localNotificationReceived events. + * + * Call this method from UNUserNotificationCenterDelegate's + * `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:` in order to process a user tap on a + * notification. + * + * To process notifications received while the app is in the foreground, call this method from + * `userNotificationCenter:willPresentNotification:withCompletionHandler:`. Use the completion handler to determine if + * the push notification should be shown; to match prior behavior which does not show foreground notifications, use + * UNNotificationPresentationOptionNone. + * + * If you need to determine if the notification is remote, check that notification.request.trigger + * is an instance of UNPushNotificationTrigger. + */ ++ (void)didReceiveNotification:(UNNotification *)notification; +/** + * Call this from your app delegate's `application:didReceiveRemoteNotification:fetchCompletionHandler:`. If you + * implement both that method and `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:`, only + * the latter will be called. + */ + (void)didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler; -+ (void)didReceiveLocalNotification:(UILocalNotification *)notification; -+ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; + +/** DEPRECATED. Use didReceiveNotification instead. */ ++ (void)didReceiveLocalNotification:(UILocalNotification *)notification RCT_DEPRECATED; +/** DEPRECATED. Use didReceiveNotification instead. */ ++ (void)didReceiveRemoteNotification:(NSDictionary *)notification RCT_DEPRECATED; @end diff --git a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm index ef526ab958b6ef..6ebe188674fac4 100644 --- a/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm +++ b/packages/react-native/Libraries/PushNotificationIOS/RCTPushNotificationManager.mm @@ -230,23 +230,33 @@ + (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error userInfo:@{@"error" : error}]; } -+ (void)didReceiveRemoteNotification:(NSDictionary *)notification -{ - NSDictionary *userInfo = @{@"notification" : notification}; - [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived - object:self - userInfo:userInfo]; ++ (void)didReceiveNotification:(UNNotification *)notification +{ + BOOL const isRemoteNotification = [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]; + if (isRemoteNotification) { + NSDictionary *userInfo = @{@"notification" : notification.request.content.userInfo}; + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived + object:self + userInfo:userInfo]; + } else { + [[NSNotificationCenter defaultCenter] postNotificationName:kLocalNotificationReceived + object:self + userInfo:RCTFormatUNNotification(notification)]; + } } + (void)didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(RCTRemoteNotificationCallback)completionHandler { - NSDictionary *userInfo = @{@"notification" : notification, @"completionHandler" : completionHandler}; + NSDictionary *userInfo = completionHandler + ? @{@"notification" : notification, @"completionHandler" : completionHandler} + : @{@"notification" : notification}; [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived object:self userInfo:userInfo]; } +// Deprecated + (void)didReceiveLocalNotification:(UILocalNotification *)notification { [[NSNotificationCenter defaultCenter] postNotificationName:kLocalNotificationReceived @@ -254,6 +264,15 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification userInfo:RCTFormatLocalNotification(notification)]; } +// Deprecated ++ (void)didReceiveRemoteNotification:(NSDictionary *)notification +{ + NSDictionary *userInfo = @{@"notification" : notification}; + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationReceived + object:self + userInfo:userInfo]; +} + - (void)handleLocalNotificationReceived:(NSNotification *)notification { [self sendEventWithName:@"localNotificationReceived" body:notification.userInfo];