我正在开发一个WatchOS3
应用程序,其中用户通过自定义操作接收本地通知。用户可以在通知,选项1和选项2上调用2个自定义操作。用户点击其中任一选项后,应用应启动到特定视图。
到目前为止,ExtenionsDelegate
:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Tapped in notification")
let identifier = response.actionIdentifier
print(identifier)
switch(identifier){
case "option1":
print("tapped option1")
case "option2":
print("tapped option2")
default: break
}
completionHandler()
}
以下是我的主InterfaceController
中的代码,其中定义了通知类别:
func actioncategories() {
let option1 = UNNotificationAction(identifier: "option1", title: "Test Option 1", options: .foreground) //Button 1
let option2 = UNNotificationAction(identifier: "option2", title: "Test Option 2", options: .foreground) //Button 2
let actioncategory = UNNotificationCategory(identifier: "action_category", actions: [option1, option2], intentIdentifiers: [])
UNUserNotificationCenter.current().setNotificationCategories([actioncategory]) //setting actions & categories
}
现在,当点击option1或option2时,如何告诉我的应用程序启动到特定视图?
答案 0 :(得分:0)
我找到了解决方案:
在ExtensionsDelegate中使用 func userNotificationCenter ,而不是在主界面控制器中使用 func handleAction(withIdentifier identifier:String?,用于通知:UNNotification) p>
使用 presentController(withName:,context:)可以打开特定视图
代码(在InterfaceController中):
override func handleAction(withIdentifier identifier: String?, for notification: UNNotification) {
print("Tapped in notification")
print(identifier)
switch(identifier){
case "option1"?:
print("tapped option1")
presentController(withName: "Option1_Screen", context: "segue")
case "option2"?:
print("tapped option2")
presentController(withName: "Option2_Screen", context: "segue")
default: break
}
}