Skip to content

Commit

Permalink
Introduce new notification callback in RCTPushNotificationManager (#4…
Browse files Browse the repository at this point in the history
…2405)

Summary:
Pull Request resolved: #42405

## Changelog:

[iOS][Deprecated] Deprecating RCTPushNotificationManager's didReceiveLocalNotification: and didReceiveRemoteNotification:

Reviewed By: philIip

Differential Revision: D52375779

fbshipit-source-id: a668d448e76960fd14784c1a0eede9629f264292
  • Loading branch information
Ingrid Wang authored and facebook-github-bot committed Jan 23, 2024
1 parent 64d8190 commit 1b85ed9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -230,30 +230,49 @@ + (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
object:self
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];
Expand Down

0 comments on commit 1b85ed9

Please sign in to comment.