2016-09-17 92 views
0

我使用如何獲取iOS 10通知中的應用程序狀態?

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

用於獲取iOS中10條通知的答覆,誰能告訴我怎麼去應用國呢?

由於iOS的9或以前我用過的

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

在這種方法中,我們可以通過

application.applicationState 

感謝您的幫助獲取應用程序的狀態。

回答

0

我做了一些搜索和我得到了這些方法

- (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 
} 
+0

那裏是第三個狀態 - UIApplicationState.Inactive,與Background有點不同,也就是應用程序被強制關閉時的狀態。任何想法我們如何檢查UNUserNotificationCenter –

0

事情是這樣的:

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

'UIApplication * application = [[UIApplication sharedApplication] .applicationState];'這行會給出錯誤,因爲'[[UIApplication sharedApplication] .applicationState]'將返回'UIApplicationState'而不是'UIApplication' – Lion

+0

當然,只需快速輸入 – nerowolfe

0

您可以在您的項目像任何地方獲得應用程序的狀態,

UIApplication *applicaiton = [UIApplication sharedApplication]; 

if (applicaiton.applicationState == UIApplicationStateBackground) { 

    NSLog(@"background state"); 
} 

同樣喜歡你可以用UIApplicationStateActive,UIApplicationStateInactive etc獲得相應的狀態

相關問題