2016-07-24 54 views
0

每當用戶推送通知,點擊該方法被調用
FUNC application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)但如果應用程序是越來越推出,並同時推來同樣的方法被調用。如何識別應用程序是否啓動或用戶單擊圖標以在前臺獲取應用程序。 這樣我就可以忽略推送通知過程。推送通知方法獲得的啓動應用程序的時候調用

回答

0

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if (launchOptions != nil) 
    { 
     // Here app will open from pushnotification 
     //RemoteNotification 
     NSDictionary* dictionary1 = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
     //LocalNotification 
     NSDictionary* dictionary2 = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
     if (dictionary1 != nil) 
     { 
      //RemoteNotification Payload 
      //set your function to handle notification 
      NSLog(@"Launched from push notification: %@", dictionary1); 
     } 
     if (dictionary2 != nil) 
     { 
      NSLog(@"Launched from dictionary2dictionary2dictionary2 notification: %@", dictionary2); 
      double delayInSeconds = 7; 
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
       // [self addMessageFromRemoteNotification:dictionary2 updateUI:NO]; 
      }); 
     } 

    } 
    else 
     {} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

if(application.applicationState == UIApplicationStateInactive) { 

     NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background"); 
     //do some tasks 

    } 
    else if (application.applicationState == UIApplicationStateBackground) { 

     NSLog(@"application Background - notification has arrived when app was in background"); 
     NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]]; 

     if([contentAvailable isEqualToString:@"1"]) { 
      // do tasks 
      NSLog(@"content-available is equal to 1"); 
     } 
    } 
    else { 
     NSLog(@"application Active - notication has arrived while app was opened"); 
     //Show an in-app banner 
     //do tasks 
    } 
相關問題