wix / react-native-notifications

React Native Notifications
MIT License
3.25k stars 764 forks source link

feat: add ios native methods to inform RNN about clicked notification and notification received in foreground. #999

Closed korzonkiee closed 8 months ago

korzonkiee commented 1 year ago

Changes

Added the following iOS native methods to RNNotifications.h:

+ (void)didReceiveForegroundNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
+ (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(void))completionHandler

Implemented the above methods in RNNotifications.m to handle foreground notifications, as well as notification responses.

Why

I have multiple notification providers in my app (react-native-moengage & react-native-notifications). In order for both of those packages to receive push notifications, I have to:

  1. disable AppDelegate method swizzling by react-native-moengage by setting MoEngageAppDelegateProxyEnabled in Info.plist to NO, more details here.
  2. set the AppDelegate as UNUserNotificationCenter,
  3. pass the notification data both to react-native-moengage & react-native-notifications.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  /// Make sure that method swizzling is disabled by setting `MoEngageAppDelegateProxyEnabled` in the app’s `Info.plist` file and setting it to bool value `NO`.

  /// Make sure that the AppDelegate is set as the `UNUserNotificationCenter`.
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  NSLog(@"AppDelegate.application.didRegisterForRemoteNotificationsWithDeviceToken");
  [[MoEngageSDKMessaging sharedInstance] setPushToken:deviceToken];
  [RNNotifications didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  NSLog(@"AppDelegate.application.didFailToRegisterForRemoteNotificationsWithError");
  [[MoEngageSDKMessaging sharedInstance]didFailToRegisterForPush];
  [RNNotifications didFailToRegisterForRemoteNotificationsWithError:error];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
  NSLog(@"AppDelegate.application.didReceiveRemoteNotification");
  [[MoEngageSDKMessaging sharedInstance] didReceieveNotificationInApplication:application withInfo:userInfo];
  [RNNotifications didReceiveBackgroundNotification:userInfo withCompletionHandler:completionHandler];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  NSLog(@"AppDelegate.userNotificationCenter.didReceiveNotificationResponse");
  [[MoEngageSDKMessaging sharedInstance] userNotificationCenter:center didReceive:response];
  [RNNotifications didReceiveNotificationResponse:response completionHandler:completionHandler];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  NSLog(@"AppDelegate.userNotificationCenter.willPresentNotification");
  [[MoEngageSDKMessaging sharedInstance] userNotificationCenter:center willPresent:notification];
  [RNNotifications didReceiveForegroundNotification:notification withCompletionHandler:completionHandler];
  completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound);
}
stale[bot] commented 11 months ago

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 8 months ago

The issue has been closed for inactivity.