func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Actions
let leterAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
leterAction.identifier = "LETER_ACTION"
leterAction.title = "Leter"
leterAction.activationMode = UIUserNotificationActivationMode.Background
leterAction.destructive = false
leterAction.authenticationRequired = true
let deleteAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
deleteAction.identifier = "DELETE_ACTION"
deleteAction.title = "Delete"
deleteAction.activationMode = UIUserNotificationActivationMode.Background
deleteAction.destructive = false
deleteAction.authenticationRequired = true
let openAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
openAction.identifier = "OPEN_ACTION"
openAction.title = "Open"
openAction.activationMode = UIUserNotificationActivationMode.Background
openAction.destructive = false
openAction.authenticationRequired = true
// category
let todoCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
todoCategory.identifier = "TODO_CATEGORY"
let taskCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
taskCategory.identifier = "TASK_CATEGORY"
let openActions:NSArray = [openAction, deleteAction]
let taskActions:NSArray = [leterAction, deleteAction]
todoCategory.setActions(openActions as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
taskCategory.setActions(taskActions as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
// NSSet of all our categories
let category1:NSSet = NSSet(objects: todoCategory)
let category2:NSSet = NSSet(objects: taskCategory)
let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound]
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: category1 as? Set<UIUserNotificationCategory>))
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: category2 as? Set<UIUserNotificationCategory>))
return true
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Actions
let leterAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
leterAction.identifier = "LETER_ACTION"
leterAction.title = "Leter"
leterAction.activationMode = UIUserNotificationActivationMode.Background
leterAction.destructive = false
leterAction.authenticationRequired = true
let deleteAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
deleteAction.identifier = "DELETE_ACTION"
deleteAction.title = "Delete"
deleteAction.activationMode = UIUserNotificationActivationMode.Background
deleteAction.destructive = false
deleteAction.authenticationRequired = true
let openAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
openAction.identifier = "OPEN_ACTION"
openAction.title = "Open"
openAction.activationMode = UIUserNotificationActivationMode.Background
openAction.destructive = false
openAction.authenticationRequired = true
// category
let todoCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
todoCategory.identifier = "TODO_CATEGORY"
let taskCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
taskCategory.identifier = "TASK_CATEGORY"
let openActions:NSArray = [openAction, deleteAction]
let taskActions:NSArray = [leterAction, deleteAction]
todoCategory.setActions(openActions as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
taskCategory.setActions(taskActions as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
// NSSet of all our categories
let category1:NSSet = NSSet(objects: todoCategory)
let category2:NSSet = NSSet(objects: taskCategory)
let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound]
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: category1 as? Set<UIUserNotificationCategory>))
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: category2 as? Set<UIUserNotificationCategory>))
return true
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Actions
let leterAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
leterAction.identifier = "LETER_ACTION"
leterAction.title = "Leter"
leterAction.activationMode = UIUserNotificationActivationMode.Background
leterAction.destructive = false
leterAction.authenticationRequired = true
let deleteAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
deleteAction.identifier = "DELETE_ACTION"
deleteAction.title = "Delete"
deleteAction.activationMode = UIUserNotificationActivationMode.Background
deleteAction.destructive = false
deleteAction.authenticationRequired = true
let openAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
openAction.identifier = "OPEN_ACTION"
openAction.title = "Open"
openAction.activationMode = UIUserNotificationActivationMode.Background
openAction.destructive = false
openAction.authenticationRequired = true
// category
let todoCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
todoCategory.identifier = "TODO_CATEGORY"
let taskCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
taskCategory.identifier = "TASK_CATEGORY"
let openActions:NSArray = [openAction, deleteAction]
let taskActions:NSArray = [leterAction, deleteAction]
todoCategory.setActions(openActions as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
taskCategory.setActions(taskActions as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
// NSSet of all our categories
let category1:NSSet = NSSet(objects: todoCategory)
let category2:NSSet = NSSet(objects: taskCategory)
let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound]
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: category1 as? Set<UIUserNotificationCategory>))
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: category2 as? Set<UIUserNotificationCategory>))
return true
}
swiftでNotification actionを 2パターン作りたい
以下のサイトを参考にして、通知をスライドしたら、アクションが表示させるアプリをSwiftで書いてみました。
SwiftのNotificationでHello, Worldして、アクションを3つ作ってみた
これを応用して、状況に応じてアクションAとアクションB、アクションAとアクションCの二つのパターン表示できるようにしたいです。この画像のように2パターンできるようにしたいです。
そう思って以下のコードを書いたのですが、これだとtaskCategoryだけしか表示されません。
2つのパターン表示できるようにするにはどうしたらいいでしょうか。お願いします。
自分が書いたコードでは、アクションはそれぞれLATER、DELETE、OPENにしてあります。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Actions
let leterAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
leterAction.identifier = "LETER_ACTION"
leterAction.title = "Leter"
leterAction.activationMode = UIUserNotificationActivationMode.Background
leterAction.destructive = false
leterAction.authenticationRequired = true
let deleteAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
deleteAction.identifier = "DELETE_ACTION"
deleteAction.title = "Delete"
deleteAction.activationMode = UIUserNotificationActivationMode.Background
deleteAction.destructive = false
deleteAction.authenticationRequired = true
let openAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
openAction.identifier = "OPEN_ACTION"
openAction.title = "Open"
openAction.activationMode = UIUserNotificationActivationMode.Background
openAction.destructive = false
openAction.authenticationRequired = true
// category
let todoCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
todoCategory.identifier = "TODO_CATEGORY"
let taskCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
taskCategory.identifier = "TASK_CATEGORY"
let openActions:NSArray = [openAction, deleteAction]
let taskActions:NSArray = [leterAction, deleteAction]
todoCategory.setActions(openActions as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
taskCategory.setActions(taskActions as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
// NSSet of all our categories
let category1:NSSet = NSSet(objects: todoCategory)
let category2:NSSet = NSSet(objects: taskCategory)
let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound]
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: category1 as? Set<UIUserNotificationCategory>))
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: category2 as? Set<UIUserNotificationCategory>))
return true
}
lang-swift