AppDelegate.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import UIKit
  2. import React
  3. import React_RCTAppDelegate
  4. import ReactAppDependencyProvider
  5. import UserNotifications
  6. import FirebaseCore
  7. import GoogleMaps
  8. @main
  9. class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
  10. var window: UIWindow?
  11. var reactNativeDelegate: ReactNativeDelegate?
  12. var reactNativeFactory: RCTReactNativeFactory?
  13. func application(
  14. _ application: UIApplication,
  15. didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  16. ) -> Bool {
  17. GMSServices.provideAPIKey("AIzaSyAVzs860l2Iuu1zG80IT1Zu4w7OvbVmJ4g")
  18. FirebaseApp.configure()
  19. Messaging.messaging().delegate = self
  20. let delegate = ReactNativeDelegate()
  21. let factory = RCTReactNativeFactory(delegate: delegate)
  22. delegate.dependencyProvider = RCTAppDependencyProvider()
  23. reactNativeDelegate = delegate
  24. reactNativeFactory = factory
  25. window = UIWindow(frame: UIScreen.main.bounds)
  26. factory.startReactNative(
  27. withModuleName: "Strides",
  28. in: window,
  29. launchOptions: launchOptions
  30. )
  31. let center = UNUserNotificationCenter.current()
  32. center.delegate = self
  33. return true
  34. }
  35. // MARK: - Linking (RN RCTLinkingManager)
  36. func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
  37. return RCTLinkingManager.application(app, open: url, options: options)
  38. }
  39. func application(
  40. _ application: UIApplication,
  41. continue userActivity: NSUserActivity,
  42. restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void
  43. ) -> Bool {
  44. return RCTLinkingManager.application(
  45. application,
  46. continue: userActivity,
  47. restorationHandler: restorationHandler
  48. )
  49. }
  50. /*func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
  51. guard let fcmToken = fcmToken else { return }
  52. // 1. 组装数据字典并发送本地通知
  53. let dataDict: [String: String] = ["token": fcmToken]
  54. NotificationCenter.default.post(
  55. name: Notification.Name("FCMToken"),
  56. object: nil,
  57. userInfo: dataDict
  58. )
  59. // 2. 将字符串转换为 Data (UTF-8)
  60. let deviceToken = fcmToken.data(using: .utf8)
  61. // 3. 将 Token 传递给 React Native Push Notification 插件
  62. // 注意:确保已在 Swift 桥接头文件 (Bridging Header) 中导入了 RNCPushNotificationIOS.h
  63. RNCPushNotificationIOS.didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
  64. }*/
  65. // MARK: - Push Notifications (RNCPushNotificationIOS)
  66. // Required to register for notifications
  67. func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
  68. RNCPushNotificationIOS.didRegister(notificationSettings)
  69. }
  70. // Required for the register event
  71. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  72. // 1. 将 APNs Device Token 关联至 Firebase Messaging
  73. Messaging.messaging().apnsToken = deviceToken
  74. // 2. 获取当前的 FCM Token 并通过通知中心发送给 RN 端
  75. if let fcmToken = Messaging.messaging().fcmToken {
  76. let userInfo: [AnyHashable: Any] = ["deviceToken": fcmToken]
  77. NotificationCenter.default.post(
  78. name: Notification.Name("RemoteNotificationsRegistered"),
  79. object: self,
  80. userInfo: userInfo
  81. )
  82. }
  83. //RNCPushNotificationIOS.didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
  84. }
  85. // Required for the notification event
  86. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  87. RNCPushNotificationIOS.didReceiveRemoteNotification(userInfo, fetchCompletionHandler: completionHandler)
  88. }
  89. // Required for the registrationError event
  90. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  91. RNCPushNotificationIOS.didFailToRegisterForRemoteNotificationsWithError(error)
  92. }
  93. // iOS 10+ Required for localNotification event
  94. func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  95. RNCPushNotificationIOS.didReceive(response)
  96. completionHandler()
  97. }
  98. // Called when a notification is delivered to a foreground app
  99. func userNotificationCenter(
  100. _ center: UNUserNotificationCenter,
  101. willPresent notification: UNNotification,
  102. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
  103. ) {
  104. completionHandler([.alert, .badge, .sound])
  105. }
  106. }
  107. class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate {
  108. override func sourceURL(for bridge: RCTBridge) -> URL? {
  109. self.bundleURL()
  110. }
  111. override func bundleURL() -> URL? {
  112. #if DEBUG
  113. RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
  114. #else
  115. Bundle.main.url(forResource: "main", withExtension: "jsbundle")
  116. #endif
  117. }
  118. }