gpt4 book ai didi

swift - 本地通知已推送但应用程序未打开

转载 作者:行者123 更新时间:2023-11-30 13:24:22 26 4
gpt4 key购买 nike

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

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

问题:当我单击接受时,我可以在调试中看到该函数被触发并显示 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/

关于swift - 本地通知已推送但应用程序未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400514/

26 4 0
文章推荐: javascript - 具有最大缓冲区的