gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:09:39 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com