我已经在我的应用程序中完成了本地通知的实现。

当手机处于待机状态(黑屏但未关闭)时,我会看到本地通知显示,我可以滑动并且有两个按钮(接受和拒绝)。

问题:当我单击“接受”时,我可以在调试中看到显示了print(“ babam!”)的函数已被触发,但该应用程序保持关闭状态。通常,该应用应该打开!我留在屏幕上,我需要在屏幕上滑动以解锁并提供密码。也许是因为我必须解锁...但是似乎很奇怪。

您可以检查我如何声明我的本地通知。但这似乎是正确的

declineAction.authenticationRequired = false


因此,任何帮助将不胜感激!

let acceptAction = UIMutableUserNotificationAction()
        acceptAction.identifier = "Accept"
        acceptAction.title = "Accept"
        acceptAction.activationMode = UIUserNotificationActivationMode.Background
        acceptAction.destructive = false
        acceptAction.authenticationRequired = false

        let declineAction = UIMutableUserNotificationAction()
        declineAction.identifier = "Decline"
        declineAction.title = "Decline"
        declineAction.activationMode = UIUserNotificationActivationMode.Background
        declineAction.destructive = false
        declineAction.authenticationRequired = false


        let category = UIMutableUserNotificationCategory()
        category.identifier = "invite"
        category.setActions([acceptAction, declineAction], forContext: UIUserNotificationActionContext.Default)
        let categories = NSSet(array: [category])

        let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories as? Set<UIUserNotificationCategory>)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)


就在我选择“接受”时触发的功能下方

 func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
        NSLog("Handle identifier : \(identifier)")
        // Must be called when finished
        if identifier == "Accept"{
        print("babam!!!")


            var storyboard = UIStoryboard(name: "Main", bundle: nil)

            var destinationController = storyboard.instantiateViewControllerWithIdentifier("gosondage1") as? Sondage

            var frontNavigationController = UINavigationController(rootViewController: destinationController!)

            var rearViewController = storyboard.instantiateViewControllerWithIdentifier("MenuController") as? MenuController

            var mainRevealController = SWRevealViewController()

            mainRevealController.rearViewController = rearViewController
            mainRevealController.frontViewController = frontNavigationController
            self.window!.rootViewController = mainRevealController
            self.window?.makeKeyAndVisible()


             }
        completionHandler()
    }

最佳答案

您应该将ActivationMode值从acceptAction.activationMode = UIUserNotificationActivationMode.Background更改为acceptAction.activationMode = UIUserNotificationActivationModeForeground

就你而言

acceptAction = UIMutableUserNotificationAction()
        acceptAction.identifier = "Accept"
        acceptAction.title = "Accept"
        acceptAction.activationMode = UIUserNotificationActivationModeForeground
        acceptAction.destructive = false
        acceptAction.authenticationRequired = false

        let declineAction = UIMutableUserNotificationAction()
        declineAction.identifier = "Decline"
        declineAction.title = "Decline"
        declineAction.activationMode = UIUserNotificationActivationMode.Background
        declineAction.destructive = false
        declineAction.authenticationRequired = false


了解更多信息 ;
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIUserNotificationAction_class/

08-07 21:59