FIRMessaging.m 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. #if !__has_feature(objc_arc)
  17. #error FIRMessagingLib should be compiled with ARC.
  18. #endif
  19. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  20. #import <GoogleUtilities/GULAppDelegateSwizzler.h>
  21. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  22. #import <GoogleUtilities/GULReachabilityChecker.h>
  23. #import <GoogleUtilities/GULUserDefaults.h>
  24. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  25. #import "FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
  26. #import "FirebaseMessaging/Interop/FIRMessagingInterop.h"
  27. #import "FirebaseMessaging/Sources/FIRMessagingAnalytics.h"
  28. #import "FirebaseMessaging/Sources/FIRMessagingCode.h"
  29. #import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
  30. #import "FirebaseMessaging/Sources/FIRMessagingContextManagerService.h"
  31. #import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
  32. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  33. #import "FirebaseMessaging/Sources/FIRMessagingPubSub.h"
  34. #import "FirebaseMessaging/Sources/FIRMessagingRemoteNotificationsProxy.h"
  35. #import "FirebaseMessaging/Sources/FIRMessagingRmqManager.h"
  36. #import "FirebaseMessaging/Sources/FIRMessagingSyncMessageManager.h"
  37. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  38. #import "FirebaseMessaging/Sources/FIRMessaging_Private.h"
  39. #import "FirebaseMessaging/Sources/NSError+FIRMessaging.h"
  40. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessagingExtensionHelper.h"
  41. #import "FirebaseMessaging/Sources/Token/FIRMessagingAuthService.h"
  42. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.h"
  43. #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.h"
  44. #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
  45. static NSString *const kFIRMessagingMessageViaAPNSRootKey = @"aps";
  46. static NSString *const kFIRMessagingReachabilityHostname = @"www.google.com";
  47. const NSNotificationName FIRMessagingRegistrationTokenRefreshedNotification =
  48. @"com.firebase.messaging.notif.fcm-token-refreshed";
  49. NSString *const kFIRMessagingUserDefaultsKeyAutoInitEnabled =
  50. @"com.firebase.messaging.auto-init.enabled"; // Auto Init Enabled key stored in NSUserDefaults
  51. NSString *const kFIRMessagingPlistAutoInitEnabled =
  52. @"FirebaseMessagingAutoInitEnabled"; // Auto Init Enabled key stored in Info.plist
  53. NSString *const FIRMessagingErrorDomain = @"com.google.fcm";
  54. const BOOL FIRMessagingIsAPNSSyncMessage(NSDictionary *message) {
  55. if ([message[kFIRMessagingMessageViaAPNSRootKey] isKindOfClass:[NSDictionary class]]) {
  56. NSDictionary *aps = message[kFIRMessagingMessageViaAPNSRootKey];
  57. if (aps && [aps isKindOfClass:[NSDictionary class]]) {
  58. return [aps[kFIRMessagingMessageAPNSContentAvailableKey] boolValue];
  59. }
  60. }
  61. return NO;
  62. }
  63. BOOL FIRMessagingIsContextManagerMessage(NSDictionary *message) {
  64. return [FIRMessagingContextManagerService isContextManagerMessage:message];
  65. }
  66. @interface FIRMessagingMessageInfo ()
  67. @property(nonatomic, readwrite, assign) FIRMessagingMessageStatus status;
  68. @end
  69. @implementation FIRMessagingMessageInfo
  70. - (instancetype)init {
  71. FIRMessagingInvalidateInitializer();
  72. }
  73. - (instancetype)initWithStatus:(FIRMessagingMessageStatus)status {
  74. self = [super init];
  75. if (self) {
  76. _status = status;
  77. }
  78. return self;
  79. }
  80. @end
  81. @interface FIRMessaging () <GULReachabilityDelegate>
  82. // FIRApp properties
  83. @property(nonatomic, readwrite, strong) NSData *apnsTokenData;
  84. @property(nonatomic, readwrite, strong) FIRMessagingClient *client;
  85. @property(nonatomic, readwrite, strong) GULReachabilityChecker *reachability;
  86. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  87. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  88. @property(nonatomic, readwrite, strong) FIRMessagingSyncMessageManager *syncMessageManager;
  89. @property(nonatomic, readwrite, strong) GULUserDefaults *messagingUserDefaults;
  90. @property(nonatomic, readwrite, strong) FIRInstallations *installations;
  91. @property(nonatomic, readwrite, strong) FIRMessagingTokenManager *tokenManager;
  92. @property(nonatomic, readwrite, strong) FIRHeartbeatLogger *heartbeatLogger;
  93. /// Message ID's logged for analytics. This prevents us from logging the same message twice
  94. /// which can happen if the user inadvertently calls `appDidReceiveMessage` along with us
  95. /// calling it implicitly during swizzling.
  96. @property(nonatomic, readwrite, strong) NSMutableSet *loggedMessageIDs;
  97. @property(nonatomic, readwrite, strong) id<FIRAnalyticsInterop> _Nullable analytics;
  98. @end
  99. @interface FIRMessaging () <FIRMessagingInterop, FIRLibrary>
  100. @end
  101. @implementation FIRMessaging
  102. + (FIRMessaging *)messaging {
  103. FIRApp *defaultApp = [FIRApp defaultApp]; // Missing configure will be logged here.
  104. id<FIRMessagingInterop> instance = FIR_COMPONENT(FIRMessagingInterop, defaultApp.container);
  105. // We know the instance coming from the container is a FIRMessaging instance, cast it and move on.
  106. return (FIRMessaging *)instance;
  107. }
  108. + (FIRMessagingExtensionHelper *)extensionHelper {
  109. static dispatch_once_t once;
  110. static FIRMessagingExtensionHelper *extensionHelper;
  111. dispatch_once(&once, ^{
  112. extensionHelper = [[FIRMessagingExtensionHelper alloc] init];
  113. });
  114. return extensionHelper;
  115. }
  116. #pragma clang diagnostic push
  117. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  118. - (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics
  119. userDefaults:(GULUserDefaults *)defaults
  120. heartbeatLogger:(FIRHeartbeatLogger *)heartbeatLogger {
  121. #pragma clang diagnostic pop
  122. self = [super init];
  123. if (self != nil) {
  124. _loggedMessageIDs = [NSMutableSet set];
  125. _messagingUserDefaults = defaults;
  126. _analytics = analytics;
  127. _heartbeatLogger = heartbeatLogger;
  128. }
  129. return self;
  130. }
  131. - (void)dealloc {
  132. [self.reachability stop];
  133. [[NSNotificationCenter defaultCenter] removeObserver:self];
  134. [self teardown];
  135. }
  136. #pragma mark - Config
  137. + (void)load {
  138. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-fcm"];
  139. }
  140. + (nonnull NSArray<FIRComponent *> *)componentsToRegister {
  141. FIRDependency *analyticsDep = [FIRDependency dependencyWithProtocol:@protocol(FIRAnalyticsInterop)
  142. isRequired:NO];
  143. FIRComponentCreationBlock creationBlock =
  144. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  145. if (!container.app.isDefaultApp) {
  146. // Only start for the default FIRApp.
  147. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeFIRApp001,
  148. @"Firebase Messaging only works with the default app.");
  149. return nil;
  150. }
  151. // Ensure it's cached so it returns the same instance every time messaging is called.
  152. *isCacheable = YES;
  153. id<FIRAnalyticsInterop> analytics = FIR_COMPONENT(FIRAnalyticsInterop, container);
  154. #pragma clang diagnostic push
  155. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  156. FIRMessaging *messaging =
  157. [[FIRMessaging alloc] initWithAnalytics:analytics
  158. userDefaults:[GULUserDefaults standardUserDefaults]
  159. heartbeatLogger:container.app.heartbeatLogger];
  160. #pragma clang diagnostic pop
  161. [messaging start];
  162. [messaging configureMessagingWithOptions:container.app.options];
  163. [messaging configureNotificationSwizzlingIfEnabled];
  164. return messaging;
  165. };
  166. FIRComponent *messagingProvider =
  167. [FIRComponent componentWithProtocol:@protocol(FIRMessagingInterop)
  168. instantiationTiming:FIRInstantiationTimingEagerInDefaultApp
  169. dependencies:@[ analyticsDep ]
  170. creationBlock:creationBlock];
  171. return @[ messagingProvider ];
  172. }
  173. - (void)configureMessagingWithOptions:(FIROptions *)options {
  174. NSString *GCMSenderID = options.GCMSenderID;
  175. if (!GCMSenderID.length) {
  176. FIRMessagingLoggerError(kFIRMessagingMessageCodeFIRApp000,
  177. @"Firebase not set up correctly, nil or empty senderID.");
  178. [NSException raise:FIRMessagingErrorDomain
  179. format:@"Could not configure Firebase Messaging. GCMSenderID must not be nil or "
  180. @"empty."];
  181. }
  182. self.tokenManager.fcmSenderID = GCMSenderID;
  183. self.tokenManager.firebaseAppID = options.googleAppID;
  184. // FCM generates a FCM token during app start for sending push notification to device.
  185. // This is not needed for app extension except for watch.
  186. #if TARGET_OS_WATCH
  187. [self didCompleteConfigure];
  188. #else
  189. if (![GULAppEnvironmentUtil isAppExtension]) {
  190. [self didCompleteConfigure];
  191. }
  192. #endif
  193. }
  194. - (void)didCompleteConfigure {
  195. NSString *cachedToken =
  196. [self.tokenManager cachedTokenInfoWithAuthorizedEntity:self.tokenManager.fcmSenderID
  197. scope:kFIRMessagingDefaultTokenScope]
  198. .token;
  199. // When there is a cached token, do the token refresh.
  200. // Before fetching FCM token, confirm there is an APNS token to avoid validation error
  201. // This error is innocuous since we refetch after APNS token is set but it can seem alarming
  202. if (cachedToken) {
  203. // Clean up expired tokens by checking the token refresh policy.
  204. [self.installations installationIDWithCompletion:^(NSString *_Nullable identifier,
  205. NSError *_Nullable error) {
  206. if ([self.tokenManager checkTokenRefreshPolicyWithIID:identifier] && self.APNSToken) {
  207. // Default token is expired, fetch default token from server.
  208. [self retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  209. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error){
  210. }];
  211. }
  212. // Set the default FCM token, there's an issue that FIRApp configure
  213. // happens before developers able to set the delegate
  214. // Hence first token set must be happen here after listener is set
  215. // TODO(chliangGoogle) Need to investigate better solution.
  216. [self updateDefaultFCMToken:self.FCMToken];
  217. }];
  218. } else if (self.isAutoInitEnabled && self.APNSToken) {
  219. // When there is no cached token, must check auto init is enabled.
  220. // If it's disabled, don't initiate token generation/refresh.
  221. // If no cache token and auto init is enabled, fetch a token from server.
  222. [self retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  223. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error){
  224. }];
  225. }
  226. }
  227. - (void)configureNotificationSwizzlingIfEnabled {
  228. // Swizzle remote-notification-related methods (app delegate and UNUserNotificationCenter)
  229. if ([FIRMessagingRemoteNotificationsProxy canSwizzleMethods]) {
  230. NSString *docsURLString = @"https://firebase.google.com/docs/cloud-messaging/ios/client"
  231. @"#method_swizzling_in_firebase_messaging";
  232. FIRMessagingLoggerNotice(kFIRMessagingMessageCodeFIRApp000,
  233. @"FIRMessaging Remote Notifications proxy enabled, will swizzle "
  234. @"remote notification receiver handlers. If you'd prefer to manually "
  235. @"integrate Firebase Messaging, add \"%@\" to your Info.plist, "
  236. @"and set it to NO. Follow the instructions at:\n%@\nto ensure "
  237. @"proper integration.",
  238. kFIRMessagingRemoteNotificationsProxyEnabledInfoPlistKey,
  239. docsURLString);
  240. [[FIRMessagingRemoteNotificationsProxy sharedProxy] swizzleMethodsIfPossible];
  241. }
  242. }
  243. - (void)start {
  244. [self setupFileManagerSubDirectory];
  245. [self setupNotificationListeners];
  246. self.tokenManager =
  247. [[FIRMessagingTokenManager alloc] initWithHeartbeatLogger:self.heartbeatLogger];
  248. self.installations = [FIRInstallations installations];
  249. [self setupTopics];
  250. // Print the library version for logging.
  251. NSString *currentLibraryVersion = FIRFirebaseVersion();
  252. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessagingPrintLibraryVersion,
  253. @"FIRMessaging library version %@", currentLibraryVersion);
  254. NSString *hostname = kFIRMessagingReachabilityHostname;
  255. self.reachability = [[GULReachabilityChecker alloc] initWithReachabilityDelegate:self
  256. withHost:hostname];
  257. [self.reachability start];
  258. // setup FIRMessaging objects
  259. [self setupRmqManager];
  260. [self setupSyncMessageManager];
  261. }
  262. - (void)setupFileManagerSubDirectory {
  263. if (![[self class] hasSubDirectory:kFIRMessagingSubDirectoryName]) {
  264. [[self class] createSubDirectory:kFIRMessagingSubDirectoryName];
  265. }
  266. if (![[self class] hasSubDirectory:kFIRMessagingInstanceIDSubDirectoryName]) {
  267. [[self class] createSubDirectory:kFIRMessagingInstanceIDSubDirectoryName];
  268. }
  269. }
  270. - (void)setupNotificationListeners {
  271. // To prevent multiple notifications remove self as observer for all events.
  272. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  273. [center removeObserver:self];
  274. [center addObserver:self
  275. selector:@selector(defaultFCMTokenWasRefreshed:)
  276. name:kFIRMessagingRegistrationTokenRefreshNotification
  277. object:nil];
  278. }
  279. - (void)setupRmqManager {
  280. self.rmq2Manager = [[FIRMessagingRmqManager alloc] initWithDatabaseName:@"rmq2"];
  281. [self.rmq2Manager loadRmqId];
  282. }
  283. - (void)setupTopics {
  284. self.pubsub = [[FIRMessagingPubSub alloc] initWithTokenManager:self.tokenManager];
  285. }
  286. - (void)setupSyncMessageManager {
  287. self.syncMessageManager =
  288. [[FIRMessagingSyncMessageManager alloc] initWithRmqManager:self.rmq2Manager];
  289. [self.syncMessageManager removeExpiredSyncMessages];
  290. }
  291. - (void)teardown {
  292. self.pubsub = nil;
  293. self.syncMessageManager = nil;
  294. self.rmq2Manager = nil;
  295. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging001, @"Did successfully teardown");
  296. }
  297. #pragma mark - Messages
  298. - (FIRMessagingMessageInfo *)appDidReceiveMessage:(NSDictionary *)message {
  299. if (!message.count) {
  300. return [[FIRMessagingMessageInfo alloc] initWithStatus:FIRMessagingMessageStatusUnknown];
  301. }
  302. // For downstream messages that go via MCS we should strip out this key before sending
  303. // the message to the device.
  304. BOOL isOldMessage = NO;
  305. NSString *messageID = message[kFIRMessagingMessageIDKey];
  306. if (messageID.length) {
  307. [self.rmq2Manager saveS2dMessageWithRmqId:messageID];
  308. BOOL isSyncMessage = FIRMessagingIsAPNSSyncMessage(message);
  309. if (isSyncMessage) {
  310. isOldMessage = [self.syncMessageManager didReceiveAPNSSyncMessage:message];
  311. }
  312. // Prevent duplicates by keeping a cache of all the logged messages during each session.
  313. // The duplicates only happen when the 3P app calls `appDidReceiveMessage:` along with
  314. // us swizzling their implementation to call the same method implicitly.
  315. // We need to rule out the contextual message because it shares the same message ID
  316. // as the local notification it will schedule. And because it is also a APNSSync message
  317. // its duplication is already checked previously.
  318. if (!isOldMessage && !FIRMessagingIsContextManagerMessage(message)) {
  319. isOldMessage = [self.loggedMessageIDs containsObject:messageID];
  320. if (!isOldMessage) {
  321. [self.loggedMessageIDs addObject:messageID];
  322. }
  323. }
  324. }
  325. if (!isOldMessage) {
  326. [FIRMessagingAnalytics logMessage:message toAnalytics:_analytics];
  327. [self handleContextManagerMessage:message];
  328. [self handleIncomingLinkIfNeededFromMessage:message];
  329. }
  330. return [[FIRMessagingMessageInfo alloc] initWithStatus:FIRMessagingMessageStatusNew];
  331. }
  332. - (BOOL)handleContextManagerMessage:(NSDictionary *)message {
  333. if (FIRMessagingIsContextManagerMessage(message)) {
  334. return [FIRMessagingContextManagerService handleContextManagerMessage:message];
  335. }
  336. return NO;
  337. }
  338. - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message {
  339. #if TARGET_OS_IOS || TARGET_OS_TV
  340. NSURL *url = [self linkURLFromMessage:message];
  341. if (url == nil) {
  342. return;
  343. }
  344. if (![NSThread isMainThread]) {
  345. dispatch_async(dispatch_get_main_queue(), ^{
  346. [self handleIncomingLinkIfNeededFromMessage:message];
  347. });
  348. return;
  349. }
  350. UIApplication *application = [GULAppDelegateSwizzler sharedApplication];
  351. if (!application) {
  352. return;
  353. }
  354. id<UIApplicationDelegate> appDelegate = application.delegate;
  355. SEL continueUserActivitySelector = @selector(application:
  356. continueUserActivity:restorationHandler:);
  357. SEL openURLWithOptionsSelector = @selector(application:openURL:options:);
  358. SEL openURLWithSourceApplicationSelector = @selector(application:
  359. openURL:sourceApplication:annotation:);
  360. // TODO(Xcode 15): When Xcode 15 is the minimum supported Xcode version, it will be unnecessary to
  361. // check if `TARGET_OS_VISION` is defined.
  362. #if TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
  363. SEL handleOpenURLSelector = @selector(application:handleOpenURL:);
  364. #endif // TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
  365. // Due to FIRAAppDelegateProxy swizzling, this selector will most likely get chosen, whether or
  366. // not the actual application has implemented
  367. // |application:continueUserActivity:restorationHandler:|. A warning will be displayed to the user
  368. // if they haven't implemented it.
  369. if ([NSUserActivity class] != nil &&
  370. [appDelegate respondsToSelector:continueUserActivitySelector]) {
  371. NSUserActivity *userActivity =
  372. [[NSUserActivity alloc] initWithActivityType:NSUserActivityTypeBrowsingWeb];
  373. userActivity.webpageURL = url;
  374. [appDelegate application:application
  375. continueUserActivity:userActivity
  376. restorationHandler:^(NSArray *_Nullable restorableObjects){
  377. // Do nothing, as we don't support the app calling this block
  378. }];
  379. } else if ([appDelegate respondsToSelector:openURLWithOptionsSelector]) {
  380. [appDelegate application:application openURL:url options:@{}];
  381. // Similarly, |application:openURL:sourceApplication:annotation:| will also always be called,
  382. // due to the default swizzling done by FIRAAppDelegateProxy in Firebase Analytics
  383. } else if ([appDelegate respondsToSelector:openURLWithSourceApplicationSelector]) {
  384. // TODO(Xcode 15): When Xcode 15 is the minimum supported Xcode version, it will be unnecessary to
  385. // check if `TARGET_OS_VISION` is defined.
  386. #if TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
  387. #pragma clang diagnostic push
  388. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  389. [appDelegate application:application
  390. openURL:url
  391. sourceApplication:FIRMessagingAppIdentifier()
  392. annotation:@{}];
  393. #pragma clang diagnostic pop
  394. } else if ([appDelegate respondsToSelector:handleOpenURLSelector]) {
  395. #pragma clang diagnostic push
  396. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  397. [appDelegate application:application handleOpenURL:url];
  398. #pragma clang diagnostic pop
  399. #endif // TARGET_OS_IOS && (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION)
  400. }
  401. #endif // TARGET_OS_IOS || TARGET_OS_TV
  402. }
  403. - (NSURL *)linkURLFromMessage:(NSDictionary *)message {
  404. NSString *urlString = message[kFIRMessagingMessageLinkKey];
  405. if (urlString == nil || ![urlString isKindOfClass:[NSString class]] || urlString.length == 0) {
  406. return nil;
  407. }
  408. NSURL *url = [NSURL URLWithString:urlString];
  409. return url;
  410. }
  411. #pragma mark - APNS
  412. - (NSData *)APNSToken {
  413. return self.apnsTokenData;
  414. }
  415. - (void)setAPNSToken:(NSData *)APNSToken {
  416. [self setAPNSToken:APNSToken type:FIRMessagingAPNSTokenTypeUnknown];
  417. }
  418. - (void)setAPNSToken:(NSData *)apnsToken type:(FIRMessagingAPNSTokenType)type {
  419. if ([apnsToken isEqual:self.apnsTokenData]) {
  420. return;
  421. }
  422. self.apnsTokenData = apnsToken;
  423. // Notify InstanceID that APNS Token has been set.
  424. NSDictionary *userInfo = @{kFIRMessagingAPNSTokenType : @(type)};
  425. // TODO(chliang) This is sent to InstanceID in case users are still using the deprecated SDK.
  426. // Should be safe to remove once InstanceID is removed.
  427. NSNotification *notification =
  428. [NSNotification notificationWithName:kFIRMessagingAPNSTokenNotification
  429. object:[apnsToken copy]
  430. userInfo:userInfo];
  431. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  432. [self.tokenManager setAPNSToken:[apnsToken copy] withUserInfo:userInfo];
  433. }
  434. #pragma mark - FCM Token
  435. - (BOOL)isAutoInitEnabled {
  436. // Defer to the class method since we're just reading from regular userDefaults and we need to
  437. // read this from IID without instantiating the Messaging singleton.
  438. return [[self class] isAutoInitEnabledWithUserDefaults:_messagingUserDefaults];
  439. }
  440. /// Checks if Messaging auto-init is enabled in the user defaults instance passed in. This is
  441. /// exposed as a class property for IID to fetch the property without instantiating an instance of
  442. /// Messaging. Since Messaging can only be used with the default FIRApp, we can have one point of
  443. /// entry without context of which FIRApp instance is being used.
  444. /// ** THIS METHOD IS DEPENDED ON INTERNALLY BY IID USING REFLECTION. PLEASE DO NOT CHANGE THE
  445. /// SIGNATURE, AS IT WOULD BREAK AUTOINIT FUNCTIONALITY WITHIN IID. **
  446. + (BOOL)isAutoInitEnabledWithUserDefaults:(GULUserDefaults *)userDefaults {
  447. // Check storage
  448. id isAutoInitEnabledObject =
  449. [userDefaults objectForKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  450. if (isAutoInitEnabledObject) {
  451. return [isAutoInitEnabledObject boolValue];
  452. }
  453. // Check Info.plist
  454. isAutoInitEnabledObject =
  455. [[NSBundle mainBundle] objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled];
  456. if (isAutoInitEnabledObject) {
  457. return [isAutoInitEnabledObject boolValue];
  458. }
  459. // If none of above exists, we default to the global switch that comes from FIRApp.
  460. return [[FIRApp defaultApp] isDataCollectionDefaultEnabled];
  461. }
  462. - (void)setAutoInitEnabled:(BOOL)autoInitEnabled {
  463. BOOL isFCMAutoInitEnabled = [self isAutoInitEnabled];
  464. [_messagingUserDefaults setBool:autoInitEnabled
  465. forKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  466. [_messagingUserDefaults synchronize];
  467. if (!isFCMAutoInitEnabled && autoInitEnabled) {
  468. [self.tokenManager tokenAndRequestIfNotExist];
  469. }
  470. }
  471. - (NSString *)FCMToken {
  472. // Gets the current default token, and requets a new one if it doesn't exist.
  473. NSString *token = [self.tokenManager tokenAndRequestIfNotExist];
  474. return token;
  475. }
  476. - (void)tokenWithCompletion:(FIRMessagingFCMTokenFetchCompletion)completion {
  477. FIROptions *options = FIRApp.defaultApp.options;
  478. [self retrieveFCMTokenForSenderID:options.GCMSenderID completion:completion];
  479. }
  480. - (void)deleteTokenWithCompletion:(FIRMessagingDeleteFCMTokenCompletion)completion {
  481. FIROptions *options = FIRApp.defaultApp.options;
  482. [self deleteFCMTokenForSenderID:options.GCMSenderID completion:completion];
  483. }
  484. - (void)retrieveFCMTokenForSenderID:(nonnull NSString *)senderID
  485. completion:(nonnull FIRMessagingFCMTokenFetchCompletion)completion {
  486. if (!senderID.length) {
  487. NSString *description = @"Couldn't fetch token because a Sender ID was not supplied. A valid "
  488. @"Sender ID is required to fetch an FCM token";
  489. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenFetch, @"%@",
  490. description);
  491. if (completion) {
  492. NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeMissingAuthorizedEntity
  493. failureReason:description];
  494. completion(nil, error);
  495. }
  496. return;
  497. }
  498. NSDictionary *options = nil;
  499. if (self.APNSToken) {
  500. options = @{kFIRMessagingTokenOptionsAPNSKey : self.APNSToken};
  501. } else {
  502. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeAPNSTokenNotAvailableDuringTokenFetch,
  503. @"APNS device token not set before retrieving FCM Token for Sender ID "
  504. @"'%@'."
  505. @"Be sure to re-retrieve the FCM token once the APNS device token is "
  506. @"set.",
  507. senderID);
  508. }
  509. [self.tokenManager
  510. tokenWithAuthorizedEntity:senderID
  511. scope:kFIRMessagingDefaultTokenScope
  512. options:options
  513. handler:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  514. if (completion) {
  515. completion(FCMToken, error);
  516. }
  517. }];
  518. }
  519. - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
  520. completion:(nonnull FIRMessagingDeleteFCMTokenCompletion)completion {
  521. if (!senderID.length) {
  522. NSString *description = @"Couldn't delete token because a Sender ID was not supplied. A "
  523. @"valid Sender ID is required to delete an FCM token";
  524. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenDelete, @"%@",
  525. description);
  526. if (completion) {
  527. NSError *error = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidRequest
  528. failureReason:description];
  529. completion(error);
  530. }
  531. return;
  532. }
  533. FIRMessaging_WEAKIFY(self);
  534. [self.installations
  535. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  536. FIRMessaging_STRONGIFY(self);
  537. if (error) {
  538. NSError *newError = [NSError messagingErrorWithCode:kFIRMessagingErrorCodeInvalidIdentity
  539. failureReason:@"Failed to get installation ID."];
  540. completion(newError);
  541. } else {
  542. [self.tokenManager deleteTokenWithAuthorizedEntity:senderID
  543. scope:kFIRMessagingDefaultTokenScope
  544. instanceID:identifier
  545. handler:^(NSError *_Nullable error) {
  546. if (completion) {
  547. completion(error);
  548. }
  549. }];
  550. }
  551. }];
  552. }
  553. - (void)deleteDataWithCompletion:(void (^)(NSError *_Nullable))completion {
  554. FIRMessaging_WEAKIFY(self);
  555. [self.tokenManager deleteWithHandler:^(NSError *error) {
  556. FIRMessaging_STRONGIFY(self);
  557. if (error) {
  558. if (completion) {
  559. completion(error);
  560. }
  561. return;
  562. }
  563. // Only request new token if FCM auto initialization is
  564. // enabled.
  565. if ([self isAutoInitEnabled]) {
  566. // Deletion succeeds! Requesting new checkin, IID and token.
  567. [self tokenWithCompletion:^(NSString *_Nullable token, NSError *_Nullable error) {
  568. if (completion) {
  569. completion(error);
  570. }
  571. }];
  572. return;
  573. }
  574. if (completion) {
  575. completion(nil);
  576. }
  577. }];
  578. }
  579. #pragma mark - FIRMessagingDelegate helper methods
  580. - (void)setDelegate:(id<FIRMessagingDelegate>)delegate {
  581. _delegate = delegate;
  582. [self validateDelegateConformsToTokenAvailabilityMethods];
  583. }
  584. // Check if the delegate conforms to |didReceiveRegistrationToken:|
  585. // and display a warning to the developer if not.
  586. // NOTE: Once |didReceiveRegistrationToken:| can be made a required method, this
  587. // check can be removed.
  588. - (void)validateDelegateConformsToTokenAvailabilityMethods {
  589. if (self.delegate && ![self.delegate respondsToSelector:@selector(messaging:
  590. didReceiveRegistrationToken:)]) {
  591. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented,
  592. @"The object %@ does not respond to "
  593. @"-messaging:didReceiveRegistrationToken:. Please implement "
  594. @"-messaging:didReceiveRegistrationToken: to be provided with an FCM "
  595. @"token.",
  596. self.delegate.description);
  597. }
  598. }
  599. - (void)notifyRefreshedFCMToken {
  600. __weak FIRMessaging *weakSelf = self;
  601. if (![NSThread isMainThread]) {
  602. dispatch_async(dispatch_get_main_queue(), ^{
  603. [weakSelf notifyRefreshedFCMToken];
  604. });
  605. return;
  606. }
  607. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  608. [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken];
  609. }
  610. // Should always trigger the token refresh notification when the delegate method is called
  611. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  612. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification
  613. object:self.tokenManager.defaultFCMToken];
  614. }
  615. #pragma mark - Topics
  616. + (NSString *)normalizeTopic:(NSString *)topic {
  617. if (!topic.length) {
  618. return nil;
  619. }
  620. if (![FIRMessagingPubSub hasTopicsPrefix:topic]) {
  621. topic = [FIRMessagingPubSub addPrefixToTopic:topic];
  622. }
  623. if ([FIRMessagingPubSub isValidTopicWithPrefix:topic]) {
  624. return [topic copy];
  625. }
  626. return nil;
  627. }
  628. - (void)subscribeToTopic:(NSString *)topic {
  629. [self subscribeToTopic:topic completion:nil];
  630. }
  631. - (void)subscribeToTopic:(NSString *)topic
  632. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  633. if ([FIRMessagingPubSub hasTopicsPrefix:topic]) {
  634. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated,
  635. @"Format '%@' is deprecated. Only '%@' should be used in "
  636. @"subscribeToTopic.",
  637. topic, [FIRMessagingPubSub removePrefixFromTopic:topic]);
  638. }
  639. __weak FIRMessaging *weakSelf = self;
  640. [self
  641. retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  642. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  643. if (error) {
  644. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging010,
  645. @"The subscription operation failed due to an "
  646. @"error getting the FCM token: %@.",
  647. error);
  648. if (completion) {
  649. completion(error);
  650. }
  651. return;
  652. }
  653. FIRMessaging *strongSelf = weakSelf;
  654. NSString *normalizeTopic = [[strongSelf class] normalizeTopic:topic];
  655. if (normalizeTopic.length) {
  656. [strongSelf.pubsub subscribeToTopic:normalizeTopic handler:completion];
  657. return;
  658. }
  659. NSString *failureReason = [NSString
  660. stringWithFormat:@"Cannot parse topic name: '%@'. Will not subscribe.",
  661. topic];
  662. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009, @"%@",
  663. failureReason);
  664. if (completion) {
  665. completion([NSError
  666. messagingErrorWithCode:kFIRMessagingErrorCodeInvalidTopicName
  667. failureReason:failureReason]);
  668. }
  669. }];
  670. }
  671. - (void)unsubscribeFromTopic:(NSString *)topic {
  672. [self unsubscribeFromTopic:topic completion:nil];
  673. }
  674. - (void)unsubscribeFromTopic:(NSString *)topic
  675. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  676. if ([FIRMessagingPubSub hasTopicsPrefix:topic]) {
  677. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTopicFormatIsDeprecated,
  678. @"Format '%@' is deprecated. Only '%@' should be used in "
  679. @"unsubscribeFromTopic.",
  680. topic, [FIRMessagingPubSub removePrefixFromTopic:topic]);
  681. }
  682. __weak FIRMessaging *weakSelf = self;
  683. [self retrieveFCMTokenForSenderID:self.tokenManager.fcmSenderID
  684. completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
  685. if (error) {
  686. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging012,
  687. @"The unsubscription operation failed due to "
  688. @"an error getting the FCM token: %@.",
  689. error);
  690. if (completion) {
  691. completion(error);
  692. }
  693. return;
  694. }
  695. FIRMessaging *strongSelf = weakSelf;
  696. NSString *normalizeTopic = [[strongSelf class] normalizeTopic:topic];
  697. if (normalizeTopic.length) {
  698. [strongSelf.pubsub unsubscribeFromTopic:normalizeTopic
  699. handler:completion];
  700. return;
  701. }
  702. NSString *failureReason = [NSString
  703. stringWithFormat:
  704. @"Cannot parse topic name: '%@'. Will not unsubscribe.", topic];
  705. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011, @"%@",
  706. failureReason);
  707. if (completion) {
  708. completion([NSError
  709. messagingErrorWithCode:kFIRMessagingErrorCodeInvalidTopicName
  710. failureReason:failureReason]);
  711. }
  712. }];
  713. }
  714. #pragma mark - GULReachabilityDelegate
  715. - (void)reachability:(GULReachabilityChecker *)reachability
  716. statusChanged:(GULReachabilityStatus)status {
  717. [self onNetworkStatusChanged];
  718. }
  719. #pragma mark - Network
  720. - (void)onNetworkStatusChanged {
  721. if ([self isNetworkAvailable]) {
  722. [self.pubsub scheduleSync:YES];
  723. }
  724. }
  725. - (BOOL)isNetworkAvailable {
  726. GULReachabilityStatus status = self.reachability.reachabilityStatus;
  727. return (status == kGULReachabilityViaCellular || status == kGULReachabilityViaWifi);
  728. }
  729. - (FIRMessagingNetworkStatus)networkType {
  730. GULReachabilityStatus status = self.reachability.reachabilityStatus;
  731. if (![self isNetworkAvailable]) {
  732. return kFIRMessagingReachabilityNotReachable;
  733. } else if (status == kGULReachabilityViaCellular) {
  734. return kFIRMessagingReachabilityReachableViaWWAN;
  735. } else {
  736. return kFIRMessagingReachabilityReachableViaWiFi;
  737. }
  738. }
  739. #pragma mark - Notifications
  740. - (void)defaultFCMTokenWasRefreshed:(NSNotification *)notification {
  741. if (notification.object && ![notification.object isKindOfClass:[NSString class]]) {
  742. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging015,
  743. @"Invalid default FCM token type %@",
  744. NSStringFromClass([notification.object class]));
  745. return;
  746. }
  747. NSString *newToken = [(NSString *)notification.object copy];
  748. [self updateDefaultFCMToken:newToken];
  749. }
  750. - (void)updateDefaultFCMToken:(NSString *)defaultFCMToken {
  751. NSString *oldToken = self.tokenManager.defaultFCMToken;
  752. NSString *newToken = defaultFCMToken;
  753. if ([self.tokenManager hasTokenChangedFromOldToken:oldToken toNewToken:newToken]) {
  754. // Make sure to set default token first before notifying others.
  755. [self.tokenManager saveDefaultTokenInfoInKeychain:newToken];
  756. [self notifyDelegateOfFCMTokenAvailability];
  757. [self.pubsub scheduleSync:YES];
  758. }
  759. }
  760. - (void)notifyDelegateOfFCMTokenAvailability {
  761. __weak FIRMessaging *weakSelf = self;
  762. if (![NSThread isMainThread]) {
  763. dispatch_async(dispatch_get_main_queue(), ^{
  764. [weakSelf notifyDelegateOfFCMTokenAvailability];
  765. });
  766. return;
  767. }
  768. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  769. [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken];
  770. }
  771. // Should always trigger the token refresh notification when the delegate method is called
  772. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  773. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification
  774. object:self.tokenManager.defaultFCMToken];
  775. }
  776. #pragma mark - Application Support Directory
  777. + (BOOL)hasSubDirectory:(NSString *)subDirectoryName {
  778. NSString *subDirectoryPath = [self pathForSubDirectory:subDirectoryName];
  779. BOOL isDirectory;
  780. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  781. isDirectory:&isDirectory]) {
  782. return NO;
  783. } else if (!isDirectory) {
  784. return NO;
  785. }
  786. return YES;
  787. }
  788. + (NSString *)pathForSubDirectory:(NSString *)subDirectoryName {
  789. NSArray *directoryPaths =
  790. NSSearchPathForDirectoriesInDomains(FIRMessagingSupportedDirectory(), NSUserDomainMask, YES);
  791. NSString *dirPath = directoryPaths.lastObject;
  792. NSArray *components = @[ dirPath, subDirectoryName ];
  793. return [NSString pathWithComponents:components];
  794. }
  795. + (BOOL)createSubDirectory:(NSString *)subDirectoryName {
  796. NSString *subDirectoryPath = [self pathForSubDirectory:subDirectoryName];
  797. BOOL hasSubDirectory;
  798. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  799. isDirectory:&hasSubDirectory]) {
  800. NSError *error;
  801. [[NSFileManager defaultManager] createDirectoryAtPath:subDirectoryPath
  802. withIntermediateDirectories:YES
  803. attributes:nil
  804. error:&error];
  805. if (error) {
  806. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging017,
  807. @"Cannot create directory %@, error: %@", subDirectoryPath, error);
  808. return NO;
  809. }
  810. } else {
  811. if (!hasSubDirectory) {
  812. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging018,
  813. @"Found file instead of directory at %@", subDirectoryPath);
  814. return NO;
  815. }
  816. }
  817. return YES;
  818. }
  819. #pragma mark - Locales
  820. + (NSString *)currentLocale {
  821. NSArray *locales = [self firebaseLocales];
  822. NSArray *preferredLocalizations =
  823. [NSBundle preferredLocalizationsFromArray:locales
  824. forPreferences:[NSLocale preferredLanguages]];
  825. NSString *legalDocsLanguage = [preferredLocalizations firstObject];
  826. // Use en as the default language
  827. return legalDocsLanguage ? legalDocsLanguage : @"en";
  828. }
  829. + (NSArray *)firebaseLocales {
  830. NSMutableArray *locales = [NSMutableArray array];
  831. NSDictionary *localesMap = [self firebaselocalesMap];
  832. for (NSString *key in localesMap) {
  833. [locales addObjectsFromArray:localesMap[key]];
  834. }
  835. return locales;
  836. }
  837. + (NSDictionary *)firebaselocalesMap {
  838. return @{
  839. // Albanian
  840. @"sq" : @[ @"sq_AL" ],
  841. // Belarusian
  842. @"be" : @[ @"be_BY" ],
  843. // Bulgarian
  844. @"bg" : @[ @"bg_BG" ],
  845. // Catalan
  846. @"ca" : @[ @"ca", @"ca_ES" ],
  847. // Croatian
  848. @"hr" : @[ @"hr", @"hr_HR" ],
  849. // Czech
  850. @"cs" : @[ @"cs", @"cs_CZ" ],
  851. // Danish
  852. @"da" : @[ @"da", @"da_DK" ],
  853. // Estonian
  854. @"et" : @[ @"et_EE" ],
  855. // Finnish
  856. @"fi" : @[ @"fi", @"fi_FI" ],
  857. // Hebrew
  858. @"he" : @[ @"he", @"iw_IL" ],
  859. // Hindi
  860. @"hi" : @[ @"hi_IN" ],
  861. // Hungarian
  862. @"hu" : @[ @"hu", @"hu_HU" ],
  863. // Icelandic
  864. @"is" : @[ @"is_IS" ],
  865. // Indonesian
  866. @"id" : @[ @"id", @"in_ID", @"id_ID" ],
  867. // Irish
  868. @"ga" : @[ @"ga_IE" ],
  869. // Korean
  870. @"ko" : @[ @"ko", @"ko_KR", @"ko-KR" ],
  871. // Latvian
  872. @"lv" : @[ @"lv_LV" ],
  873. // Lithuanian
  874. @"lt" : @[ @"lt_LT" ],
  875. // Macedonian
  876. @"mk" : @[ @"mk_MK" ],
  877. // Malay
  878. @"ms" : @[ @"ms_MY" ],
  879. // Maltese
  880. @"mt" : @[ @"mt_MT" ],
  881. // Polish
  882. @"pl" : @[ @"pl", @"pl_PL", @"pl-PL" ],
  883. // Romanian
  884. @"ro" : @[ @"ro", @"ro_RO" ],
  885. // Russian
  886. @"ru" : @[ @"ru_RU", @"ru", @"ru_BY", @"ru_KZ", @"ru-RU" ],
  887. // Slovak
  888. @"sk" : @[ @"sk", @"sk_SK" ],
  889. // Slovenian
  890. @"sl" : @[ @"sl_SI" ],
  891. // Swedish
  892. @"sv" : @[ @"sv", @"sv_SE", @"sv-SE" ],
  893. // Turkish
  894. @"tr" : @[ @"tr", @"tr-TR", @"tr_TR" ],
  895. // Ukrainian
  896. @"uk" : @[ @"uk", @"uk_UA" ],
  897. // Vietnamese
  898. @"vi" : @[ @"vi", @"vi_VN" ],
  899. // The following are groups of locales or locales that sub-divide a
  900. // language).
  901. // Arabic
  902. @"ar" : @[
  903. @"ar", @"ar_DZ", @"ar_BH", @"ar_EG", @"ar_IQ", @"ar_JO", @"ar_KW",
  904. @"ar_LB", @"ar_LY", @"ar_MA", @"ar_OM", @"ar_QA", @"ar_SA", @"ar_SD",
  905. @"ar_SY", @"ar_TN", @"ar_AE", @"ar_YE", @"ar_GB", @"ar-IQ", @"ar_US"
  906. ],
  907. // Simplified Chinese
  908. @"zh_Hans" : @[ @"zh_CN", @"zh_SG", @"zh-Hans" ],
  909. // Traditional Chinese
  910. @"zh_Hant" : @[ @"zh_HK", @"zh_TW", @"zh-Hant", @"zh-HK", @"zh-TW" ],
  911. // Dutch
  912. @"nl" : @[ @"nl", @"nl_BE", @"nl_NL", @"nl-NL" ],
  913. // English
  914. @"en" : @[
  915. @"en", @"en_AU", @"en_CA", @"en_IN", @"en_IE", @"en_MT", @"en_NZ", @"en_PH",
  916. @"en_SG", @"en_ZA", @"en_GB", @"en_US", @"en_AE", @"en-AE", @"en_AS", @"en-AU",
  917. @"en_BD", @"en-CA", @"en_EG", @"en_ES", @"en_GB", @"en-GB", @"en_HK", @"en_ID",
  918. @"en-IN", @"en_NG", @"en-PH", @"en_PK", @"en-SG", @"en-US"
  919. ],
  920. // French
  921. @"fr" :
  922. @[ @"fr", @"fr_BE", @"fr_CA", @"fr_FR", @"fr_LU", @"fr_CH", @"fr-CA", @"fr-FR", @"fr_MA" ],
  923. // German
  924. @"de" : @[ @"de", @"de_AT", @"de_DE", @"de_LU", @"de_CH", @"de-DE" ],
  925. // Greek
  926. @"el" : @[ @"el", @"el_CY", @"el_GR" ],
  927. // Italian
  928. @"it" : @[ @"it", @"it_IT", @"it_CH", @"it-IT" ],
  929. // Japanese
  930. @"ja" : @[ @"ja", @"ja_JP", @"ja_JP_JP", @"ja-JP" ],
  931. // Norwegian
  932. @"no" : @[ @"nb", @"no_NO", @"no_NO_NY", @"nb_NO" ],
  933. // Brazilian Portuguese
  934. @"pt_BR" : @[ @"pt_BR", @"pt-BR" ],
  935. // European Portuguese
  936. @"pt_PT" : @[ @"pt", @"pt_PT", @"pt-PT" ],
  937. // Serbian
  938. @"sr" : @[ @"sr_BA", @"sr_ME", @"sr_RS", @"sr_Latn_BA", @"sr_Latn_ME", @"sr_Latn_RS" ],
  939. // European Spanish
  940. @"es_ES" : @[ @"es", @"es_ES", @"es-ES" ],
  941. // Mexican Spanish
  942. @"es_MX" : @[ @"es-MX", @"es_MX", @"es_US", @"es-US" ],
  943. // Latin American Spanish
  944. @"es_419" : @[
  945. @"es_AR", @"es_BO", @"es_CL", @"es_CO", @"es_CR", @"es_DO", @"es_EC",
  946. @"es_SV", @"es_GT", @"es_HN", @"es_NI", @"es_PA", @"es_PY", @"es_PE",
  947. @"es_PR", @"es_UY", @"es_VE", @"es-AR", @"es-CL", @"es-CO"
  948. ],
  949. // Thai
  950. @"th" : @[ @"th", @"th_TH", @"th_TH_TH" ],
  951. };
  952. }
  953. #pragma mark - Utilities used by InstanceID
  954. + (NSString *)FIRMessagingSDKVersion {
  955. return FIRFirebaseVersion();
  956. }
  957. + (NSString *)FIRMessagingSDKCurrentLocale {
  958. return [self currentLocale];
  959. }
  960. @end