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

时间:2017-06-28 14:29:49

标签: ios iphone swift push-notification swift2.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)
}

4 个答案:

答案 0 :(得分:1)

要在终止状态或被杀死状态下点击通知打开特定视图控制器,您需要在app委托方法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)
            }

答案 1 :(得分:0)

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

答案 2 :(得分: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)

答案 3 :(得分:-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)
}