Comments (2)
from swiftui-navigation-stack.
Hi @HowardTheDuck007 your question is really interesting and, to be honest, I struggled a little bit finding a possible solution. I'll show you a simple example leveraging local notifications, but the concepts are the same.
Let's start defining an object that manages notifications and sets the right view to navigate to when a notification occurs:
enum MyViews {
case viewA
case viewB
case viewC
}
class NotificationManager: NSObject, ObservableObject {
@Published var currentViewId: MyViews?
@ViewBuilder
func currentView(for id: MyViews) -> some View {
switch id {
case .viewA:
ViewA()
case .viewB:
ViewB()
case .viewC:
ViewC()
}
}
}
extension NotificationManager: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//App is in foreground
//do whatever you want here, for example:
currentViewId = .viewB
completionHandler([.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//App is in background, user has tapped on the notification
//do whatever you want here, for example:
currentViewId = .viewB
completionHandler()
}
}
In our App
we must register this object as the notification delegate. Also, we'll create our root view with the NavigationStackView
:
@main
struct NavigationStackExampleApp: App {
private let notificationManager = NotificationManager()
init() {
UNUserNotificationCenter.current().delegate = notificationManager
}
var body: some Scene {
WindowGroup {
NavigationStackView {
ContentView()
.environmentObject(notificationManager)
}
}
}
}
ContentView
is a simple view to ask notifications permission and to schedule an example notification. It also listens for changes on the notification manager currentViewId
published property in order to know when and where to navigate:
struct ContentView: View {
@EnvironmentObject private var notificationManager: NotificationManager
@EnvironmentObject private var navStack: NavigationStack
var body: some View {
VStack {
Button("Request Permission") {
requestPermission()
}
Button("Schedule Notification") {
scheduleNotification()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onChange(of: notificationManager.currentViewId) { viewId in
guard let id = viewId else {
return
}
let viewToShow = notificationManager.currentView(for: id)
navStack.push(viewToShow)
}
}
private func requestPermission() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
//manage errors here
}
}
private func scheduleNotification() {
let content = UNMutableNotificationContent()
content.title = "Notification Title"
content.subtitle = "Notification subtitle"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}
ViewA
, ViewB
and ViewB
are just fullscreen views with a message:
struct ViewA: View {
var body: some View {
Text("VIEW A")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct ViewB: View {
var body: some View {
Text("VIEW B")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct ViewC: View {
var body: some View {
Text("VIEW C")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
If you try this simple example you'll get these two results (remember to click on Request Permission first):
You can fin the complete GIST here: https://gist.github.com/matteopuc/0317ec13b9e52a4a9f52e9a636e67569
If you need further help don't hesitate to ask and thanks for the interesting question.
from swiftui-navigation-stack.
Related Issues (20)
- Stack Reset / Jump to a completely different view HOT 2
- Transition animation not working HOT 1
- @State variables get reset after pop HOT 3
- Support for iOS 15. HOT 1
- Push doesn't work if app is backgrounded and aware of scenePhase
- Return to the same position in the previous view HOT 2
- Support for Xcode 14 and new SwiftUI API HOT 6
- Previous view completely reloaded after pop HOT 1
- Popping to a specific view
- Callback navigation sometimes doesnt work HOT 1
- iOS 16 transitions malfunctioning HOT 9
- after pop,the previous scrollview auto scroll to top? HOT 6
- Xcode 14 Compiling Issue HOT 6
- not support swipe back HOT 2
- Referencing @Environment(\.scenePhase) var scenePhase breaks navigation when moving app to background
- [Bug]: Clicking on any other window in macOS will return stack to its root
- Value not changed by @Binding
- onappear excuse only first time
- how use in List cell
- after pop,the previous scrollview auto scroll to top?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftui-navigation-stack.