您根本无法将按钮添加到动态通知界面。如果您尝试这样做,您将收到错误
非法配置:Notification界面中不支持按钮。
但是,您可以将系统按钮添加到除Dismiss
之外的其他通知按钮。设置通知中心的类别时,您可以指定要添加到通知类别的自定义UNNotificationActions
。
var categories = Set<UNNotificationCategory>()
let myCategory = UNNotificationCategory(identifier: "MyCategory", actions: [/*your custom actions go here*/], intentIdentifiers: [], options: []) //set up the actions here
categories.insert(myCategory)
center.setNotificationCategories(categories)
然后你就可以在你的UNUserNotificationCenterDelegate
方法处理与这些行动的用户交互(作为其动态通知界面上显示为正常的按钮),userNotificationCenter(_:didReceive:withCompletionHandler:)
这样的:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
switch response.actionIdentifier {
case "Ok":
print("Ok action tapped")
case "Dismiss":
print("Dismiss action tapped")
default:
break
}
completionHandler()
}