【问题标题】:how to open a specific controller after tapping on push notification when the app is terminated or killed如何在应用程序终止或终止时点击推送通知后打开特定控制器
【发布时间】:2017-12-01 23:49:02
【问题描述】:

实际上我在我的应用程序中尝试做的是,我正在为通知设置警报,然后我强制终止应用程序。现在当通知到来并且我正在点击它时,它只是打开应用程序而不是我要打开的特定控制器(HoroscopeNotificationViewController),而在设置警报后如果应用程序在后台运行,然后点击通知特定控制器的打开并正常工作。

您的帮助将不胜感激..提前致谢

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let _:UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {

        NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(AppDelegate.showNotification), userInfo: nil, repeats: false)

let firstCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
    firstCategory.identifier = "FIRST_CATEGORY"
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    print("MessagesID : \(userInfo["gcm_message_id"]!)")
    print(userInfo)
}

func application(application: UIApplication,
                 handleActionWithIdentifier identifier:String?,
                                            forLocalNotification notification:UILocalNotification,
                                                                 completionHandler: (() -> Void))
{

    if (identifier == "FIRST_ACTION")
    {
        NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil)
    }
    else if (identifier == "SECOND_ACTION")
    {
        NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil)
    }

    completionHandler()
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    print("MessageId: \(userInfo["gcm_message_id"])")
    print(userInfo)
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    if #available(iOS 8.2, *) {
        print("Function goes here : \(notification.alertTitle)")
    } else {
        // Fallback on earlier versions
    }
    if notification.category == "FIRST_CATEGORY" {
        sleep(8)
        let horoscopeNotificationObj = window?.rootViewController?.storyboard!.instantiateViewControllerWithIdentifier("HoroscopeNotificationViewController") as! HoroscopeNotificationViewController
        window?.rootViewController?.presentViewController(horoscopeNotificationObj, animated: true, completion: nil)
        }
    }

func showNotification() {

    NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil)
}

【问题讨论】:

  • 可能视图控制器尚未加载到内存中。尝试调用 myController.view 强制将视图控制器加载到内存中,然后再尝试显示视图控制器。
  • 张,你能帮我在哪里加载 HoroscopeNotificationViewController。它在 Appdelegate 中吗?如果是,那么在哪里加载以及如何加载,如果你能帮助我详细说明代码,那么我会非常有义务。
  • 您不是创建该应用的人吗?你应该知道它在哪里。按 Cmd-Shift-O 调出搜索字段,然后输入控制器的名称。
  • 不,先生,我的查询是在 Appdelegate 中加载控制器的位置
  • @SirsenduDas 如果此解决方案适合您,请接受答案!

标签: ios iphone swift push-notification swift2.3


【解决方案1】:

要在终止状态或终止状态下点击通知打开特定的视图控制器,您需要在应用程序委托方法 didFinishLaunchingWithOptions 参数中检查启动选项对象的存在。请参考:

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

if let localNotification: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { 
    //launchOptions is not nil
//if this will not work then call this below method when application root view controller is ready to handle child view transition or just do a sec delay
                self.application(application, didReceiveLocalNotification: localNotification)
            }

【讨论】:

  • 如果此解决方案适合您,请接受答案@Sirsendu Das
【解决方案2】:

感谢大家为解决我的问题所做的努力。 而不是使用“NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(AppDelegate.showNotification), userInfo: nil, repeats: false)”和“func showNotification() {NSNotificationCenter.defaultCenter().postNotificationName(”modifyListNotification “,对象:无)}” 我只是使用 instantiateViewControllerWithIdentifier 将控制器从 didFinishLaunchingWithOptions 推送到指定的控制器,它对我有用。 再次感谢@Sachin 和@Zhang

【讨论】:

    【解决方案3】:

    **

    SWIFT 4

    **

     let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "chatView") as! ThreadViewController
    
            let threadIdToPass = JSON(notificationData)["threadId"]
            nextViewController.threadId = threadIdToPass.intValue
            print("App Delegate thread data \(threadIdToPass.intValue)")
            let navigationController = self.window?.rootViewController as! UINavigationController
    
            navigationController.show(nextViewController, sender: Any?.self)
    

    【讨论】:

      【解决方案4】:
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
      
      if let localNotifcation: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification {
      
               let NotificationInfo = (launchOptions![UIApplicationLaunchOptionsKey.localNotification])!
      
           self.application(application, didReceive: NotificationInfo as! UILocalNotification)
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-04
        • 2016-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-06
        • 2016-05-05
        • 2021-09-28
        • 1970-01-01
        相关资源
        最近更新 更多