我已经写了以下通知代码
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
notification.alertBody = "Do you want to extend your checkout time
notification.alertAction = "to view"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["isExtendCheckOutTime": "true"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
通知显示正常,但当我滑动时,我想加载UIAlertViewController
并执行一些任务。
我是swift的新手,想知道在哪里编写代码以获得所需的行为。我在应用代表中尝试没有成功。
答案 0 :(得分:0)
如果我没有记错,只有当你点击actionButton时才有可能。 这样你就必须添加
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
if identifier == "CANCEL_ACTION" {
NSNotificationCenter.defaultCenter().postNotificationName("actionCancelPressed", object: nil)
}
completionHandler()
}
到AppDelegate。
使用带有UIUserNotificationAction的类别。 像:
let cancelAction1 = UIMutableUserNotificationAction()
cancelAction1.identifier = "CANCEL_ACTION"
cancelAction1.title = "cancel"
let cancelAction2 = UIMutableUserNotificationAction()
cancelAction2.identifier = "CANCEL_ACTION"
cancelAction2.title = "cancel"
cancelAction2.activationMode = .Background
cancelAction2.destructive = true
cancelAction2.authenticationRequired = false
let openAction = UIMutableUserNotificationAction()
openAction.identifier = "OPEN_ACTION"
openAction.title = "open"
let actions1 = [cancelAction1, openAction]
let actions2 = [cancelAction2, openAction]
let firstCategory = UIMutableUserNotificationCategory()
firstCategory.identifier = "FIRST_CATEGORY"
firstCategory.setActions(actions1, forContext: .Minimal)
let secondCategory = UIMutableUserNotificationCategory()
secondCategory.identifier = "SECOND_CATEGORY"
secondCategory.setActions(actions2, forContext: .Minimal)
let set: Set<UIUserNotificationCategory> = [firstCategory, secondCategory]
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories:set)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
然后
notification.category = "FIRST_CATEGORY"
否则你必须自己抓住这个动作。 但我错了!)