AppDelegate.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 UIKit
  15. import FirebaseCore
  16. import FirebaseMessaging
  17. import FirebaseAnalytics
  18. @UIApplicationMain
  19. class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
  20. func application(_ application: UIApplication,
  21. didFinishLaunchingWithOptions launchOptions: [UIApplication
  22. .LaunchOptionsKey: Any]?) -> Bool {
  23. FirebaseApp.configure()
  24. application.delegate = self
  25. FirebaseAnalytics.Analytics.logEvent("test", parameters: nil)
  26. let center = UNUserNotificationCenter.current()
  27. center.delegate = self
  28. center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
  29. if error != nil {
  30. print("Failed requesting notification permission: ", error ?? "")
  31. }
  32. }
  33. application.registerForRemoteNotifications()
  34. return true
  35. }
  36. // Implement this to display notification when app is in foreground.
  37. func userNotificationCenter(_ center: UNUserNotificationCenter,
  38. willPresent notification: UNNotification,
  39. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
  40. -> Void) {
  41. completionHandler([.alert, .sound])
  42. }
  43. func userNotificationCenter(_ center: UNUserNotificationCenter,
  44. didReceive response: UNNotificationResponse,
  45. withCompletionHandler completionHandler: @escaping () -> Void) {
  46. completionHandler()
  47. }
  48. func application(_ application: UIApplication,
  49. didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  50. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)
  51. -> Void) {
  52. print("Hidden message arrived:\n" + userInfo.debugDescription)
  53. // Log delivery signal for data/hidden/background messages
  54. Messaging.serviceExtension().exportDeliveryMetricsToBigQuery(withMessageInfo: userInfo)
  55. completionHandler(.newData)
  56. }
  57. }