【问题标题】:How to open particular post on the click on push notification如何在点击推送通知时打开特定帖子
【发布时间】:2016-04-19 05:37:36
【问题描述】:

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

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 变得为零。那我该怎么做呢?

【问题讨论】:

  • 您的代码在应用运行时是否有效?
  • 是的,它在应用程序运行时工作@SuhasPatil

标签: ios swift push-notification swift2 ios9


【解决方案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 {

}

【讨论】:

  • 好吧,人没有使用你的代码,但你的回答给了我一个线索并解决了我的问题。感谢好友@Suhas patil
猜你喜欢
  • 2017-09-15
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
相关资源
最近更新 更多