2017-08-24 75 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() 
}