How to open particular post on the click on push notification

北慕城南 提交于 2019-12-23 05:19:12

问题


in my app i implemented push notification. when my app is in running state and if push notification come i am handle that with this 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 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)



    }

What i want :: when my app is not running and if push notification come i want to open perticular post based on that notification post id. so how can i do this? Here is my 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 by this code i am not getting id means myid is getting nil. so how can i do this?


回答1:


i think when you quit your application the didReceiveRemoteNotification method not gets called when you tap on the notification, instead the method didFinishLaunchingWithOptions gets called there you have to check weather application is launched by notification

Put below code in your didFinishLaunchingWithOptions :

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

above code is for handling the push notifications if your using local notification then try:

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

}


来源:https://stackoverflow.com/questions/36709454/how-to-open-particular-post-on-the-click-on-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!