OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:59
标题: ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller

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

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

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



Best Answer-推荐答案


要在终止状态或终止状态下点击通知打开特定的 View Controller ,您需要在应用程序委托(delegate)方法 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)
            }

关于ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805438/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4