2017-08-24 77 views
0

我正在创建自定义动态通知以显示用户。一旦我收到此通知didReceiveNotification功能NotificationController我设置接口插座与正确的数据。我的问题是,我不知道我怎样才能添加默认以上的自定义按钮驳回按钮,因为通知故事板犯规让按钮插入和Apple Documentation将按钮添加到动态通知界面

不包括按钮,开关,或其它交互式控件。

但是我看到很多具有自定义操作的手表应用程序,如Messages和Facebook Messenger。有什么方法可以将自定义操作添加到watchOS的动态界面?

回答

1

您根本无法将按钮添加到动态通知界面。如果您尝试这样做,您将收到错误

非法配置: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() 
}