2016-08-02 68 views
0

我正在推送通知和本地通知,但在前台通知工作正常,但当终止应用程序在这种情况下,我不能重定向特定的观点时,点击通知。为什么不能处理APNS推送通知和本地通知?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

     // Check if launched from notification 
     // 1 
     if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] { 
      // 2 
      let aps = notification["aps"] as! [String: AnyObject] 
      //Redirect to notification view 
      handlePushMessage(aps) 
     }else if let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? [String: AnyObject] { 
      // 2 
      self.postData = notification["aps"] as! [String: AnyObject] 
      //Redirect to notification view 
      didTapNotification() 
     }else{ 
      //Session 
      //Redirect to Main view 
      checkUserSession() 
     } 
    return true 
} 

我当面向应用程式无效此面临着来自APNS通知和本地通知这个问题,或者terminate.Please帮我来回找到解决方案。

+0

当你点击通知你的应用程序在后台运行或没有?你的代码只有在你的应用程序将被重新启动,但不是当它在后台并且你点击通知时才能工作 –

+0

然后如何在后台应用程序中处理/不活动状态如何重定向到特定视图? –

+0

当我点击通知时,我的应用程序在后台条件下,那么它不会重定向特定页面。 –

回答

0

最后我得到的解决方案管理APNS推送通知和本地通知,其现在工作的罚款。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true) 

    //Register here For Notification 

    if #available(iOS 9, *) { 

     let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] 
     let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil) 
     application.registerUserNotificationSettings(pushNotificationSettings) 
     application.registerForRemoteNotifications() 

    }else{ 

     UIApplication.sharedApplication().registerUserNotificationSettings(
      UIUserNotificationSettings(forTypes: .Alert, categories: nil)) 
     application.registerForRemoteNotifications() 
    } 

dispatch_async(dispatch_get_main_queue()) { 
     // Check if launched from notification 
     // 1 
     if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] { 
      // 2 
      let aps = notification["aps"] as! [String: AnyObject] 
      handlePushMessage(aps) 
     }else if let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { 
      // 2 
      self.postData = notification.userInfo as! [String: AnyObject] 
      didTapNotification() 
     }else{ 
      //Session 
      checkUserSession() 
     } 
     } 

} 
0

您如何在didReceiveRemoteNotification函数中尝试?

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    if(application.applicationState == UIApplicationStateBackground){ 



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



    } 
} 
+0

它已经在我身边工作了只有一件事是当你终止应用程序,并点击通知然后它不起作用。 –

+0

你如何添加if(application.applicationState == UIApplicationStateBackground){ } – Rurouni

0

试试这个代码,你可以根据你的要求进行修改。如果你有任何理解这个逻辑的问题,请告诉我。使用completionHandler的didReceiveRemoteNotification可以在后台和前台运行。不要忘记在plist中做适当的更改以支持后台获取和后台通知。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 
    application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum) 

    if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] { 
     notificationDataDict = notification; 
    } 


    return true 
} 
func handleRemoteNotificationWithUserInfo(application:UIApplication, userInfo:NSDictionary){ 

} 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

    print(userInfo) 

    if application.applicationState != .Inactive{ 

     notificationDataDict = userInfo; 

     if let navigationController = self.window?.rootViewController as? UINavigationController{ 


      if application.applicationState == .Active{ 

       if application.backgroundRefreshStatus == .Available{ 

        completionHandler(.NewData) 

        self.handleRemoteNotificationWithUserInfo(application, userInfo: userInfo) 


       } 
       else 
       { 
        completionHandler(.NoData) 
       } 
      } 
      else{ 
       completionHandler(.NewData) 

       self.handleRemoteNotificationWithUserInfo(application, userInfo: userInfo) 
      } 

     } 


    } 
}