-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pushing to certain view after Push notification received #31
Comments
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.
In our
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 |
Hello Matteo,
Thank you very much for taking the time to investigate and develop this solution.
I think this is a great approach, and I am going to try it in my app update.
And by the way….being both Italians….Grazie ancora Matteo, a presto!
Cristiano
… Il giorno 19 nov 2020, alle ore 13:18, Matteo ***@***.***> ha scritto:
Hi @HowardTheDuck007 <https://github.com/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 your App you must register this object as the notification delegate. Also, in your App 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):
Notification occurs while the app is in foreground:
<https://user-images.githubusercontent.com/5569047/99664706-e280f680-2a68-11eb-8d01-aa7666b9d72e.gif>
Notification occurs while the app is in background. The user clicks on the notification:
<https://user-images.githubusercontent.com/5569047/99665054-64711f80-2a69-11eb-8306-41cb81db05d3.gif>
You can fin the complete GIST here: https://gist.github.com/matteopuc/0317ec13b9e52a4a9f52e9a636e67569 <https://gist.github.com/matteopuc/0317ec13b9e52a4a9f52e9a636e67569>
If you need further help don't hesitate to ask and thanks for the interesting question.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#31 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AC23VA666NCFQNMQLZ2TWBLSQUEIPANCNFSM4R5QFJJQ>.
|
Hello, great library!
I was wondering if you have any suggestion or example to use it to push a view after a push notification is received from AppDelegate.
I tried to push the view like this:
self.appState.navigationStack?.push((MyView()))
But apparently it cannot be done, unless you trigger the push from a View/button.
Thank you!
The text was updated successfully, but these errors were encountered: