0

I am using

userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler

for getting the notification response in iOS 10, can anyone tell me how to get the Application states in it ?

Because in iOS 9 or before I used

application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

In this method, we can get the application state by

application.applicationState

thanks for the help.

Sachin Nautiyal
  • 568
  • 4
  • 15

3 Answers3

1

You can get application's state from anywhere in your project something like,

   UIApplication *applicaiton = [UIApplication sharedApplication];

if (applicaiton.applicationState  == UIApplicationStateBackground) {

    NSLog(@"background state");
}

same like you can use UIApplicationStateActive,UIApplicationStateInactive etc to get respective state

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
1

I did some search and I got these methods

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center  
willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{  
NSLog( @"for handling push in foreground" );  
// Your code
NSLog(@"%@", notification.request.content.userInfo); //for getting response payload data
}  

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response  withCompletionHandler:(void (^)())completionHandler   {  
NSLog( @"for handling push in background" );  
// Your code
NSLog(@"%@", notification.request.content.userInfo); //for getting response payload data 
} 
Sachin Nautiyal
  • 568
  • 4
  • 15
  • 1
    there is third state - UIApplicationState.Inactive, which is slightly different from Background, i.e. the state when app is forced closed. Any idea how we can check that for UNUserNotificationCenter – Bishal Ghimire Dec 17 '17 at 03:19
0

Something like this:

NSString * state = @"Unknown";
UIApplication *application = [UIApplication sharedApplication];
if ( application.applicationState == UIApplicationStateInactive ) {
    //The application received the notification from an inactive state, i.e. the user tapped the "View" button for the alert.
    //If the visible view controller in your view controller stack isn't the one you need then show the right one.
    state = @"UIApplicationStateInactive";
}

if(application.applicationState == UIApplicationStateActive ) {
    //The application received a notification in the active state, so you can display an alert view or do something appropriate.
    state = @"UIApplicationStateActive";
}

if(application.applicationState == UIApplicationStateBackground ) {
    state = @"UIApplicationStateBackground";
}
nerowolfe
  • 4,787
  • 3
  • 20
  • 19
  • `UIApplication *application = [[UIApplication sharedApplication].applicationState];` this line will give error because `[[UIApplication sharedApplication].applicationState]` will return `UIApplicationState` not `UIApplication` – Ketan Parmar Sep 17 '16 at 10:03
  • Sure, just fast typing – nerowolfe Sep 17 '16 at 10:04