FIRMessaging.m 43 KB

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