ios - 当有多种通知时,我如何拥有不同的 UIUserNotificationSettings

标签 ios swift apple-push-notifications

在我的项目中我遇到了一个问题:有两个不同的通知。

其中一个需要两个 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只有一个条件。

最佳答案

终于,我找到了答案,完美解决了我的问题。我将代码复制到此处,以防有人发现与我相同的问题。

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>)

方法允许您有多个通知类别。

最后一步。当你后端想要推送通知时。您只需要构建键值的一部分(“类别”=>“您的类别标识符”),一切都已完成。

 (body['aps'] = array('alert' => '','sound' => 'default','link_url' => '','category' => 'Notification_Category',);)

关于ios - 当有多种通知时,我如何拥有不同的 UIUserNotificationSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36749270/

相关文章:

ios - CloudKit 未返回最新数据

ios - TableView 单元格在滚动时消失

ios - 如何使用 WKScriptMessageHandler 捕获发送到父元素的 postmessage?

iOS 8 和 segues 抛出意外异常

ios - FCM/APNS 集成对于新 token 可以正常工作,但在转换旧 APNS token 时就不行了?

ios - 如何让uislider像apple music一样

ios - 自定义标签单元格的 didSelectRowAtIndexPath

ios - 如何处理 View Controller 上的远程通知(而不是 AppDelegate)

iOS 应用程序看不到来自框架的公共(public)协议(protocol)

ios - 推送通知不工作