SceneDelegate.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 FirebaseInstallations
  16. import FirebaseMessaging
  17. import SwiftUI
  18. import UIKit
  19. class SceneDelegate: UIResponder, UIWindowSceneDelegate, MessagingDelegate {
  20. var window: UIWindow?
  21. let identity = Identity()
  22. let settings = UserSettings()
  23. var cancellables = Set<AnyCancellable>()
  24. func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
  25. options connectionOptions: UIScene.ConnectionOptions) {
  26. let contentView = ContentView()
  27. // Choose using delegate for token refresh.
  28. if settings.shouldUseDelegateThanNotification {
  29. Messaging.messaging().delegate = self
  30. }
  31. // Use a UIHostingController as window root view controller.
  32. if let windowScene = scene as? UIWindowScene {
  33. let window = UIWindow(windowScene: windowScene)
  34. window
  35. .rootViewController = UIHostingController(rootView: contentView
  36. .environmentObject(identity)
  37. .environmentObject(settings))
  38. self.window = window
  39. window.makeKeyAndVisible()
  40. }
  41. // Choose using notification for token refresh.
  42. if !settings.shouldUseDelegateThanNotification {
  43. // Subscribe to token refresh
  44. NotificationCenter.default
  45. .publisher(for: Notification.Name.MessagingRegistrationTokenRefreshed)
  46. .map { $0.object as? String }
  47. .receive(on: RunLoop.main)
  48. .assign(to: \Identity.token, on: identity)
  49. .store(in: &cancellables)
  50. }
  51. // Subscribe to fid changes
  52. NotificationCenter.default
  53. .publisher(for: Notification.Name.InstallationIDDidChange)
  54. .receive(on: RunLoop.main)
  55. .sink(receiveValue: { _ in
  56. Installations.installations().installationID(completion: { fid, error in
  57. if let error = error as NSError? {
  58. print("Failed to get FID: ", error)
  59. return
  60. }
  61. self.identity.installationsID = fid
  62. })
  63. })
  64. .store(in: &cancellables)
  65. }
  66. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
  67. identity.token = fcmToken
  68. print("=============================\n")
  69. print("Did refresh token:\n", identity.token ?? "")
  70. print("\n=============================\n")
  71. }
  72. }