Я ссылаюсь на код от Apple, но не работает, вот мой код:
override func viewDidLoad() {
super.viewDidLoad()
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert,.sound]) { (granted, error) in
print(granted)
}
//Set content
let content = UNMutableNotificationContent()
content.title = "Hello World"
content.body = "My First Notification App"
content.sound = UNNotificationSound.default()
//Set trigger
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
//Set request
let uuid = UUID().uuidString
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
//Set actions
let blueAction = UNNotificationAction(identifier: "ACTION_BLUE", title: "Blue", options: .init(rawValue: 0))
let greenAction = UNNotificationAction(identifier: "ACTION_GREEN", title: "Green", options: .init(rawValue: 0))
let category = UNNotificationCategory(identifier: "COLOR_ACTION", actions: [blueAction,greenAction], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "", options: .customDismissAction)
center.setNotificationCategories([category]) //category
center.add(request) { (err) in
if err != nil {
print(err)
}
}
// Do any additional setup after loading the view, typically from a nib.
}
вот UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.content.categoryIdentifier == "COLOR_ACTION" {
}
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
Я думаю, что замечать было неправильно, но это не работа. Надеюсь, что кто-то может мне помочь, я сейчас в замешательстве, смеется.