当应用程序被终止或关闭时,如何在点击推送通知后打开特定控制器?

3
实际上我想在我的应用程序中做的是,我设置了一个通知闹钟,然后强制终止了应用程序。当通知到来并且我点击通知时,它只是打开应用程序而不是我想要打开的特定控制器(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以强制将视图控制器加载到内存中。 - Zhang
张先生,您能告诉我在哪里加载HoroscopeNotificationViewController吗?是在Appdelegate中吗?如果是的话,请详细说明如何加载以及代码应该怎么写。非常感谢您的帮助。 - Sirsendu Das
你不是创建这个应用的人吗?你应该知道它在哪里。按下Cmd-Shift-O打开搜索框,然后输入你的控制器的名称。 - Zhang
不好意思,我的问题是在Appdelegate中加载控制器的位置在哪里。 - Sirsendu Das
@SirsenduDas 如果这个解决方案对您有用,请接受答案!! - SachinVsSachin
4个回答

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。 - SachinVsSachin

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

0

**

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)

-1
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)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接