swift - Open a ViewController from remote notification -


i try open particular viewcontroller when app catch remote notification.

let me show project's architecture. here storyboard : enter image description here

when receive notification want open "simplepostviewcontroller", appdelegate :

var window: uiwindow? var navigationvc: uinavigationcontroller?  func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool {     let notificationtypes: uiusernotificationtype = [uiusernotificationtype.alert, uiusernotificationtype.badge, uiusernotificationtype.sound]     let pushnotificationsettings = uiusernotificationsettings(fortypes: notificationtypes, categories: nil)     let storyboard = uistoryboard(name: "main", bundle: nil)      self.navigationvc = storyboard.instantiateviewcontrollerwithidentifier("lastestpostsnavigationcontroller") as? uinavigationcontroller     application.registerusernotificationsettings(pushnotificationsettings)     application.registerforremotenotifications()     return true }  func application(application: uiapplication, didreceiveremotenotification userinfo: [nsobject : anyobject]) {     if let postid = userinfo["postid"] as? string {         print(postid)          let api = evwordpressapi(wordpressoauth2settings: wordpress.wordpressoauth2settings, site: wordpress.sitename)          api.postbyid(postid) { post in             if (post != nil) {                 self.navigationvc!.pushviewcontroller(simplepostviewcontroller(), animated: true)             } else {                 print("an error occurred")             }         }      } } 

i save uinavigationviewcontroller when app launch , try push new simplepostviewcontroller when receive notification. nothing happen. placed breakpoints , seen pushviewcontroller method reached, not viewwillappear of simplepostviewcontroller.

i used "whats new" view add perform segue nothing happen too.

solution :

for child in (self.rootviewcontroller?.childviewcontrollers)! {     if child.restorationidentifier == "lastestpostsnavigationcontroller" {        let lastestpoststableviewcontroller = (child.childviewcontrollers[0]) as! lastestpoststableviewcontroller        let simplepostvc = (self.storyboard?.instantiateviewcontrollerwithidentifier("postviewcontroller"))! as! postviewcontroller         simplepostvc.post = post        lastestpoststableviewcontroller.navigationcontroller?.pushviewcontroller(simplepostvc, animated: true)     }  } 

i use :

child.childviewcontrollers[0] 

because i've 1 child in example.

i created sample project local notification instead of remote notification ease of showing functionality should simple setting root view controller of window in app delegate didreceiveremote notification.

func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool {      // subscribe notifications - assume user chose yes     application.registerusernotificationsettings(uiusernotificationsettings(fortypes: [.alert, .badge, .sound], categories: nil))      return true }   func applicationdidenterbackground(application: uiapplication) {     //crete local notification     let notification = uilocalnotification()     notification.alertbody = "this fake notification"     notification.firedate  = nsdate(timeintervalsincenow: 2)     uiapplication.sharedapplication().schedulelocalnotification(notification) }  func application(application: uiapplication, didreceivelocalnotification notification: uilocalnotification) {     let sb = uistoryboard(name: "main", bundle: nil)     let othervc = sb.instantiateviewcontrollerwithidentifier("othervc") as! otherviewcontroller     window?.rootviewcontroller = othervc; }  func application(application: uiapplication, didreceiveremotenotification userinfo: [nsobject : anyobject]) {     //your code here } 

` need worry managing view hierarchy , sending need send notification user data.

in example, create local notification when close app fires after view seconds. if launch app notification, open "other view controller" "simplepostviewcontroller" in case.

also, sure registering remote notifications in didfinishlaunchwithoptions.

github simple sample : https://github.com/spt131/examplenotificationresponse


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -