动态通知界面添加按钮
Add buttons to dynamic notification interface
我正在创建自定义动态通知以显示给用户。一旦我在 NotificationController
的 didReceiveNotification
函数中收到此通知,我就会使用正确的数据设置接口插座。我的问题是我没有意识到如何在默认关闭按钮上方添加自定义按钮,因为通知故事板不允许插入按钮并且 Apple Documentation 说
Do not include buttons, switches, or other interactive controls.
但我看到很多手表应用程序都有自己的自定义操作,例如消息和 Facebook Messenger。有什么方法可以将自定义操作添加到 watchOS 的动态界面?
您根本无法将按钮添加到动态通知界面。如果您尝试这样做,您将收到错误
Illegal Configuration: Buttons are not supported in Notification interfaces.
但是,除了 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()
}
我正在创建自定义动态通知以显示给用户。一旦我在 NotificationController
的 didReceiveNotification
函数中收到此通知,我就会使用正确的数据设置接口插座。我的问题是我没有意识到如何在默认关闭按钮上方添加自定义按钮,因为通知故事板不允许插入按钮并且 Apple Documentation 说
Do not include buttons, switches, or other interactive controls.
但我看到很多手表应用程序都有自己的自定义操作,例如消息和 Facebook Messenger。有什么方法可以将自定义操作添加到 watchOS 的动态界面?
您根本无法将按钮添加到动态通知界面。如果您尝试这样做,您将收到错误
Illegal Configuration: Buttons are not supported in Notification interfaces.
但是,除了 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()
}