Push notification SwiftUI-当用户打开推送通知时打开特定视图
A有一个用SwiftUI制作的应用程序,解析用于DB。 我是应用程序的一部分,我集成了一些发送通知的云功能(例如:当有人向您发送消息时,您将收到由该云功能触发的推送通知)。 在过去的几天里,我一直在努力寻找当你按下通知打开应用程序时如何打开特定视图。 我已经找到了一些解决方案,但无法让它们发挥作用 这是我目前掌握的代码:Push notification SwiftUI-当用户打开推送通知时打开特定视图,push-notification,swiftui,apple-push-notifications,Push Notification,Swiftui,Apple Push Notifications,A有一个用SwiftUI制作的应用程序,解析用于DB。 我是应用程序的一部分,我集成了一些发送通知的云功能(例如:当有人向您发送消息时,您将收到由该云功能触发的推送通知)。 在过去的几天里,我一直在努力寻找当你按下通知打开应用程序时如何打开特定视图。 我已经找到了一些解决方案,但无法让它们发挥作用 这是我目前掌握的代码: class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
//Parse Intialization
...
//notifications
registerForPushNotifications()
//Notification Badge
UIApplication.shared.applicationIconBadgeNumber = 0
// start notification while app is in Foreground
UNUserNotificationCenter.current().delegate = self
return true
}
// This function will be called right after user tap on the notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("app opened from PushNotification tap")
UIApplication.shared.applicationIconBadgeNumber = 0
completionHandler()
}
}
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView(currentTab: Tab.home)
}
}
}
应用程序打印“从PushNotification tap打开的应用程序”,但如果我在AppDelegate中放入一个变量,并使用该变量的.onReceive或.onChange侦听ContentView中的更改,则不会出现任何结果
struct ContentView: View {
@ObservedObject var appState = AppState()
@State var currentTab : Tab
@State var noReloadAddItemView = false
var body: some View {
TabView(selection: $appState.currentTab) {
NavigationView {
HomeView(appState: appState, noReloadAddItemView: $noReloadAddItemView)
}
.tabItem {
if appState.currentTab == .home {
Image(systemName: "house.fill")
} else {
Image(systemName: "house")
}
Text(LocalizedStringKey("HomeTabMenu"))
}.tag(Tab.home)
NavigationView {
SearchView(appState: appState, noReloadAddItemView: $noReloadAddItemView)
}
.tabItem {
if appState.currentTab == .search {
Image(systemName: "magnifyingglass.circle.fill")
} else {
Image(systemName: "magnifyingglass")
}
Text(LocalizedStringKey("SearchTabMenu"))
}.tag(Tab.search)
NavigationView {
AddItemView(appState: appState, noReloadAddItemView: $noReloadAddItemView)
}
.tabItem {
if appState.currentTab == .add {
Image(systemName: "plus.circle.fill")
} else {
Image(systemName: "plus.circle")
}
Text(LocalizedStringKey("SellTabMenu"))
}.tag(Tab.add)
NavigationView {
ShoppingCartFavoritesView(appState: appState, noReloadAddItemView: $noReloadAddItemView)
}
.tabItem {
if appState.currentTab == .favorites {
Image(systemName: "cart.fill")
} else {
Image(systemName: "cart")
}
Text(LocalizedStringKey("CartTabMenu"))
}.tag(Tab.favorites)
NavigationView {
ProfileView(appState: appState, noReloadAddItemView: $noReloadAddItemView)
}
.tabItem {
if appState.currentTab == .profile {
Image(systemName: "person.fill")
} else {
Image(systemName: "person")
}
Text(LocalizedStringKey("ProfileTabMenu"))
}.tag(Tab.profile)
}
.accentColor(Color("ColorMainDark"))
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(currentTab: Tab.home)
}
}
class AppState: ObservableObject {
@Published var currentTab : Tab = .home
}
enum Tab {
case home, search, add, favorites, profile
}
您需要某种共享状态,您可以修改SwiftUI知道如何应对。
可观察对象
非常适合:
class AppState: ObservableObject {
static let shared = AppState()
@Published var pageToNavigationTo : String?
}
然后,为了倾听并回应它,您可以在主视图中使用两种不同的方法
选项1——基于ObservedObject值的NavigationLink绑定:
struct ContentView : View {
@ObservedObject var appState = AppState.shared //<-- note this
@State var navigate = false
var pushNavigationBinding : Binding<Bool> {
.init { () -> Bool in
appState.pageToNavigationTo != nil
} set: { (newValue) in
if !newValue { appState.pageToNavigationTo = nil }
}
}
var body: some View {
NavigationView {
Text("My content")
.overlay(NavigationLink(destination: Dest(message: appState.pageToNavigationTo ?? ""),
isActive: pushNavigationBinding) {
EmptyView()
})
}
}
}
struct Dest : View {
var message : String
var body: some View {
Text("\(message)")
}
}
我将把特定NavigationView、NavigationLink、TabView等的实现细节留给您,但这应该可以让您开始
最后,一个模拟通知并显示导航视图如何:
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
print("Dispatch")
AppState.shared.pageToNavigationTo = "test"
}
return true
}
}
class AppState: ObservableObject {
static let shared = AppState()
@Published var pageToNavigationTo : String?
}
@main
struct MultiWindowApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView : View {
@ObservedObject var appState = AppState.shared
@State var navigate = false
var pushNavigationBinding : Binding<Bool> {
.init { () -> Bool in
appState.pageToNavigationTo != nil
} set: { (newValue) in
if !newValue { appState.pageToNavigationTo = nil }
}
}
var body: some View {
NavigationView {
Text("My content")
.overlay(NavigationLink(destination: Dest(message: appState.pageToNavigationTo ?? ""),
isActive: pushNavigationBinding) {
EmptyView()
})
}
}
}
struct Dest : View {
var message : String
var body: some View {
Text("\(message)")
}
}
类AppDelegate:NSObject、UIApplicationDelegate、UnuseNotificationCenterDelegate{
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions:Any]?=nil)->Bool{
DispatchQueue.main.asyncAfter(截止日期:.now()+2){
打印(“发送”)
AppState.shared.pageToNavigationTo=“测试”
}
返回真值
}
}
类AppState:ObservableObject{
静态let shared=AppState()
@已发布变量pageToNavigationTo:字符串?
}
@主要
结构多窗口应用程序:应用程序{
@UIApplicationDelegateAdaptor(AppDelegate.self)变量AppDelegate
var body:一些场景{
窗口组{
ContentView()
}
}
}
结构ContentView:View{
@ObservedObject var appState=appState.shared
@状态变量导航=false
var pushNavigationBinding:绑定{
.init{()->Bool-in
appState.pageToNavigationTo!=无
}集合:{(newValue)在
如果!newValue{appState.pageToNavigationTo=nil}
}
}
var body:一些观点{
导航视图{
文本(“我的内容”)
.overlay(导航链接(目的地:Dest(消息:appState.pageToNavigationTo??),
isActive:pushNavigationBinding){
EmptyView()
})
}
}
}
结构目标:视图{
var消息:字符串
var body:一些观点{
文本(“\(消息)”)
}
}
谢谢。它正在工作:)
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
print("Dispatch")
AppState.shared.pageToNavigationTo = "test"
}
return true
}
}
class AppState: ObservableObject {
static let shared = AppState()
@Published var pageToNavigationTo : String?
}
@main
struct MultiWindowApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView : View {
@ObservedObject var appState = AppState.shared
@State var navigate = false
var pushNavigationBinding : Binding<Bool> {
.init { () -> Bool in
appState.pageToNavigationTo != nil
} set: { (newValue) in
if !newValue { appState.pageToNavigationTo = nil }
}
}
var body: some View {
NavigationView {
Text("My content")
.overlay(NavigationLink(destination: Dest(message: appState.pageToNavigationTo ?? ""),
isActive: pushNavigationBinding) {
EmptyView()
})
}
}
}
struct Dest : View {
var message : String
var body: some View {
Text("\(message)")
}
}