AppDelegate.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import UIKit
  17. import FirebaseCommunity
  18. import UserNotifications
  19. @UIApplicationMain
  20. class AppDelegate: UIResponder, UIApplicationDelegate {
  21. var window: UIWindow?
  22. static let isWithinUnitTest: Bool = {
  23. if let testClass = NSClassFromString("XCTestCase") {
  24. return true
  25. } else {
  26. return false
  27. }
  28. }()
  29. static var hasPresentedInvalidServiceInfoPlistAlert = false
  30. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  31. guard !AppDelegate.isWithinUnitTest else {
  32. // During unit tests, we don't want to initialize Firebase, since by default we want to able
  33. // to run unit tests without requiring a non-dummy GoogleService-Info.plist file
  34. return true
  35. }
  36. guard SampleAppUtilities.appContainsRealServiceInfoPlist() else {
  37. // We can't run because the GoogleService-Info.plist file is likely the dummy file which needs
  38. // to be replaced with a real one, or somehow the file has been removed from the app bundle.
  39. // See: https://github.com/firebase/firebase-ios-sdk/
  40. // We'll present a friendly alert when the app becomes active.
  41. return true
  42. }
  43. FirebaseApp.configure()
  44. Messaging.messaging().delegate = self
  45. Messaging.messaging().shouldEstablishDirectChannel = true
  46. NotificationsController.configure()
  47. if #available(iOS 8.0, *) {
  48. // Always register for remote notifications. This will not show a prompt to the user, as by
  49. // default it will provision silent notifications. We can use UNUserNotificationCenter to
  50. // request authorization for user-facing notifications.
  51. application.registerForRemoteNotifications()
  52. } else {
  53. // iOS 7 didn't differentiate between user-facing and other notifications, so we should just
  54. // register for remote notifications
  55. NotificationsController.shared.registerForUserFacingNotificationsFor(application)
  56. }
  57. printFCMToken()
  58. return true
  59. }
  60. func printFCMToken() {
  61. if let token = Messaging.messaging().fcmToken {
  62. print("FCM Token: \(token)")
  63. } else {
  64. print("FCM Token: nil")
  65. }
  66. }
  67. func application(_ application: UIApplication,
  68. didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  69. print("APNS Token: \(deviceToken.hexByteString)")
  70. NotificationCenter.default.post(name: APNSTokenReceivedNotification, object: nil)
  71. if #available(iOS 8.0, *) {
  72. } else {
  73. // On iOS 7, receiving a device token also means our user notifications were granted, so fire
  74. // the notification to update our user notifications UI
  75. NotificationCenter.default.post(name: UserNotificationsChangedNotification, object: nil)
  76. }
  77. }
  78. func application(_ application: UIApplication,
  79. didRegister notificationSettings: UIUserNotificationSettings) {
  80. NotificationCenter.default.post(name: UserNotificationsChangedNotification, object: nil)
  81. }
  82. func applicationDidBecomeActive(_ application: UIApplication) {
  83. // If the app didn't start property due to an invalid GoogleService-Info.plist file, show an
  84. // alert to the developer.
  85. if !SampleAppUtilities.appContainsRealServiceInfoPlist() &&
  86. !AppDelegate.hasPresentedInvalidServiceInfoPlistAlert {
  87. if let vc = window?.rootViewController {
  88. SampleAppUtilities.presentAlertForInvalidServiceInfoPlistFrom(vc)
  89. AppDelegate.hasPresentedInvalidServiceInfoPlistAlert = true
  90. }
  91. }
  92. }
  93. }
  94. extension AppDelegate: MessagingDelegate {
  95. func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
  96. printFCMToken()
  97. }
  98. // Direct channel data messages are delivered here, on iOS 10.0+.
  99. // The `shouldEstablishDirectChannel` property should be be set to |true| before data messages can
  100. // arrive.
  101. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  102. // Convert to pretty-print JSON
  103. guard let data =
  104. try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options: .prettyPrinted),
  105. let prettyPrinted = String(data: data, encoding: .utf8) else {
  106. return
  107. }
  108. print("Received direct channel message:\n\(prettyPrinted)")
  109. }
  110. }