【发布时间】:2021-02-19 19:36:44
【问题描述】:
有一个用 SwiftUI 制作的应用,Parse 用于 DB。 我在应用程序的某些部分集成了一些发送通知的云功能(例如:当有人向您发送消息时,您将收到由该云功能触发的推送通知)。 在过去的日子里,当您按下通知打开应用程序时,我一直在努力寻找如何打开特定视图。 我找到了一些解决方案,但无法让它们发挥作用。
这是我到目前为止的代码:
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 点击打开应用程序”,但如果我在 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
}
【问题讨论】:
标签: push-notification swiftui apple-push-notifications