0

I'm developing a WatchOS3 application in which the user receives local notifications with custom actions. The user has 2 custom actions that he can call on the notification, option 1 and option 2. After the user taps on either of the options, the app should launch into a specific view.

So far, the notification actions are handled correctly with this function in the 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()
}

And here's the code from my main InterfaceController in which the notifications categories are defined:

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
}

Now how do I tell my application to launch into a specific view when either option1 or option2 is tapped?

Ketan P
  • 4,259
  • 3
  • 30
  • 36
Fabian
  • 1
  • 2

1 Answers1

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
    }
}
Fabian
  • 1
  • 2