增加push功能。

This commit is contained in:
oscarz
2024-09-10 16:39:30 +08:00
parent 5b3b2f4a5f
commit 434df13a41
6 changed files with 232 additions and 7 deletions

View File

@ -8,15 +8,24 @@
import SwiftUI
import TrustDecision
import Combine
import Firebase
import FirebaseAnalytics
import FirebaseCrashlytics
@main
struct AIGrammarApp: App {
let persistenceController = PersistenceController.shared
// AppDelegate
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
let persistenceController = PersistenceController.shared
@StateObject private var appState = NofifyState()
@State private var urlToOpen: String?
init() {
// AppDelegate
appDelegate.app = self
//
setupLogging()
_ = InitApp.shared
@ -33,12 +42,68 @@ struct AIGrammarApp: App {
var body: some Scene {
WindowGroup {
AllTabView()
AllTabView(selectedTab: $appState.selectedTab, showPromotion: $appState.showPromotion, promotionMode: $appState.promotionMode)
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.environmentObject(IAPManager()) // IAPManager
.environmentObject(globalEnvironment) // IAPManager
.environmentObject(appState)
.preferredColorScheme(.light) // 使
.sheet(isPresented: $appState.showPromotion) {
switch appState.promotionMode {
case .halfScreen:
PromotionView() //
.presentationDetents([.medium])
case .fullScreen:
PromotionView() //
.edgesIgnoringSafeArea(.all)
default:
EmptyView()
}
}
.onChange(of: urlToOpen) { url in
if let url = url, let link = URL(string: url) {
UIApplication.shared.open(link)
}
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
handlePushNotification()
}
}
}
// app
func openURL(urlString: String) {
urlToOpen = urlString
}
func handlePushNotification() {
// 线
DispatchQueue.main.async {
let pushInfo = globalEnvironment.pushSettings
// tab
if pushInfo.gotoTab >= 0 && pushInfo.gotoTab < 4 {
self.appState.selectedTab = pushInfo.gotoTab
}else {
logger.info("invalid goto Tab: \(pushInfo.gotoTab)")
}
//
if pushInfo.showPage && pushInfo.page != "" {
self.appState.showPromotion = true
switch pushInfo.showMode {
case "halfScreen" :
self.appState.promotionMode = PromotionDisplayType.halfScreen
case "fullScreen" :
self.appState.promotionMode = PromotionDisplayType.fullScreen
default:
break
}
}
//
if pushInfo.showPage && pushInfo.openURL != "" {
urlToOpen = pushInfo.openURL
}
logger.info("Push Values. gotoTab: \(pushInfo.gotoTab), showPage: \(pushInfo.showPage), page: \(pushInfo.page), showMode: \(pushInfo.showMode), url: \(pushInfo.openURL)")
}
}
}