Watch tutorial - Handling action buttons in local notifications willPresent and didReceive

The tutorial really should have gone to the trouble, and made code available, for handling action buttons.

Here’s how to handle the action buttons reponse in LocalNotifications.swift

extension LocalNotifications: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification
) async → UNNotificationPresentationOptions {
print(“willPresent fired from watch delegate”)
UNUserNotificationCenter.current().delegate = self

  let category = notification.request.content.categoryIdentifier
  
  // Notification received, display to user, even if app is in foreground or background
  return [.banner, .badge, .list, .sound]

}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () → Void) {
NSLog(“(#function)”)
print(“didReceive fired from watch delegate”)
let r = response

    switch response.actionIdentifier {
        case "ACCEPT_CALL_ACTION":
           print("Clicked ACCEPT_CALL_ACTION, sendMessageToFlutter")
            
           break
             
        case "DECLINE_CALL_ACTION":
          print("Clicked DECLINE_CALL_ACTION")
           break
             
        // Handle other actions…
      
         default:
            break
    }
    completionHandler()

}
}

You mention tutorial, but are you referring to a specific section of the book? Could you indicate the chapter/section?

https://www.raywenderlich.com/books/watchos-with-swiftui-by-tutorials/v1.0/chapters/6-notifications

Sorry, the code in original post was reformatted and put into sections. But both functions go in the LocalNotifications.swift file from the sample code repo’s notifications project here:

@gargoyle please see above

I’d like to re-phrase my opening paragraph in the original post, but i don’t see an edit button. The opening sounds a bit harsh, and i wanted to retract that and just say that the Watch tutorials are excellent. They take you from 0 to having a real, working watch app. Really useful.

1 Like

@transmarineras74yank compliments always welcome, of course! :smile: