SceneDelegate.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Combine
  15. import UIKit
  16. import SwiftUI
  17. import FirebaseInstanceID
  18. import FirebaseMessaging
  19. import FirebaseInstallations
  20. class SceneDelegate: UIResponder, UIWindowSceneDelegate, MessagingDelegate {
  21. var window: UIWindow?
  22. let identity = Identity()
  23. let settings = UserSettings()
  24. var cancellables = Set<AnyCancellable>()
  25. func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
  26. options connectionOptions: UIScene.ConnectionOptions) {
  27. let contentView = ContentView()
  28. // Choose using delegate for token refresh.
  29. if settings.shouldUseDelegateThanNotification {
  30. Messaging.messaging().delegate = self
  31. }
  32. // Use a UIHostingController as window root view controller.
  33. if let windowScene = scene as? UIWindowScene {
  34. let window = UIWindow(windowScene: windowScene)
  35. window
  36. .rootViewController = UIHostingController(rootView: contentView
  37. .environmentObject(identity)
  38. .environmentObject(settings))
  39. self.window = window
  40. window.makeKeyAndVisible()
  41. }
  42. // Choose using notification for token refresh.
  43. if !settings.shouldUseDelegateThanNotification {
  44. // Subscribe to token refresh
  45. NotificationCenter.default
  46. .publisher(for: Notification.Name.MessagingRegistrationTokenRefreshed)
  47. .map { $0.object as? String }
  48. .receive(on: RunLoop.main)
  49. .assign(to: \Identity.token, on: identity)
  50. .store(in: &cancellables)
  51. }
  52. // Subscribe to fid changes
  53. NotificationCenter.default
  54. .publisher(for: Notification.Name.InstallationIDDidChange)
  55. .receive(on: RunLoop.main)
  56. .sink(receiveValue: { _ in
  57. Installations.installations().installationID(completion: { fid, error in
  58. if let error = error as NSError? {
  59. print("Failed to get FID: ", error)
  60. return
  61. }
  62. self.identity.instanceID = fid
  63. })
  64. })
  65. .store(in: &cancellables)
  66. }
  67. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
  68. identity.token = fcmToken
  69. print("=============================\n")
  70. print("Did refresh token:\n", identity.token ?? "")
  71. print("\n=============================\n")
  72. }
  73. }