2017-01-11 1 views
0

Meine App verfügt über zwei Arten von Anfangszustand.Probleme mit der Anzeige von Push-Benachrichtigungen in iOS 10 Swift 3

  1. Anmeldung
  2. nach dem Login die Dashboard-& andere Sachen enthält.

Meine app funktioniert gut, wenn Ansicht von AppDelegate, dass ich Problem bin vor

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 
} 

Schalt, wenn ich Push-Benachrichtigung zu behandeln haben. I empfangenen Push-Benachrichtigung in IOS 10 in diesem Verfahren

@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) 
} 

Wenn i Benachrichtigung von Benachrichtigungsfach angezapft diese beiden Verfahren ausgelöst werden, während app Vordergrund oder im Hintergrund ist. Nachdem ich auf die Benachrichtigung getippt habe, muss ich eine Detailseite anzeigen. Ich habe versucht, die Detailseite so zu setzen, die ich vor

@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 
      } 
     } 
    } 
} 

Jedes Mal, wenn ich, dass die App abstürzt tun in didFinishLaunchingWithOptions Methode tat. Wie man das überwindet. Bitte helfen Sie. Danke im Voraus.

+0

auf welcher Zeile stürzt Ihre App –

+0

, wenn ich Details anzeigen als rootviewController in userNotificationCenter-Methode –

+0

was ist Ihr Crash-Protokoll kannst du mir zeigen ? –

Antwort

0

Schließlich löste dies mit Observer Design Pattern.

Wenn App im Hintergrund ist & die die Meldung im Infobereich tippte dann die Benachrichtigung Post von hier aus wie die

@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) 
      } 
     } 
    } 
} 

Um unten Code in Ihre Viewcontroller

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

Get diese Benachrichtigung init zu erhalten Antwort von der folgenden Code

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) 
    } 
} 

Vergessen Sie nicht, Beobachter aus den Viewcontroller wie die

deinit { 
    NotificationCenter.default.removeObserver(self) 
} 

zu entfernen Wenn App keine Instanz im Hintergrund hat & die Meldung im Fach tippte dann, wie die Benachrichtigung zeigen, die keine Instanz hat. Dies kann wie in AppDelegate

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

Überprüfen Sie Ihre erste Viewcontroller ist dieser Schlüssel vorhanden ist oder nicht so handhaben sein

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) 
    } 

Vergessen Sie nicht, diesen Schlüssel zu entfernen, nachdem die detailsController wie die

zeigt
Verwandte Themen