1
在我的項目中,我遇到了一個問題:有兩個不同的通知。當有很多種通知時,我怎樣纔能有不同的UIUserNotificationSettings
其中一個需要兩個UIMutableUserNotificationActions
(確定並取消),另一個只需要一個(稍後提醒我)。
下面是代碼:
let completeAction = UIMutableUserNotificationAction()
completeAction.identifier = "OK" // the unique identifier for this action
completeAction.title = "OK" // title for the action button
completeAction.activationMode = .Background // UIUserNotificationActivationMode.Background - don't bring app to foreground
completeAction.authenticationRequired = false // don't require unlocking before performing action
completeAction.destructive = true // display action in red
let cancelAction = UIMutableUserNotificationAction()
cancelAction.identifier = "Cancel"
if #available(iOS 9.0, *) {
cancelAction.parameters = [UIUserNotificationTextInputActionButtonTitleKey : "Send"]
} else {
// Fallback on earlier versions
}
cancelAction.title = "Cancel"
if #available(iOS 9.0, *) {
cancelAction.behavior = UIUserNotificationActionBehavior.TextInput
} else {
// Fallback on earlier versions
}
cancelAction.activationMode = .Background
cancelAction.destructive = false
cancelAction.authenticationRequired = false
let todoCategory = UIMutableUserNotificationCategory() // notification categories allow us to create groups of actions that we can associate with a notification
todoCategory.identifier = "TODO_CATEGORY"
todoCategory.setActions([cancelAction, completeAction], forContext: .Minimal) // UIUserNotificationActionContext.Default (4 actions max)
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: NSSet(array: [todoCategory]) as? Set<UIUserNotificationCategory>))
但有隻有一個UIUserNotificationSettings
條件。
請勿將您的代碼作爲圖像發佈。用實際的代碼副本更新你的問題並粘貼到問題中(並正確格式化)。 – rmaddy
@rmaddy我已修復。謝謝。這是我第一次問問題。 :d – Castiel