How to get Application states in iOS 10 notification?

3.1k Views Asked by At

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.

3

There are 3 best solutions below

1
Sachin Nautiyal On BEST ANSWER

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 
} 
2
nerowolfe On

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";
}
0
Ketan Parmar On

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