【问题标题】:How to open a specific view controller from a custom notification action in WatchOS 3如何从 WatchOS 3 中的自定义通知操作中打开特定的视图控制器
【发布时间】:2016-10-14 17:50:36
【问题描述】:

我正在开发一个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 时,如何告诉我的应用程序启动到特定视图?

【问题讨论】:

    标签: watchkit watchos-3 wkinterfacecontroller usernotifications


    【解决方案1】:

    我找到了解决办法:

    • 不要在 ExtensionsDelegate 中使用 func userNotificationCenter,而是在主界面控制器中使用 func handleAction(withIdentifier identifier: String?, for notification: 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2017-08-13
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多