swift2 如何打开一个特定的控制器后,点击推送通知时,应用程序被终止或杀死

0md85ypi  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(175)

实际上,我在我的应用程序中尝试做的是,我为通知设置一个警报,然后我强制终止应用程序。现在,当通知到来时,我点击它,它只会打开应用程序,而不是我想要打开的特定控制器(HoroscopeNotificationViewController),而在设置警报后,如果应用程序在后台运行,然后在轻敲通知时,特定控制器打开并工作良好。
你的帮助将非常感激1.提前感谢

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

k4aesqcs1#

要打开一个特定的视图控制器在点击通知时处于终止状态或已删除状态,你需要检查应用委托方法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)
            }
ttisahbt

ttisahbt2#

感谢大家为解决我的查询所做的努力。不要使用“NSTimer.scheduledTimerWithTimeInterval(3. 0,目标:自我,选择器:#选择器(应用程序委派.showNotification),使用者信息:无,重复:如果您有任何问题,请使用以下方法解决:nil)}”我只是简单地使用instantiateViewControllerWithIdentifier将控制器从didFinishLaunchingWithOptions推送到指定的控制器,它对我很有效。再次感谢@Sachin和@Zhang

lc8prwob

lc8prwob3#

**

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)
92dk7w1h

92dk7w1h4#

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

相关问题