已推送本地通知但应用未打开
Local notification is pushed but app doesn't open
我已经在我的应用中实现了本地通知。
当 phone 处于待机状态(黑屏但未关闭)时,我看到本地通知显示,我可以滑动并且有两个按钮(接受和拒绝)。
问题:当我点击“接受”时,我可以在调试中看到该功能被触发并显示了 print("babam!"),但应用程序保持关闭状态。 Normaly,应用程序应该打开!我停留在屏幕上,需要滑动才能解锁并输入密码。也许吧,因为我必须解锁...但它看起来很奇怪。
您可以查看我是如何声明本地通知的。但是
似乎是正确的
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)
就在我选择 "accept"
时触发的 func 下面
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
我已经在我的应用中实现了本地通知。
当 phone 处于待机状态(黑屏但未关闭)时,我看到本地通知显示,我可以滑动并且有两个按钮(接受和拒绝)。
问题:当我点击“接受”时,我可以在调试中看到该功能被触发并显示了 print("babam!"),但应用程序保持关闭状态。 Normaly,应用程序应该打开!我停留在屏幕上,需要滑动才能解锁并输入密码。也许吧,因为我必须解锁...但它看起来很奇怪。
您可以查看我是如何声明本地通知的。但是
似乎是正确的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)
就在我选择 "accept"
时触发的 func 下面 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