在我的项目中,我遇到了一个问题:有两种不同的通知。
其中一个需要两个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
只有一个条件。
答案 0 :(得分:2)
最后,我找到了答案并完美地解决了我的问题。我复制代码,以防有人发现我遇到的问题。
IOS允许您使用不同的通知操作,UIMutableUserNotificationCategory
let cancelCategory = UIMutableUserNotificationCategory() // notification categories allow us to create groups of actions that we can associate with a notification
cancelCategory.identifier = "Notification_Category"
cancelCategory.setActions([cancelAction], forContext: .Default) //
和
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: NSSet(array: [todoCategory,cancelCategory]) as? Set<UIUserNotificationCategory>)
方法允许您有多个通知类别。
最后一步。当你后端想推送通知。你应该构建一个关键值的一部分(&#34;类别&#34; =&gt;&#34;你的类别标识符&#34;),并且每件事都已经完成。
(body['aps'] = array('alert' => '','sound' => 'default','link_url' => '','category' => 'Notification_Category',);)