0

WatchOS3アプリケーションを開発しています。このアプリケーションでは、ユーザーがカスタムアクションでローカル通知を受け取ります。ユーザーは、通知で呼び出すことができる2つのカスタムアクション、オプション1とオプション2を持っています。ユーザーがいずれかのオプションをタップすると、アプリは特定のビューに起動するはずです。WatchOS 3のカスタム通知アクションから特定のView Controllerを開く方法

これまでのところ、通知アクションはExtenionsDelegateにこの機能が正しく処理されています

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 
} 

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私のメインのコードですオプション1またはオプション2のいずれかをタップすると、特定のビューに起動するようアプリケーションに指示するにはどうすればよいですか?

答えて

0

Iが溶液実測値:メインインタフェースコントローラ

FUNCのhandleAction(UNNotification:通知の文字列?withIdentifier識別子)を使用し、代わりExtensionsDelegateにFUNCのuserNotificationCenterを使用する

  • 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 
    } 
} 
関連する問題