NotificationsController.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 FirebaseMessaging
  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
  49. .post(name: UserNotificationsChangedNotification, object: nil)
  50. })
  51. } else if #available(iOS 8.0, *) {
  52. let userNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound],
  53. categories: [])
  54. application.registerUserNotificationSettings(userNotificationSettings)
  55. } else {
  56. application.registerForRemoteNotifications(matching: [.alert, .badge, .sound])
  57. }
  58. }
  59. func getAllowedNotificationTypes(_ completion:
  60. @escaping (_ allowedTypes: [NotificationsControllerAllowedNotificationType]) -> Void) {
  61. guard Messaging.messaging().apnsToken != nil else {
  62. completion([.none])
  63. return
  64. }
  65. var types: [NotificationsControllerAllowedNotificationType] = [.silent]
  66. if #available(iOS 10.0, *) {
  67. UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { settings in
  68. if settings.alertSetting == .enabled {
  69. types.append(.alert)
  70. }
  71. if settings.badgeSetting == .enabled {
  72. types.append(.badge)
  73. }
  74. if settings.soundSetting == .enabled {
  75. types.append(.sound)
  76. }
  77. DispatchQueue.main.async {
  78. completion(types)
  79. }
  80. })
  81. } else if #available(iOS 8.0, *) {
  82. if let userNotificationSettings = UIApplication.shared.currentUserNotificationSettings {
  83. if userNotificationSettings.types.contains(.alert) {
  84. types.append(.alert)
  85. }
  86. if userNotificationSettings.types.contains(.badge) {
  87. types.append(.badge)
  88. }
  89. if userNotificationSettings.types.contains(.sound) {
  90. types.append(.sound)
  91. }
  92. }
  93. completion(types)
  94. } else {
  95. let enabledTypes = UIApplication.shared.enabledRemoteNotificationTypes()
  96. if enabledTypes.contains(.alert) {
  97. types.append(.alert)
  98. }
  99. if enabledTypes.contains(.badge) {
  100. types.append(.badge)
  101. }
  102. if enabledTypes.contains(.sound) {
  103. types.append(.sound)
  104. }
  105. completion(types)
  106. }
  107. }
  108. }
  109. // MARK: - UNUserNotificationCenterDelegate
  110. @available(iOS 10.0, *)
  111. extension NotificationsController: UNUserNotificationCenterDelegate {
  112. func userNotificationCenter(_ center: UNUserNotificationCenter,
  113. willPresent notification: UNNotification,
  114. withCompletionHandler completionHandler:
  115. @escaping (UNNotificationPresentationOptions) -> Void) {
  116. // Always show the incoming notification, even if the app is in foreground
  117. print("Received notification in foreground:")
  118. let jsonString = notification.request.content.userInfo.jsonString ?? "{}"
  119. print("\(jsonString)")
  120. completionHandler([.alert, .badge, .sound])
  121. }
  122. func userNotificationCenter(_ center: UNUserNotificationCenter,
  123. didReceive response: UNNotificationResponse,
  124. withCompletionHandler completionHandler: @escaping () -> Void) {
  125. print("Received notification response")
  126. let jsonString = response.notification.request.content.userInfo.jsonString ?? "{}"
  127. print("\(jsonString)")
  128. completionHandler()
  129. }
  130. }