Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当存在多种通知时,如何使用不同的UIUserNotificationSettings_Ios_Swift_Apple Push Notifications - Fatal编程技术网

Ios 当存在多种通知时,如何使用不同的UIUserNotificationSettings

Ios 当存在多种通知时,如何使用不同的UIUserNotificationSettings,ios,swift,apple-push-notifications,Ios,Swift,Apple Push Notifications,在我的项目中,我遇到了一个问题:有两种不同的通知 其中一个需要两个UIMutableUserNotificationActions(确定和取消),另一个只需要一个(稍后提醒我) 代码如下: let completeAction = UIMutableUserNotificationAction() completeAction.identifier = "OK" // the unique identifier for this action completeAction.title = "OK

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

其中一个需要两个
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>))
let completeAction=UIMutableUserNotificationAction()
completeAction.identifier=“OK”//此操作的唯一标识符
completeAction.title=“确定”//title用于操作按钮
completeAction.activationMode=.Background//UIUserNotificationActivationMode.Background-不要将应用程序带到前台
completeAction.authenticationRequired=false//执行操作前不需要解锁
completeAction.destructive=true//以红色显示操作
让cancelAction=UIMutableUserNotificationAction()
cancelAction.identifier=“取消”
如果可用(iOS 9.0,*){
cancelAction.parameters=[UIUserNotificationTextInputActionButtontItemKey:“发送”]
}否则{
//对早期版本的回退
}
cancelAction.title=“取消”
如果可用(iOS 9.0,*){
cancelAction.behavior=UIUserNotificationActionBehavior.TextInput
}否则{
//对早期版本的回退
}
cancelAction.activationMode=.Background
cancelAction.destructive=false
cancelAction.authenticationRequired=false
让todoCategory=UIMutableUserNotificationCategory()//通知类别允许我们创建可以与通知关联的操作组
todoCategory.identifier=“TODO\u类别”
todoCategory.setActions([cancelAction,completeAction],forContext:.Minimal)//UIUserNotificationActionContext.Default(最多4个操作)
application.registerUserNotificationSettings(UIUserNotificationSettings(for类型:[.Alert、.Badge、.Sound],类别:NSSet(数组:[ToLocateGory])as?Set))

但是,
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) //


不要将代码作为图像发布。使用实际代码副本更新您的问题,并粘贴到问题中(格式正确)。@rmaddy我已修复。谢谢。这是我第一次问问题D
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',);)