NotificationsController.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 UserNotifications
  18. import FirebaseCommunity
  19. enum NotificationsControllerAllowedNotificationType: String {
  20. case none = "None"
  21. case silent = "Silent Updates"
  22. case alert = "Alerts"
  23. case badge = "Badges"
  24. case sound = "Sounds"
  25. }
  26. let APNSTokenReceivedNotification: Notification.Name
  27. = Notification.Name(rawValue: "APNSTokenReceivedNotification")
  28. let UserNotificationsChangedNotification: Notification.Name
  29. = Notification.Name(rawValue: "UserNotificationsChangedNotification")
  30. class NotificationsController: NSObject {
  31. static let shared: NotificationsController = {
  32. let instance = NotificationsController()
  33. return instance
  34. }()
  35. class func configure() {
  36. let sharedController = NotificationsController.shared
  37. // Always become the delegate of UNUserNotificationCenter, even before we've requested user
  38. // permissions
  39. if #available(iOS 10.0, *) {
  40. UNUserNotificationCenter.current().delegate = sharedController
  41. }
  42. }
  43. func registerForUserFacingNotificationsFor(_ application: UIApplication) {
  44. if #available(iOS 10.0, *) {
  45. UNUserNotificationCenter.current()
  46. .requestAuthorization(options: [.alert, .badge, .sound],
  47. completionHandler: { (granted, error) in
  48. NotificationCenter.default.post(name: UserNotificationsChangedNotification, object: nil)
  49. })
  50. } else if #available(iOS 8.0, *) {
  51. let userNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound],
  52. categories: [])
  53. application.registerUserNotificationSettings(userNotificationSettings)
  54. } else {
  55. application.registerForRemoteNotifications(matching: [.alert, .badge, .sound])
  56. }
  57. }
  58. func getAllowedNotificationTypes(_ completion:
  59. @escaping (_ allowedTypes: [NotificationsControllerAllowedNotificationType]) -> Void) {
  60. guard Messaging.messaging().apnsToken != nil else {
  61. completion([.none])
  62. return
  63. }
  64. var types: [NotificationsControllerAllowedNotificationType] = [.silent]
  65. if #available(iOS 10.0, *) {
  66. UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { (settings) in
  67. if settings.alertSetting == .enabled {
  68. types.append(.alert)
  69. }
  70. if settings.badgeSetting == .enabled {
  71. types.append(.badge)
  72. }
  73. if settings.soundSetting == .enabled {
  74. types.append(.sound)
  75. }
  76. DispatchQueue.main.async {
  77. completion(types)
  78. }
  79. })
  80. } else if #available(iOS 8.0, *) {
  81. if let userNotificationSettings = UIApplication.shared.currentUserNotificationSettings {
  82. if userNotificationSettings.types.contains(.alert) {
  83. types.append(.alert)
  84. }
  85. if userNotificationSettings.types.contains(.badge) {
  86. types.append(.badge)
  87. }
  88. if userNotificationSettings.types.contains(.sound) {
  89. types.append(.sound)
  90. }
  91. }
  92. completion(types)
  93. } else {
  94. let enabledTypes = UIApplication.shared.enabledRemoteNotificationTypes()
  95. if enabledTypes.contains(.alert) {
  96. types.append(.alert)
  97. }
  98. if enabledTypes.contains(.badge) {
  99. types.append(.badge)
  100. }
  101. if enabledTypes.contains(.sound) {
  102. types.append(.sound)
  103. }
  104. completion(types)
  105. }
  106. }
  107. }
  108. // MARK: - UNUserNotificationCenterDelegate
  109. @available(iOS 10.0, *)
  110. extension NotificationsController: UNUserNotificationCenterDelegate {
  111. func userNotificationCenter(_ center: UNUserNotificationCenter,
  112. willPresent notification: UNNotification,
  113. withCompletionHandler completionHandler:
  114. @escaping (UNNotificationPresentationOptions) -> Void) {
  115. // Always show the incoming notification, even if the app is in foreground
  116. completionHandler([.alert, .badge, .sound])
  117. }
  118. }