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

Posted

技术标签:

【中文标题】如何在点击推送通知时打开特定帖子【英文标题】: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 【参考方案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

以上是关于如何在点击推送通知时打开特定帖子的主要内容,如果未能解决你的问题,请参考以下文章

当用户使用 iOS 13 Swift 5 点击推送通知时,在特定视图中打开应用程序

单击推送通知时如何在我的颤振应用程序中打开特定页面

在 Android 中点击 One Signal 推送通知时如何导航到特定页面?

单击推送通知时如何打开应用程序的特定页面

在 ionic 3 中点击 One Signal 推送通知时如何导航到特定页面?

收到来自 Ionic 推送的通知时如何打开到特定状态?