如何在点击推送通知上打开特定post

在我的应用程序中我实现了推送通知 当我的应用程序处于运行状态时,如果推送通知来了,我将使用此代码处理。

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 as String, preferredStyle: UIAlertControllerStyle.Alert) alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) // Find the presented VC... var presentedVC = self.window?.rootViewController while (presentedVC!.presentedViewController != nil) { presentedVC = presentedVC!.presentedViewController } presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil) } 

我想要的::当我的应用程序没有运行时,如果推送通知,我想根据该通知postID打开特定post。 那怎么能这样呢? 这是我的代码

 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 } 

但通过这个代码,我没有得到id意味着myid正在变为零。 那怎么能这样呢?

我认为当你退出你的应用程序时,当你点击通知时不会调用didReceiveRemoteNotification方法,而是在那里调用didFinishLaunchingWithOptions方法,你必须检查天气应用程序是否通过通知启动

将以下代码放在didFinishLaunchingWithOptions中:

 var notification: UILocalNotification = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! UILocalNotification) if notification != nil { // handle your notification } 

上面的代码用于处理推送通知,如果您使用本地通知然后尝试:

 // if launched by the local notification var notification: UILocalNotification = (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification) if notification != nil { }