ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller

标签 ios iphone swift push-notification swift2

实际上我想在我的应用程序中做的是,我为通知设置了一个警报,然后我强行终止了该应用程序。现在当通知来了并且我正在点击它时,它只是打开应用程序而不是我想打开的特定 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)
}

最佳答案

要在终止状态或终止状态下点击通知打开特定的 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/

相关文章:

ios - RestKit 与主键的关系映射

ios - 如何在每 N 个单词后将子字符串作为分隔符添加到段落

ios - 谷歌分析 ios 屏幕浏览量

iphone - startDeviceMotionUpdatesToQueue 的安全更新间隔 :withHandler:?

ios - 如何以编程方式快速切换到暗模式

iOS:将缓存策略设置为否时是否会出现任何问题?

ios - 在 iOS 中打开或关闭推送通知检查

ios - Iphone 应用程序不会每次都请求许可

swift - Realm Sandbox OS X 问题

ios - iOS 应用程序中的 Realm 文件大小