import UIKit import React import React_RCTAppDelegate import ReactAppDependencyProvider import UserNotifications import FirebaseCore import GoogleMaps @main class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate { var window: UIWindow? var reactNativeDelegate: ReactNativeDelegate? var reactNativeFactory: RCTReactNativeFactory? func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil ) -> Bool { GMSServices.provideAPIKey("AIzaSyAVzs860l2Iuu1zG80IT1Zu4w7OvbVmJ4g") FirebaseApp.configure() Messaging.messaging().delegate = self let delegate = ReactNativeDelegate() let factory = RCTReactNativeFactory(delegate: delegate) delegate.dependencyProvider = RCTAppDependencyProvider() reactNativeDelegate = delegate reactNativeFactory = factory window = UIWindow(frame: UIScreen.main.bounds) factory.startReactNative( withModuleName: "Strides", in: window, launchOptions: launchOptions ) let center = UNUserNotificationCenter.current() center.delegate = self return true } // MARK: - Linking (RN RCTLinkingManager) func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { return RCTLinkingManager.application(app, open: url, options: options) } func application( _ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void ) -> Bool { return RCTLinkingManager.application( application, continue: userActivity, restorationHandler: restorationHandler ) } /*func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { guard let fcmToken = fcmToken else { return } // 1. 组装数据字典并发送本地通知 let dataDict: [String: String] = ["token": fcmToken] NotificationCenter.default.post( name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict ) // 2. 将字符串转换为 Data (UTF-8) let deviceToken = fcmToken.data(using: .utf8) // 3. 将 Token 传递给 React Native Push Notification 插件 // 注意:确保已在 Swift 桥接头文件 (Bridging Header) 中导入了 RNCPushNotificationIOS.h RNCPushNotificationIOS.didRegisterForRemoteNotifications(withDeviceToken: deviceToken) }*/ // MARK: - Push Notifications (RNCPushNotificationIOS) // Required to register for notifications func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { RNCPushNotificationIOS.didRegister(notificationSettings) } // Required for the register event func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // 1. 将 APNs Device Token 关联至 Firebase Messaging Messaging.messaging().apnsToken = deviceToken // 2. 获取当前的 FCM Token 并通过通知中心发送给 RN 端 if let fcmToken = Messaging.messaging().fcmToken { let userInfo: [AnyHashable: Any] = ["deviceToken": fcmToken] NotificationCenter.default.post( name: Notification.Name("RemoteNotificationsRegistered"), object: self, userInfo: userInfo ) } //RNCPushNotificationIOS.didRegisterForRemoteNotifications(withDeviceToken: deviceToken) } // Required for the notification event func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { RNCPushNotificationIOS.didReceiveRemoteNotification(userInfo, fetchCompletionHandler: completionHandler) } // Required for the registrationError event func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { RNCPushNotificationIOS.didFailToRegisterForRemoteNotificationsWithError(error) } // iOS 10+ Required for localNotification event func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { RNCPushNotificationIOS.didReceive(response) completionHandler() } // Called when a notification is delivered to a foreground app func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void ) { completionHandler([.alert, .badge, .sound]) } } class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate { override func sourceURL(for bridge: RCTBridge) -> URL? { self.bundleURL() } override func bundleURL() -> URL? { #if DEBUG RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index") #else Bundle.main.url(forResource: "main", withExtension: "jsbundle") #endif } }