ios - How to open particular post on the click on push notification -
in app implemented push notification. when app in running state , if push notification come handle code.
func application(application: uiapplication, didreceiveremotenotification userinfo: [nsobject : anyobject]) { print(userinfo) myid = (userinfo["id"] as? string)! print(myid) if let notification = userinfo["aps"] as? nsdictionary, let alert = notification["alert"] as? string { var alertctrl = uialertcontroller(title: "notification", message: alert string, preferredstyle: uialertcontrollerstyle.alert) alertctrl.addaction(uialertaction(title: "ok", style: uialertactionstyle.default, handler: nil)) // find presented vc... var presentedvc = self.window?.rootviewcontroller while (presentedvc!.presentedviewcontroller != nil) { presentedvc = presentedvc!.presentedviewcontroller } presentedvc!.presentviewcontroller(alertctrl, animated: true, completion: nil) }
what want :: when app not running , if push notification come want open perticular post based on notification post id. how can this? here code
func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { svprogresshud.setdefaultmasktype(svprogresshudmasktype.black) uiapplication.sharedapplication().applicationiconbadgenumber = 0 let dictemp = launchoptions?["uiapplicationlaunchoptionsremotenotificationkey"] if dictemp != nil{ window = uiwindow(frame: uiscreen.mainscreen().bounds) storyboard = uistoryboard(name: "main", bundle: nil) myid = (dictemp["id"] as? string)! let controller:pushnotificationpostviewcontroller = self.storyboard.instantiateviewcontrollerwithidentifier("pushnotificationpostviewcontroller") as! pushnotificationpostviewcontroller navigation = uinavigationcontroller(rootviewcontroller: controller) window?.rootviewcontroller = navigation window?.makekeyandvisible() } else { window = uiwindow(frame: uiscreen.mainscreen().bounds) storyboard = uistoryboard(name: "main", bundle: nil) let controller:mainviewcontroller = self.storyboard.instantiateviewcontrollerwithidentifier("mainviewcontroller") as! mainviewcontroller navigation = uinavigationcontroller(rootviewcontroller: controller) window?.rootviewcontroller = navigation window?.makekeyandvisible() } //print(nsuserdefaults.standarduserdefaults().valueforkey("pushnotify")as! bool) let notificationtypes: uiusernotificationtype = [uiusernotificationtype.alert, uiusernotificationtype.badge, uiusernotificationtype.sound] let pushnotificationsettings = uiusernotificationsettings(fortypes: notificationtypes, categories: nil) application.registerusernotificationsettings(pushnotificationsettings) if (nsuserdefaults.standarduserdefaults().valueforkey("pushnotify")) != nil { pushnotification = nsuserdefaults.standarduserdefaults().valueforkey("pushnotify") as! bool // let notificationcheck = nsuserdefaults.standarduserdefaults().valueforkey("pushnotify") as! bool if (pushnotification == true) { application.registerforremotenotifications() } else { application.unregisterforremotenotifications() } } else { application.registerforremotenotifications() } return true }
but code not getting id means myid getting nil. how can this?
i think when quit application didreceiveremotenotification method not gets called when tap on notification, instead method didfinishlaunchingwithoptions gets called there have check weather application launched notification
put below code in didfinishlaunchingwithoptions :
var notification: uilocalnotification = (launchoptions[uiapplicationlaunchoptionsremotenotificationkey] as! uilocalnotification) if notification != nil { // handle notification }
above code handling push notifications if using local notification try:
// if launched local notification var notification: uilocalnotification = (launchoptions[uiapplicationlaunchoptionslocalnotificationkey] as! uilocalnotification) if notification != nil { }
Comments
Post a Comment