2016-06-21 28 views
0

蘋果DOC Getting the User’s Attention While in the Background如何確定後臺程序來前景攻絲本地通知的動作

通知是指被暫停的應用程序的方式,是在 背景,或者沒有運行得到用戶的關注。

我的應用程序被iOS喚醒,因爲區域監視和is in the background併發布本地通知。用戶點擊通知,應用將處於前臺。

如何確定應用程序由於用戶點擊通知而進入前臺?

哪個委託方法將包含通知信息。

didFinishLaunchingWithOption or didReceiveLocalNotification 
+0

的確定這如果應用程序被暫停,而不是運行,那麼應用程序將調用「didFinishLaunchingWithOption」,如果應用程序在後臺運行,那麼你將收到通知信息在「didReceiveLocalNotification」中。 – Pushpa

回答

1

如果您的應用程序在後臺運行,並且您在LocalNotification橫幅竊聽,那麼你將被調用下面的方法:

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

的iOS 8之後:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler 

如果應用程序是不在後臺運行,您將收到通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
if ([launchOptions valueForKey:@"UIApplicationLaunchOptionsLocalNotificationKey"]) { 
// here you will get 
} 
1

當UILocalNotification被觸發時,您可以檢測到您的應用程序的狀態,並且如果調用 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification ,則確保接收到本地通知。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    UIApplicationState state = [application applicationState]; 
    if (state == UIApplicationStateInactive) { 
     // Application was in the background when notification was delivered. 
    } else { 

    } 
} 
0

您可以通過使用application.applicationState

if(application.applicationState == UIApplicationStateInactive) 
    { 
      NSLog(@" Inactive"); 
     // when you tapping on notification 

    } 
    else if (application.applicationState == UIApplicationStateBackground) 
    { 
      NSLog(@" background"); 
    } 
    else 
    { 
      NSLog(@" Active"); 

    } 
相關問題