FIRMessagingAPITest.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2021 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 Foundation
  15. import FirebaseCore
  16. import FirebaseMessaging
  17. import UserNotifications
  18. // This file is a build-only test for the public Messaging Swift APIs not
  19. // exercised in the integration tests.
  20. func apis() {
  21. let messaging = Messaging.messaging()
  22. let delegate = CustomDelegate()
  23. messaging.delegate = delegate
  24. messaging.isAutoInitEnabled = false
  25. messaging.token(completion: { _, _ in
  26. })
  27. messaging.deleteToken { _ in
  28. }
  29. messaging.retrieveFCMToken(forSenderID: "fakeSenderID") { _, _ in
  30. }
  31. messaging.deleteData { _ in
  32. }
  33. messaging.deleteFCMToken(forSenderID: "fakeSenderID") { _ in
  34. }
  35. if let _ = messaging.apnsToken {}
  36. let apnsToken = Data()
  37. messaging.setAPNSToken(apnsToken, type: .prod)
  38. // Use random to eliminate the warning that we're evaluating to a constant.
  39. let type: MessagingAPNSTokenType = Bool.random() ? .prod : .sandbox
  40. switch type {
  41. case .prod: ()
  42. case .sandbox: ()
  43. case .unknown: ()
  44. // The following case serves to silence the warning that this
  45. // enum "may have additional unknown values". In the event that the existing
  46. // cases change, this switch statement will generate
  47. // a "Switch must be exhaustive" warning.
  48. @unknown default: ()
  49. }
  50. // Use random to eliminate the warning that we're evaluating to a constant.
  51. let messagingError: MessagingError = Bool
  52. .random() ? MessagingError(.unknown) : MessagingError(.authentication)
  53. switch messagingError.code {
  54. case .unknown: ()
  55. case .authentication: ()
  56. case .noAccess: ()
  57. case .timeout: ()
  58. case .network: ()
  59. case .operationInProgress: ()
  60. case .invalidRequest: ()
  61. case .invalidTopicName: ()
  62. // The following case serves to silence the warning that this
  63. // enum "may have additional unknown values". In the event that the existing
  64. // cases change, this switch statement will generate
  65. // a "Switch must be exhaustive" warning.
  66. @unknown default: ()
  67. }
  68. // Use random to eliminate the warning that we're evaluating to a constant.
  69. let status: MessagingMessageStatus = Bool.random() ? .unknown : .new
  70. switch status {
  71. case .new: ()
  72. case .unknown: ()
  73. // The following case serves to silence the warning that this
  74. // enum "may have additional unknown values". In the event that the existing
  75. // cases change, this switch statement will generate
  76. // a "Switch must be exhaustive" warning.
  77. @unknown default: ()
  78. }
  79. // TODO: Mark the initializer as unavialable, as devs shouldn't be able to instantiate this.
  80. _ = MessagingMessageInfo().status
  81. NotificationCenter.default.post(name: .MessagingRegistrationTokenRefreshed, object: nil)
  82. let topic = "cat_video"
  83. messaging.subscribe(toTopic: topic)
  84. messaging.unsubscribe(fromTopic: topic)
  85. messaging.unsubscribe(fromTopic: topic, completion: { error in
  86. if let error = error {
  87. switch error {
  88. // Handle errors in the new format.
  89. case MessagingError.timeout:
  90. ()
  91. default:
  92. ()
  93. }
  94. }
  95. })
  96. messaging.unsubscribe(fromTopic: topic) { _ in
  97. }
  98. messaging.appDidReceiveMessage([:])
  99. if #available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
  100. let serviceExtension = Messaging.serviceExtension()
  101. let content = UNMutableNotificationContent()
  102. serviceExtension.populateNotificationContent(content) { content in
  103. }
  104. serviceExtension.exportDeliveryMetricsToBigQuery(withMessageInfo: [:])
  105. }
  106. }
  107. class CustomDelegate: NSObject, MessagingDelegate {
  108. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {}
  109. }
  110. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  111. func apiAsync() async throws {
  112. let messaging = Messaging.messaging()
  113. let topic = "cat_video"
  114. #if compiler(>=5.5.2) && canImport(_Concurrency)
  115. try await messaging.subscribe(toTopic: topic)
  116. try await messaging.unsubscribe(fromTopic: topic)
  117. try await messaging.token()
  118. try await messaging.retrieveFCMToken(forSenderID: "fakeSenderID")
  119. try await messaging.deleteToken()
  120. try await messaging.deleteFCMToken(forSenderID: "fakeSenderID")
  121. try await messaging.deleteData()
  122. // Test new handling of errors
  123. do {
  124. try await messaging.unsubscribe(fromTopic: topic)
  125. } catch MessagingError.timeout {
  126. } catch {}
  127. #endif
  128. }