0

我正在开发一个WatchOS3应用程序,用户可以在其中接收带有自定义操作的本地通知。用户可以在通知上调用 2 个自定义操作,即选项 1 和选项 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 时,如何告诉我的应用程序启动到特定视图?

4

1 回答 1

0

I found a solution:

  • Instead of using func userNotificationCenter in ExtensionsDelegate, use func handleAction(withIdentifier identifier: String?, for notification: UNNotification) in your main interface controller

  • with presentController(withName: , context: ) you can open a specific view

Code (in 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
    }
}
于 2016-10-16T18:07:45.307 回答