UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Handling responses to user notifications in SwiftUI

Forums > SwiftUI

Hi, I'm working on my first app in 100% SwiftUI.

The app generates a number of different types of user notification (i.e. local notifications) via UNUserNotificationCenter.

I've figured out the creation of notifications and this is working well. I've created a UserNotificationManager class as an ObservableObject which handles creation / scheduling of / changes to the notifications based on state variables in the app's various views.

The next steps are: (a) depending on the type of notification, to take the user back to the most appropriate place in the app when they tap (i.e. to open a specific view) (b) to incorporate actions in some of the notifications, so users can update selected data in the app directly from the notification

However, it isn't clear how to do this in SwiftUI.

I can find tutorials that work in Swift and UIKit assuming that a ViewController handles notification responses and conforms to the UNUserNotificationCenterDelegate protocol, but I can't find a way to make it work in SwiftUI. I've tried some things with AppDelegate (which I've set up to conform to the above protocol) and that's enabled me to show the notifications when the app is in the foreground, but I can't get it do anything beyond this. I've also tried to setup my UserNotificationManager class to confirm to the protocol but this requires creation of a load of protocol stubs and it isn't clear how to use them.

Can anyone offer some suggestions?

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.