1

I have the following code in my AppDelegate as per the react-native PushNotificationIOS documentation (not using in plugins), with a slight tweak on didReceiveRemoteNotification so that it handles notifications for both foreground and background, however, I can't tell where it was handled from, and my handler needs to know since it does different things.

Any ideas how I can get this information?

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
 {
  [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
 }
 // Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
 {
  [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
 }
 // Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
 {
  [RCTPushNotificationManager didReceiveRemoteNotification:userInfo];
  completionHandler(UIBackgroundFetchResultNewData);
 }
 // Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
 {
  [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
 }
 // Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
 {
  [RCTPushNotificationManager didReceiveLocalNotification:notification];
 }
JackDev
  • 4,891
  • 1
  • 39
  • 48
  • Do you want to know JS side or in native side. If in native side, refer to this https://stackoverflow.com/questions/38971994/detect-if-the-application-in-background-or-foreground-in-swift. In JS side you can use react-native AppState api. https://facebook.github.io/react-native/docs/appstate – santosh Mar 06 '19 at 18:30
  • Thank you @santosh, I'll probably try it first from the react-native side, since I'm more familiar with that, but in case I don't get it right I'll check the native side. – JackDev Mar 06 '19 at 21:32
  • Also if it answers the question , I will post in the answer section for getting upvote. :-) – santosh Mar 06 '19 at 22:08
  • It does answer the question, thank you! I check if it's in the foreground using `AppState.currentState === 'active'`, and if that returns false it's opening it from the background! – JackDev Mar 07 '19 at 14:09

1 Answers1

1

Do you want to know in JS side or in native side. If in native side, refer to this Detect if the application in background or foreground in swift. In JS side you can use react-native AppState api. https://facebook.github.io/react-native/docs/appstate

santosh
  • 140
  • 1
  • 13