2017-01-11 8 views
0

내 앱에는 2 가지 초기 상태가 있습니다.iOS 10에서 푸시 알림을 보는 데 문제가 있음 Swift 3

  1. 로그인 대시 보드 & 다른 물건을 포함 로그인 후
  2. . 내가 푸시 알림을 처리 할 때이 문제에 직면하고 그

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
         let userDataExist = CommonUserFunction.isUserDataExist() as Bool 
    
         if userDataExist == true { 
    
          let homeViewController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "HomeView") as? HomeViewController 
          let navigationController = UINavigationController() 
          navigationController.viewControllers = [homeViewController!] 
    
          self.window!.rootViewController = navigationController 
         } 
         else{ 
    
          let loginController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "LoginView") as? LoginController 
          let navigationController = UINavigationController() 
          navigationController.viewControllers = [loginController!] 
    
          self.window!.rootViewController = navigationController 
         } 
    
         self.window?.makeKeyAndVisible() 
    
         return true 
    } 
    

    처럼 AppDelegate에에서보기를 전환 할 때

내 응용 프로그램은 제대로 작동합니다. 내가 응용 프로그램 전경 또는 배경 반면 이러한 두 가지 방법이 해고 통지 트레이에서 알림을 탭하면 나는이 방법

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) { 
    print("Handle push from foreground") 
    // custom code to handle push while app is in the foreground 

    print(notification.request.content.userInfo) 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    // custom code to handle push while app is in the foreground 
    print(response.notification.request.content.userInfo) 
} 

에 아이폰 OS (10)에 푸시 알림을 받았다. 알림을 탭하면 세부 정보 페이지가 표시되어야합니다. 난 내가 didFinishLaunchingWithOptions 방법

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    let pushResponse = response.notification.request.content.userInfo as NSDictionary 

    let rtype = pushResponse.object(forKey: "rtype") as? String 
    let rid = pushResponse.object(forKey: "rid") as? String 

    if rtype != nil { 

     if rtype == "5" { 

      if rid != nil { 
       let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController 
       noticeDetailsViewController!.id = rtype! 
       noticeDetailsViewController?.fromPush = true 
       let navigationController = UINavigationController() 
       navigationController.viewControllers = [noticeDetailsViewController!] 
       self.window!.rootViewController = navigationController 
      } 
     } 
    } 
} 

내가 그 응용 프로그램이 충돌 할 때마다에 이전 한 것을 같은 세부 정보 페이지를 설정했습니다. 이것을 극복하는 방법. 도와주세요. 미리 감사드립니다.

+0

내 앱이 충돌하는 곳 –

+0

userNotificationCenter 방법의 세부 정보보기를 rootviewController로 설정하면 –

+0

크래시 로그 란 무엇인가요? 보여 주실 수 있어요 ? –

답변

0

마지막으로 Observer 디자인 패턴을 사용하여이를 해결했습니다. 응용 프로그램이 배경에있을 때

는 & 당신의 ViewController

NotificationCenter.default.addObserver(self, selector: #selector(self.getPushResponse), name: NSNotification.Name(rawValue: "getPush"), object: nil) 

하려면 코드 아래에이 알림 초기화를 받으려면 그

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    let pushResponse = response.notification.request.content.userInfo as NSDictionary 

    let rtype = pushResponse.object(forKey: "rtype") as? String 
    let rid = pushResponse.object(forKey: "rid") as? String 

    if rtype != nil { 

     if rtype == "5" { 

      if rid != nil { 

       NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse) 
      } 
     } 
    } 
} 

처럼 여기에서 알림을 게시 알림 트레이에 알림을 탭 아래 코드의 응답

func getPushResponse(_ notification: Notification){ 

    if let info = notification.object as? NSDictionary { 
     let rid = info.object(forKey: "rid") as? String 
     let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController 
     noticeDetailsViewController?.id = rid! 
     noticeDetailsViewController?.fromPush = true 
     self.navigationController?.pushViewController(noticeDetailsViewController!, animated: true) 
    } 
} 

이 응용 프로그램은 어떤 인스턴스가없는 알림을 표시하는 방법을 다음 트레이의 알림 탭 배경 &에서 어떤 인스턴스가없는 경우 그

deinit { 
    NotificationCenter.default.removeObserver(self) 
} 

처럼의 ViewController에서 관찰자를 제거하는 것을 잊지 마십시오. 이 같은 초기의 ViewController이 키 체크인 존재 여부를 AppDelegate에

let prefs: UserDefaults = UserDefaults.standard 
    if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary { 
     prefs.set(remoteNotification as! [AnyHashable: Any], forKey: "startUpPush") 
     prefs.synchronize() 
    } 

에서 그렇게 처리 할 수 ​​

let prefs:UserDefaults = UserDefaults.standard 
    if prefs.value(forKey: "startUpPush") != nil { 

     let pushResponse = prefs.object(forKey: "startUpPush") as! NSDictionary 
     NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse) 
    } 

처럼 detailsController을 표시 한 후이 키를 제거하는 것을 잊지 마세요
if fromPush == true { 
     let prefs: UserDefaults = UserDefaults.standard 
     if prefs.value(forKey: "startUpPush") != nil { 
      prefs.removeObject(forKey: "startUpPush") 
      prefs.synchronize() 
     } 
    }