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

时间:2016-04-19 05:37:36

标签: ios swift push-notification swift2 ios9

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

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)



    }

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

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正在变为零。那我怎么能这样做呢?

1 个答案:

答案 0 :(得分:1)

  

我认为当你退出申请时   点击时不会调用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 {

}