FIRMessaging.m 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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 "FIRMessaging.h"
  20. #import "FIRMessaging_Private.h"
  21. #import <UIKit/UIKit.h>
  22. #import "FIRMessagingClient.h"
  23. #import "FIRMessagingConstants.h"
  24. #import "FIRMessagingContextManagerService.h"
  25. #import "FIRMessagingDataMessageManager.h"
  26. #import "FIRMessagingDefines.h"
  27. #import "FIRMessagingLogger.h"
  28. #import "FIRMessagingPubSub.h"
  29. #import "FIRMessagingReceiver.h"
  30. #import "FIRMessagingRmqManager.h"
  31. #import "FIRMessagingSyncMessageManager.h"
  32. #import "FIRMessagingUtilities.h"
  33. #import "FIRMessagingVersionUtilities.h"
  34. #import <FirebaseCore/FIRReachabilityChecker.h>
  35. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  36. #import "NSError+FIRMessaging.h"
  37. static NSString *const kFIRMessagingMessageViaAPNSRootKey = @"aps";
  38. static NSString *const kFIRMessagingReachabilityHostname = @"www.google.com";
  39. static NSString *const kFIRMessagingDefaultTokenScope = @"*";
  40. static NSString *const kFIRMessagingFCMTokenFetchAPNSOption = @"apns_token";
  41. #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  42. const NSNotificationName FIRMessagingSendSuccessNotification =
  43. @"com.firebase.messaging.notif.send-success";
  44. const NSNotificationName FIRMessagingSendErrorNotification =
  45. @"com.firebase.messaging.notif.send-error";
  46. const NSNotificationName FIRMessagingMessagesDeletedNotification =
  47. @"com.firebase.messaging.notif.messages-deleted";
  48. const NSNotificationName FIRMessagingConnectionStateChangedNotification =
  49. @"com.firebase.messaging.notif.connection-state-changed";
  50. const NSNotificationName FIRMessagingRegistrationTokenRefreshedNotification =
  51. @"com.firebase.messaging.notif.fcm-token-refreshed";
  52. #else
  53. NSString *const FIRMessagingSendSuccessNotification =
  54. @"com.firebase.messaging.notif.send-success";
  55. NSString *const FIRMessagingSendErrorNotification =
  56. @"com.firebase.messaging.notif.send-error";
  57. NSString * const FIRMessagingMessagesDeletedNotification =
  58. @"com.firebase.messaging.notif.messages-deleted";
  59. NSString * const FIRMessagingConnectionStateChangedNotification =
  60. @"com.firebase.messaging.notif.connection-state-changed";
  61. NSString * const FIRMessagingRegistrationTokenRefreshedNotification =
  62. @"com.firebase.messaging.notif.fcm-token-refreshed";
  63. #endif // defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  64. NSString *const kFIRMessagingUserDefaultsKeyAutoInitEnabled =
  65. @"com.firebase.messaging.auto-init.enabled"; // Auto Init Enabled key stored in NSUserDefaults
  66. static NSString *const kFIRMessagingPlistAutoInitEnabled =
  67. @"FirebaseMessagingAutoInitEnabled"; // Auto Init Enabled key stored in Info.plist
  68. #pragma clang diagnostic push
  69. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  70. /// A private helper to convert enum values without depending on their numerical values
  71. /// (avoid casting). This can be removed when InstanceID's deprecated APNS type enum is
  72. /// removed.
  73. FIRInstanceIDAPNSTokenType FIRIIDAPNSTokenTypeFromAPNSTokenType(FIRMessagingAPNSTokenType type) {
  74. switch (type) {
  75. case FIRMessagingAPNSTokenTypeProd:
  76. return FIRInstanceIDAPNSTokenTypeProd;
  77. case FIRMessagingAPNSTokenTypeSandbox:
  78. return FIRInstanceIDAPNSTokenTypeSandbox;
  79. case FIRMessagingAPNSTokenTypeUnknown:
  80. return FIRInstanceIDAPNSTokenTypeUnknown;
  81. default:
  82. return FIRInstanceIDAPNSTokenTypeUnknown;
  83. }
  84. }
  85. #pragma clang diagnostic pop
  86. @interface FIRMessagingMessageInfo ()
  87. @property(nonatomic, readwrite, assign) FIRMessagingMessageStatus status;
  88. @end
  89. @implementation FIRMessagingMessageInfo
  90. - (instancetype)init {
  91. FIRMessagingInvalidateInitializer();
  92. }
  93. - (instancetype)initWithStatus:(FIRMessagingMessageStatus)status {
  94. self = [super init];
  95. if (self) {
  96. _status = status;
  97. }
  98. return self;
  99. }
  100. @end
  101. #pragma mark - for iOS 10 compatibility
  102. @implementation FIRMessagingRemoteMessage
  103. - (instancetype)init {
  104. self = [super init];
  105. if (self) {
  106. _appData = [[NSMutableDictionary alloc] init];
  107. }
  108. return self;
  109. }
  110. - (instancetype)initWithMessage:(FIRMessagingRemoteMessage *)message {
  111. self = [self init];
  112. if (self) {
  113. _appData = [message.appData copy];
  114. }
  115. return self;
  116. }
  117. @end
  118. @interface FIRMessaging ()<FIRMessagingClientDelegate, FIRMessagingReceiverDelegate,
  119. FIRReachabilityDelegate>
  120. // FIRApp properties
  121. @property(nonatomic, readwrite, copy) NSString *fcmSenderID;
  122. @property(nonatomic, readwrite, strong) NSData *apnsTokenData;
  123. @property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
  124. @property(nonatomic, readwrite, strong) FIRInstanceID *instanceID;
  125. @property(nonatomic, readwrite, assign) BOOL isClientSetup;
  126. @property(nonatomic, readwrite, strong) FIRMessagingClient *client;
  127. @property(nonatomic, readwrite, strong) FIRReachabilityChecker *reachability;
  128. @property(nonatomic, readwrite, strong) FIRMessagingDataMessageManager *dataMessageManager;
  129. @property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
  130. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  131. @property(nonatomic, readwrite, strong) FIRMessagingReceiver *receiver;
  132. @property(nonatomic, readwrite, strong) FIRMessagingSyncMessageManager *syncMessageManager;
  133. @property(nonatomic, readwrite, strong) NSUserDefaults *messagingUserDefaults;
  134. /// Message ID's logged for analytics. This prevents us from logging the same message twice
  135. /// which can happen if the user inadvertently calls `appDidReceiveMessage` along with us
  136. /// calling it implicitly during swizzling.
  137. @property(nonatomic, readwrite, strong) NSMutableSet *loggedMessageIDs;
  138. - (instancetype)initWithInstanceID:(FIRInstanceID *)instanceID
  139. userDefaults:(NSUserDefaults *)defaults NS_DESIGNATED_INITIALIZER;
  140. @end
  141. @implementation FIRMessaging
  142. + (FIRMessaging *)messaging {
  143. static FIRMessaging *messaging;
  144. static dispatch_once_t onceToken;
  145. dispatch_once(&onceToken, ^{
  146. messaging = [[FIRMessaging alloc] initPrivately];
  147. [messaging start];
  148. });
  149. return messaging;
  150. }
  151. - (instancetype)initWithInstanceID:(FIRInstanceID *)instanceID
  152. userDefaults:(NSUserDefaults *)defaults {
  153. self = [super init];
  154. if (self != nil) {
  155. _loggedMessageIDs = [NSMutableSet set];
  156. _instanceID = instanceID;
  157. _messagingUserDefaults = defaults;
  158. }
  159. return self;
  160. }
  161. - (instancetype)initPrivately {
  162. return [self initWithInstanceID:[FIRInstanceID instanceID]
  163. userDefaults:[NSUserDefaults standardUserDefaults]];
  164. }
  165. - (void)dealloc {
  166. [self.reachability stop];
  167. [[NSNotificationCenter defaultCenter] removeObserver:self];
  168. [self teardown];
  169. }
  170. - (void)setRemoteMessageDelegate:(id<FIRMessagingDelegate>)delegate {
  171. _delegate = delegate;
  172. }
  173. - (id<FIRMessagingDelegate>)remoteMessageDelegate {
  174. return self.delegate;
  175. }
  176. #pragma mark - Config
  177. - (void)start {
  178. // Print the library version for logging.
  179. NSString *currentLibraryVersion = FIRMessagingCurrentLibraryVersion();
  180. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessagingPrintLibraryVersion,
  181. @"FIRMessaging library version %@",
  182. currentLibraryVersion);
  183. [self setupReceiver];
  184. NSString *hostname = kFIRMessagingReachabilityHostname;
  185. self.reachability = [[FIRReachabilityChecker alloc] initWithReachabilityDelegate:self
  186. loggerDelegate:nil
  187. withHost:hostname];
  188. [self.reachability start];
  189. [self setupApplicationSupportSubDirectory];
  190. // setup FIRMessaging objects
  191. [self setupRmqManager];
  192. [self setupClient];
  193. [self setupSyncMessageManager];
  194. [self setupDataMessageManager];
  195. [self setupTopics];
  196. self.isClientSetup = YES;
  197. [self setupNotificationListeners];
  198. }
  199. - (void)setupApplicationSupportSubDirectory {
  200. NSString *messagingSubDirectory = kFIRMessagingApplicationSupportSubDirectory;
  201. if (![[self class] hasApplicationSupportSubDirectory:messagingSubDirectory]) {
  202. [[self class] createApplicationSupportSubDirectory:messagingSubDirectory];
  203. }
  204. }
  205. - (void)setupNotificationListeners {
  206. // To prevent multiple notifications remove self as observer for all events.
  207. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  208. [center removeObserver:self];
  209. [center addObserver:self
  210. selector:@selector(didReceiveDefaultInstanceIDToken:)
  211. name:kFIRMessagingFCMTokenNotification
  212. object:nil];
  213. [center addObserver:self
  214. selector:@selector(defaultInstanceIDTokenWasRefreshed:)
  215. name:kFIRMessagingRegistrationTokenRefreshNotification
  216. object:nil];
  217. [center addObserver:self
  218. selector:@selector(didReceiveAPNSToken:)
  219. name:kFIRMessagingAPNSTokenNotification
  220. object:nil];
  221. [center addObserver:self
  222. selector:@selector(applicationStateChanged)
  223. name:UIApplicationDidBecomeActiveNotification
  224. object:nil];
  225. [center addObserver:self
  226. selector:@selector(applicationStateChanged)
  227. name:UIApplicationDidEnterBackgroundNotification
  228. object:nil];
  229. }
  230. - (void)setupReceiver {
  231. self.receiver = [[FIRMessagingReceiver alloc] init];
  232. self.receiver.delegate = self;
  233. }
  234. - (void)setupClient {
  235. self.client = [[FIRMessagingClient alloc] initWithDelegate:self
  236. reachability:self.reachability
  237. rmq2Manager:self.rmq2Manager];
  238. }
  239. - (void)setupDataMessageManager {
  240. self.dataMessageManager =
  241. [[FIRMessagingDataMessageManager alloc] initWithDelegate:self.receiver
  242. client:self.client
  243. rmq2Manager:self.rmq2Manager
  244. syncMessageManager:self.syncMessageManager];
  245. [self.dataMessageManager refreshDelayedMessages];
  246. [self.client setDataMessageManager:self.dataMessageManager];
  247. }
  248. - (void)setupRmqManager {
  249. self.rmq2Manager = [[FIRMessagingRmqManager alloc] initWithDatabaseName:@"rmq2"];
  250. [self.rmq2Manager loadRmqId];
  251. }
  252. - (void)setupTopics {
  253. _FIRMessagingDevAssert(self.client, @"Invalid nil client before init pubsub.");
  254. self.pubsub = [[FIRMessagingPubSub alloc] initWithClient:self.client];
  255. }
  256. - (void)setupSyncMessageManager {
  257. self.syncMessageManager =
  258. [[FIRMessagingSyncMessageManager alloc] initWithRmqManager:self.rmq2Manager];
  259. // Delete the expired messages with a delay. We don't want to block startup with a somewhat
  260. // expensive db call.
  261. FIRMessaging_WEAKIFY(self);
  262. dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
  263. dispatch_after(time, dispatch_get_main_queue(), ^{
  264. FIRMessaging_STRONGIFY(self);
  265. [self.syncMessageManager removeExpiredSyncMessages];
  266. });
  267. }
  268. - (void)teardown {
  269. _FIRMessagingDevAssert([NSThread isMainThread],
  270. @"FIRMessaging should be called from main thread only.");
  271. [self.client teardown];
  272. self.pubsub = nil;
  273. self.syncMessageManager = nil;
  274. self.rmq2Manager = nil;
  275. self.dataMessageManager = nil;
  276. self.client = nil;
  277. self.isClientSetup = NO;
  278. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging001, @"Did successfully teardown");
  279. }
  280. #pragma mark - Messages
  281. - (FIRMessagingMessageInfo *)appDidReceiveMessage:(NSDictionary *)message {
  282. if (!message.count) {
  283. return [[FIRMessagingMessageInfo alloc] initWithStatus:FIRMessagingMessageStatusUnknown];
  284. }
  285. // For downstream messages that go via MCS we should strip out this key before sending
  286. // the message to the device.
  287. BOOL isOldMessage = NO;
  288. NSString *messageID = message[kFIRMessagingMessageIDKey];
  289. if ([messageID length]) {
  290. [self.rmq2Manager saveS2dMessageWithRmqId:messageID];
  291. BOOL isSyncMessage = [[self class] isAPNSSyncMessage:message];
  292. if (isSyncMessage) {
  293. isOldMessage = [self.syncMessageManager didReceiveAPNSSyncMessage:message];
  294. }
  295. }
  296. // Prevent duplicates by keeping a cache of all the logged messages during each session.
  297. // The duplicates only happen when the 3P app calls `appDidReceiveMessage:` along with
  298. // us swizzling their implementation to call the same method implicitly.
  299. if (!isOldMessage && messageID.length) {
  300. isOldMessage = [self.loggedMessageIDs containsObject:messageID];
  301. if (!isOldMessage) {
  302. [self.loggedMessageIDs addObject:messageID];
  303. }
  304. }
  305. if (!isOldMessage) {
  306. Class firMessagingLogClass = NSClassFromString(@"FIRMessagingLog");
  307. SEL logMessageSelector = NSSelectorFromString(@"logMessage:");
  308. if ([firMessagingLogClass respondsToSelector:logMessageSelector]) {
  309. #pragma clang diagnostic push
  310. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  311. [firMessagingLogClass performSelector:logMessageSelector
  312. withObject:message];
  313. }
  314. #pragma clang diagnostic pop
  315. [self handleContextManagerMessage:message];
  316. [self handleIncomingLinkIfNeededFromMessage:message];
  317. }
  318. return [[FIRMessagingMessageInfo alloc] initWithStatus:FIRMessagingMessageStatusNew];
  319. }
  320. - (BOOL)handleContextManagerMessage:(NSDictionary *)message {
  321. if ([FIRMessagingContextManagerService isContextManagerMessage:message]) {
  322. return [FIRMessagingContextManagerService handleContextManagerMessage:message];
  323. }
  324. return NO;
  325. }
  326. + (BOOL)isAPNSSyncMessage:(NSDictionary *)message {
  327. if ([message[kFIRMessagingMessageViaAPNSRootKey] isKindOfClass:[NSDictionary class]]) {
  328. NSDictionary *aps = message[kFIRMessagingMessageViaAPNSRootKey];
  329. return [aps[kFIRMessagingMessageAPNSContentAvailableKey] boolValue];
  330. }
  331. return NO;
  332. }
  333. - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message {
  334. NSURL *url = [self linkURLFromMessage:message];
  335. if (url == nil) {
  336. return;
  337. }
  338. if (![NSThread isMainThread]) {
  339. dispatch_async(dispatch_get_main_queue(), ^{
  340. [self handleIncomingLinkIfNeededFromMessage:message];
  341. });
  342. return;
  343. }
  344. UIApplication *application = [UIApplication sharedApplication];
  345. id<UIApplicationDelegate> appDelegate = application.delegate;
  346. SEL continueUserActivitySelector =
  347. @selector(application:continueUserActivity:restorationHandler:);
  348. SEL openURLWithOptionsSelector = @selector(application:openURL:options:);
  349. SEL openURLWithSourceApplicationSelector =
  350. @selector(application:openURL:sourceApplication:annotation:);
  351. SEL handleOpenURLSelector = @selector(application:handleOpenURL:);
  352. // Due to FIRAAppDelegateProxy swizzling, this selector will most likely get chosen, whether or
  353. // not the actual application has implemented
  354. // |application:continueUserActivity:restorationHandler:|. A warning will be displayed to the user
  355. // if they haven't implemented it.
  356. if ([NSUserActivity class] != nil &&
  357. [appDelegate respondsToSelector:continueUserActivitySelector]) {
  358. NSUserActivity *userActivity =
  359. [[NSUserActivity alloc] initWithActivityType:NSUserActivityTypeBrowsingWeb];
  360. userActivity.webpageURL = url;
  361. [appDelegate application:application
  362. continueUserActivity:userActivity
  363. restorationHandler:^(NSArray * _Nullable restorableObjects) {
  364. // Do nothing, as we don't support the app calling this block
  365. }];
  366. } else if ([appDelegate respondsToSelector:openURLWithOptionsSelector]) {
  367. #pragma clang diagnostic push
  368. #pragma clang diagnostic ignored "-Wunguarded-availability"
  369. [appDelegate application:application openURL:url options:@{}];
  370. #pragma clang diagnostic pop
  371. // Similarly, |application:openURL:sourceApplication:annotation:| will also always be called, due
  372. // to the default swizzling done by FIRAAppDelegateProxy in Firebase Analytics
  373. } else if ([appDelegate respondsToSelector:openURLWithSourceApplicationSelector]) {
  374. [appDelegate application:application
  375. openURL:url
  376. sourceApplication:FIRMessagingAppIdentifier()
  377. annotation:@{}];
  378. } else if ([appDelegate respondsToSelector:handleOpenURLSelector]) {
  379. [appDelegate application:application handleOpenURL:url];
  380. }
  381. }
  382. - (NSURL *)linkURLFromMessage:(NSDictionary *)message {
  383. NSString *urlString = message[kFIRMessagingMessageLinkKey];
  384. if (urlString == nil || ![urlString isKindOfClass:[NSString class]] || urlString.length == 0) {
  385. return nil;
  386. }
  387. NSURL *url = [NSURL URLWithString:urlString];
  388. return url;
  389. }
  390. #pragma mark - APNS
  391. - (NSData *)APNSToken {
  392. return self.apnsTokenData;
  393. }
  394. - (void)setAPNSToken:(NSData *)APNSToken {
  395. [self setAPNSToken:APNSToken type:FIRMessagingAPNSTokenTypeUnknown];
  396. }
  397. - (void)setAPNSToken:(NSData *)apnsToken type:(FIRMessagingAPNSTokenType)type {
  398. if ([apnsToken isEqual:self.apnsTokenData]) {
  399. return;
  400. }
  401. self.apnsTokenData = apnsToken;
  402. #pragma clang diagnostic push
  403. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  404. [self.instanceID setAPNSToken:apnsToken
  405. type:FIRIIDAPNSTokenTypeFromAPNSTokenType(type)];
  406. #pragma clang diagnostic pop
  407. }
  408. #pragma mark - FCM
  409. - (BOOL)isAutoInitEnabled {
  410. // Check storage
  411. id isAutoInitEnabledObject =
  412. [_messagingUserDefaults objectForKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  413. if (isAutoInitEnabledObject) {
  414. return [isAutoInitEnabledObject boolValue];
  415. }
  416. // Check Info.plist
  417. isAutoInitEnabledObject =
  418. [[NSBundle mainBundle] objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled];
  419. if (isAutoInitEnabledObject) {
  420. return [isAutoInitEnabledObject boolValue];
  421. }
  422. // If none of above exists, we default assume FCM auto init is enabled.
  423. return YES;
  424. }
  425. - (void)setAutoInitEnabled:(BOOL)autoInitEnabled {
  426. BOOL isFCMAutoInitEnabled = [self isAutoInitEnabled];
  427. [_messagingUserDefaults setBool:autoInitEnabled
  428. forKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled];
  429. if (!isFCMAutoInitEnabled && autoInitEnabled) {
  430. self.defaultFcmToken = self.instanceID.token;
  431. }
  432. }
  433. - (NSString *)FCMToken {
  434. NSString *token = self.defaultFcmToken;
  435. if (!token) {
  436. // We may not have received it from Instance ID yet (via NSNotification), so extract it directly
  437. token = self.instanceID.token;
  438. }
  439. return token;
  440. }
  441. - (void)retrieveFCMTokenForSenderID:(nonnull NSString *)senderID
  442. completion:(nonnull FIRMessagingFCMTokenFetchCompletion)completion {
  443. if (!senderID.length) {
  444. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenFetch,
  445. @"Sender ID not supplied. It is required for a token fetch, "
  446. @"to identify the sender.");
  447. if (completion) {
  448. NSString *description = @"Couldn't fetch token because a Sender ID was not supplied. A valid "
  449. @"Sender ID is required to fetch an FCM token";
  450. NSError *error = [NSError fcm_errorWithCode:FIRMessagingErrorInvalidRequest
  451. userInfo:@{NSLocalizedDescriptionKey : description}];
  452. completion(nil, error);
  453. }
  454. return;
  455. }
  456. NSDictionary *options = nil;
  457. if (self.APNSToken) {
  458. options = @{kFIRMessagingFCMTokenFetchAPNSOption : self.APNSToken};
  459. } else {
  460. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeAPNSTokenNotAvailableDuringTokenFetch,
  461. @"APNS device token not set before retrieving FCM Token for Sender ID "
  462. @"'%@'. Notifications to this FCM Token will not be delivered over APNS."
  463. @"Be sure to re-retrieve the FCM token once the APNS device token is "
  464. @"set.", senderID);
  465. }
  466. [self.instanceID tokenWithAuthorizedEntity:senderID
  467. scope:kFIRMessagingDefaultTokenScope
  468. options:options
  469. handler:completion];
  470. }
  471. - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
  472. completion:(nonnull FIRMessagingDeleteFCMTokenCompletion)completion {
  473. if (!senderID.length) {
  474. FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenDelete,
  475. @"Sender ID not supplied. It is required to delete an FCM token.");
  476. if (completion) {
  477. NSString *description = @"Couldn't delete token because a Sender ID was not supplied. A "
  478. @"valid Sender ID is required to delete an FCM token";
  479. NSError *error = [NSError fcm_errorWithCode:FIRMessagingErrorInvalidRequest
  480. userInfo:@{NSLocalizedDescriptionKey : description}];
  481. completion(error);
  482. }
  483. return;
  484. }
  485. [self.instanceID deleteTokenWithAuthorizedEntity:senderID
  486. scope:kFIRMessagingDefaultTokenScope
  487. handler:completion];
  488. }
  489. #pragma mark - FIRMessagingDelegate helper methods
  490. - (void)setDelegate:(id<FIRMessagingDelegate>)delegate {
  491. _delegate = delegate;
  492. [self validateDelegateConformsToTokenAvailabilityMethods];
  493. }
  494. // Check if the delegate conforms to either |didReceiveRegistrationToken:| or
  495. // |didRefreshRegistrationToken:|, and display a warning to the developer if not.
  496. // NOTE: Once |didReceiveRegistrationToken:| can be made a required method, this
  497. // check can be removed.
  498. - (void)validateDelegateConformsToTokenAvailabilityMethods {
  499. if (self.delegate &&
  500. ![self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)] &&
  501. ![self.delegate respondsToSelector:@selector(messaging:didRefreshRegistrationToken:)]) {
  502. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented,
  503. @"The object %@ does not respond to "
  504. @"-messaging:didReceiveRegistrationToken:, nor "
  505. @"-messaging:didRefreshRegistrationToken:. Please implement "
  506. @"-messaging:didReceiveRegistrationToken: to be provided with an FCM "
  507. @"token.", self.delegate.description);
  508. }
  509. }
  510. - (void)notifyDelegateOfFCMTokenAvailability {
  511. __weak FIRMessaging *weakSelf = self;
  512. if (![NSThread isMainThread]) {
  513. dispatch_async(dispatch_get_main_queue(), ^{
  514. [weakSelf notifyDelegateOfFCMTokenAvailability];
  515. });
  516. return;
  517. }
  518. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
  519. [self.delegate messaging:self didReceiveRegistrationToken:self.defaultFcmToken];
  520. }
  521. }
  522. #pragma mark - Application State Changes
  523. - (void)applicationStateChanged {
  524. if (self.shouldEstablishDirectChannel) {
  525. [self updateAutomaticClientConnection];
  526. }
  527. }
  528. #pragma mark - Direct Channel
  529. - (void)setShouldEstablishDirectChannel:(BOOL)shouldEstablishDirectChannel {
  530. if (_shouldEstablishDirectChannel == shouldEstablishDirectChannel) {
  531. return;
  532. }
  533. _shouldEstablishDirectChannel = shouldEstablishDirectChannel;
  534. [self updateAutomaticClientConnection];
  535. }
  536. - (BOOL)isDirectChannelEstablished {
  537. return self.client.isConnectionActive;
  538. }
  539. - (BOOL)shouldBeConnectedAutomatically {
  540. // We require a token from Instance ID
  541. NSString *token = self.defaultFcmToken;
  542. // Only on foreground connections
  543. UIApplicationState applicationState = [UIApplication sharedApplication].applicationState;
  544. BOOL shouldBeConnected = _shouldEstablishDirectChannel &&
  545. (token.length > 0) &&
  546. applicationState == UIApplicationStateActive;
  547. return shouldBeConnected;
  548. }
  549. - (void)updateAutomaticClientConnection {
  550. if (![NSThread isMainThread]) {
  551. // Call this method from the main thread
  552. dispatch_async(dispatch_get_main_queue(), ^{
  553. [self updateAutomaticClientConnection];
  554. });
  555. return;
  556. }
  557. BOOL shouldBeConnected = [self shouldBeConnectedAutomatically];
  558. if (shouldBeConnected && !self.client.isConnected) {
  559. [self.client connectWithHandler:^(NSError *error) {
  560. if (!error) {
  561. // It means we connected. Fire connection change notification
  562. [self notifyOfDirectChannelConnectionChange];
  563. }
  564. }];
  565. } else if (!shouldBeConnected && self.client.isConnected) {
  566. [self.client disconnect];
  567. [self notifyOfDirectChannelConnectionChange];
  568. }
  569. }
  570. - (void)notifyOfDirectChannelConnectionChange {
  571. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  572. [center postNotificationName:FIRMessagingConnectionStateChangedNotification object:self];
  573. }
  574. #pragma mark - Connect
  575. - (void)connectWithCompletion:(FIRMessagingConnectCompletion)handler {
  576. _FIRMessagingDevAssert([NSThread isMainThread],
  577. @"FIRMessaging connect should be called from main thread only.");
  578. _FIRMessagingDevAssert(self.isClientSetup, @"FIRMessaging client not setup.");
  579. [self.client connectWithHandler:^(NSError *error) {
  580. if (handler) {
  581. handler(error);
  582. }
  583. if (!error) {
  584. // It means we connected. Fire connection change notification
  585. [self notifyOfDirectChannelConnectionChange];
  586. }
  587. }];
  588. }
  589. - (void)disconnect {
  590. _FIRMessagingDevAssert([NSThread isMainThread],
  591. @"FIRMessaging should be called from main thread only.");
  592. if ([self.client isConnected]) {
  593. [self.client disconnect];
  594. [self notifyOfDirectChannelConnectionChange];
  595. }
  596. }
  597. #pragma mark - Topics
  598. + (NSString *)normalizeTopic:(NSString *)topic {
  599. if (![FIRMessagingPubSub hasTopicsPrefix:topic]) {
  600. topic = [FIRMessagingPubSub addPrefixToTopic:topic];
  601. }
  602. if ([FIRMessagingPubSub isValidTopicWithPrefix:topic]) {
  603. return [topic copy];
  604. }
  605. return nil;
  606. }
  607. - (void)subscribeToTopic:(NSString *)topic {
  608. [self subscribeToTopic:topic completion:nil];
  609. }
  610. - (void)subscribeToTopic:(NSString *)topic
  611. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  612. if (self.defaultFcmToken.length && topic.length) {
  613. NSString *normalizeTopic = [[self class ] normalizeTopic:topic];
  614. if (normalizeTopic.length) {
  615. [self.pubsub subscribeToTopic:normalizeTopic handler:completion];
  616. } else {
  617. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging009,
  618. @"Cannot parse topic name %@. Will not subscribe.", topic);
  619. }
  620. } else {
  621. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging010,
  622. @"Cannot subscribe to topic: %@ with token: %@", topic,
  623. self.defaultFcmToken);
  624. }
  625. }
  626. - (void)unsubscribeFromTopic:(NSString *)topic {
  627. [self unsubscribeFromTopic:topic completion:nil];
  628. }
  629. - (void)unsubscribeFromTopic:(NSString *)topic
  630. completion:(nullable FIRMessagingTopicOperationCompletion)completion {
  631. if (self.defaultFcmToken.length && topic.length) {
  632. NSString *normalizeTopic = [[self class] normalizeTopic:topic];
  633. if (normalizeTopic.length) {
  634. [self.pubsub unsubscribeFromTopic:normalizeTopic handler:completion];
  635. } else {
  636. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging011,
  637. @"Cannot parse topic name %@. Will not unsubscribe.", topic);
  638. }
  639. } else {
  640. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging012,
  641. @"Cannot unsubscribe to topic: %@ with token: %@", topic,
  642. self.defaultFcmToken);
  643. }
  644. }
  645. #pragma mark - Send
  646. - (void)sendMessage:(NSDictionary *)message
  647. to:(NSString *)to
  648. withMessageID:(NSString *)messageID
  649. timeToLive:(int64_t)ttl {
  650. _FIRMessagingDevAssert([to length] != 0, @"Invalid receiver id for FIRMessaging-message");
  651. NSMutableDictionary *fcmMessage = [[self class] createFIRMessagingMessageWithMessage:message
  652. to:to
  653. withID:messageID
  654. timeToLive:ttl
  655. delay:0];
  656. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessaging013, @"Sending message: %@ with id: %@",
  657. message, messageID);
  658. [self.dataMessageManager sendDataMessageStanza:fcmMessage];
  659. }
  660. + (NSMutableDictionary *)createFIRMessagingMessageWithMessage:(NSDictionary *)message
  661. to:(NSString *)to
  662. withID:(NSString *)msgID
  663. timeToLive:(int64_t)ttl
  664. delay:(int)delay {
  665. NSMutableDictionary *fcmMessage = [NSMutableDictionary dictionary];
  666. fcmMessage[kFIRMessagingSendTo] = [to copy];
  667. fcmMessage[kFIRMessagingSendMessageID] = msgID ? [msgID copy] : @"";
  668. fcmMessage[kFIRMessagingSendTTL] = @(ttl);
  669. fcmMessage[kFIRMessagingSendDelay] = @(delay);
  670. fcmMessage[KFIRMessagingSendMessageAppData] =
  671. [NSMutableDictionary dictionaryWithDictionary:message];
  672. return fcmMessage;
  673. }
  674. #pragma mark - IID dependencies
  675. + (NSString *)FIRMessagingSDKVersion {
  676. return FIRMessagingCurrentLibraryVersion();
  677. }
  678. + (NSString *)FIRMessagingSDKCurrentLocale {
  679. return [self currentLocale];
  680. }
  681. - (void)setAPNSToken:(NSData *)apnsToken error:(NSError *)error {
  682. if (apnsToken) {
  683. self.apnsTokenData = [apnsToken copy];
  684. }
  685. }
  686. #pragma mark - FIRMessagingReceiverDelegate
  687. - (void)receiver:(FIRMessagingReceiver *)receiver
  688. receivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
  689. if ([self.delegate respondsToSelector:@selector(messaging:didReceiveMessage:)]) {
  690. #pragma clang diagnostic push
  691. #pragma clang diagnostic ignored "-Wunguarded-availability"
  692. [self.delegate messaging:self didReceiveMessage:remoteMessage];
  693. #pragma pop
  694. } else if ([self.delegate respondsToSelector:@selector(applicationReceivedRemoteMessage:)]) {
  695. #pragma clang diagnostic push
  696. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  697. [self.delegate applicationReceivedRemoteMessage:remoteMessage];
  698. #pragma clang diagnostic pop
  699. } else {
  700. // Delegate methods weren't implemented, so messages are being dropped, log a warning
  701. FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRemoteMessageDelegateMethodNotImplemented,
  702. @"FIRMessaging received data-message, but FIRMessagingDelegate's"
  703. @"-messaging:didReceiveMessage: not implemented");
  704. }
  705. }
  706. #pragma mark - FIRReachabilityDelegate
  707. - (void)reachability:(FIRReachabilityChecker *)reachability
  708. statusChanged:(FIRReachabilityStatus)status {
  709. [self onNetworkStatusChanged];
  710. }
  711. #pragma mark - Network
  712. - (void)onNetworkStatusChanged {
  713. if (![self.client isConnected] && [self isNetworkAvailable]) {
  714. if (self.client.shouldStayConnected) {
  715. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging014,
  716. @"Attempting to establish direct channel.");
  717. [self.client retryConnectionImmediately:YES];
  718. }
  719. [self.pubsub scheduleSync:YES];
  720. }
  721. }
  722. - (BOOL)isNetworkAvailable {
  723. FIRReachabilityStatus status = self.reachability.reachabilityStatus;
  724. return (status == kFIRReachabilityViaCellular || status == kFIRReachabilityViaWifi);
  725. }
  726. - (FIRMessagingNetworkStatus)networkType {
  727. FIRReachabilityStatus status = self.reachability.reachabilityStatus;
  728. if (![self isNetworkAvailable]) {
  729. return kFIRMessagingReachabilityNotReachable;
  730. } else if (status == kFIRReachabilityViaCellular) {
  731. return kFIRMessagingReachabilityReachableViaWWAN;
  732. } else {
  733. return kFIRMessagingReachabilityReachableViaWiFi;
  734. }
  735. }
  736. #pragma mark - Notifications
  737. - (void)didReceiveDefaultInstanceIDToken:(NSNotification *)notification {
  738. if (notification.object && ![notification.object isKindOfClass:[NSString class]]) {
  739. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging015,
  740. @"Invalid default FCM token type %@",
  741. NSStringFromClass([notification.object class]));
  742. return;
  743. }
  744. NSString *oldToken = self.defaultFcmToken;
  745. self.defaultFcmToken = [(NSString *)notification.object copy];
  746. if (self.defaultFcmToken && ![self.defaultFcmToken isEqualToString:oldToken]) {
  747. [self notifyDelegateOfFCMTokenAvailability];
  748. }
  749. [self.pubsub scheduleSync:YES];
  750. if (self.shouldEstablishDirectChannel) {
  751. [self updateAutomaticClientConnection];
  752. }
  753. }
  754. - (void)defaultInstanceIDTokenWasRefreshed:(NSNotification *)notification {
  755. // Retrieve the Instance ID default token, and if it is non-nil, post it
  756. NSString *token = self.instanceID.token;
  757. // Sometimes Instance ID doesn't yet have a token, so wait until the default
  758. // token is fetched, and then notify. This ensures that this token should not
  759. // be nil when the developer accesses it.
  760. if (token != nil) {
  761. NSString *oldToken = self.defaultFcmToken;
  762. self.defaultFcmToken = [token copy];
  763. if (self.defaultFcmToken && ![self.defaultFcmToken isEqualToString:oldToken]) {
  764. [self notifyDelegateOfFCMTokenAvailability];
  765. }
  766. // Call deprecated refresh method, because it should still work (until it is removed).
  767. #pragma clang diagnostic push
  768. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  769. if ([self.delegate respondsToSelector:@selector(messaging:didRefreshRegistrationToken:)]) {
  770. [self.delegate messaging:self didRefreshRegistrationToken:token];
  771. }
  772. #pragma clang diagnostic pop
  773. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  774. [center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification object:nil];
  775. }
  776. }
  777. - (void)didReceiveAPNSToken:(NSNotification *)notification {
  778. NSData *apnsToken = notification.object;
  779. if (![apnsToken isKindOfClass:[NSData class]]) {
  780. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging016, @"Invalid APNS token type %@",
  781. NSStringFromClass([notification.object class]));
  782. return;
  783. }
  784. // Set this value directly, and since this came from InstanceID, don't set it back to InstanceID
  785. self.apnsTokenData = [apnsToken copy];
  786. }
  787. #pragma mark - Application Support Directory
  788. + (BOOL)hasApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  789. NSString *subDirectoryPath = [self pathForApplicationSupportSubDirectory:subDirectoryName];
  790. BOOL isDirectory;
  791. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  792. isDirectory:&isDirectory]) {
  793. return NO;
  794. } else if (!isDirectory) {
  795. return NO;
  796. }
  797. return YES;
  798. }
  799. + (NSString *)pathForApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  800. NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
  801. NSUserDomainMask, YES);
  802. NSString *applicationSupportDirPath = directoryPaths.lastObject;
  803. NSArray *components = @[applicationSupportDirPath, subDirectoryName];
  804. return [NSString pathWithComponents:components];
  805. }
  806. + (BOOL)createApplicationSupportSubDirectory:(NSString *)subDirectoryName {
  807. NSString *subDirectoryPath = [self pathForApplicationSupportSubDirectory:subDirectoryName];
  808. BOOL hasSubDirectory;
  809. if (![[NSFileManager defaultManager] fileExistsAtPath:subDirectoryPath
  810. isDirectory:&hasSubDirectory]) {
  811. NSError *error;
  812. [[NSFileManager defaultManager] createDirectoryAtPath:subDirectoryPath
  813. withIntermediateDirectories:YES
  814. attributes:nil
  815. error:&error];
  816. if (error) {
  817. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging017,
  818. @"Cannot create directory %@, error: %@", subDirectoryPath, error);
  819. return NO;
  820. }
  821. } else {
  822. if (!hasSubDirectory) {
  823. FIRMessagingLoggerError(kFIRMessagingMessageCodeMessaging018,
  824. @"Found file instead of directory at %@", subDirectoryPath);
  825. return NO;
  826. }
  827. }
  828. return YES;
  829. }
  830. #pragma mark - Locales
  831. + (NSString *)currentLocale {
  832. NSArray *locales = [self firebaseLocales];
  833. NSArray *preferredLocalizations =
  834. [NSBundle preferredLocalizationsFromArray:locales
  835. forPreferences:[NSLocale preferredLanguages]];
  836. NSString *legalDocsLanguage = [preferredLocalizations firstObject];
  837. // Use en as the default language
  838. return legalDocsLanguage ? legalDocsLanguage : @"en";
  839. }
  840. + (NSArray *)firebaseLocales {
  841. NSMutableArray *locales = [NSMutableArray array];
  842. NSDictionary *localesMap = [self firebaselocalesMap];
  843. for (NSString *key in localesMap) {
  844. [locales addObjectsFromArray:localesMap[key]];
  845. }
  846. return locales;
  847. }
  848. + (NSDictionary *)firebaselocalesMap {
  849. return @{
  850. // Albanian
  851. @"sq" : @[ @"sq_AL" ],
  852. // Belarusian
  853. @"be" : @[ @"be_BY" ],
  854. // Bulgarian
  855. @"bg" : @[ @"bg_BG" ],
  856. // Catalan
  857. @"ca" : @[ @"ca", @"ca_ES" ],
  858. // Croatian
  859. @"hr" : @[ @"hr", @"hr_HR" ],
  860. // Czech
  861. @"cs" : @[ @"cs", @"cs_CZ" ],
  862. // Danish
  863. @"da" : @[ @"da", @"da_DK" ],
  864. // Estonian
  865. @"et" : @[ @"et_EE" ],
  866. // Finnish
  867. @"fi" : @[ @"fi", @"fi_FI" ],
  868. // Hebrew
  869. @"he" : @[ @"he", @"iw_IL" ],
  870. // Hindi
  871. @"hi" : @[ @"hi_IN" ],
  872. // Hungarian
  873. @"hu" : @[ @"hu", @"hu_HU" ],
  874. // Icelandic
  875. @"is" : @[ @"is_IS" ],
  876. // Indonesian
  877. @"id" : @[ @"id", @"in_ID", @"id_ID" ],
  878. // Irish
  879. @"ga" : @[ @"ga_IE" ],
  880. // Korean
  881. @"ko" : @[ @"ko", @"ko_KR", @"ko-KR" ],
  882. // Latvian
  883. @"lv" : @[ @"lv_LV" ],
  884. // Lithuanian
  885. @"lt" : @[ @"lt_LT" ],
  886. // Macedonian
  887. @"mk" : @[ @"mk_MK" ],
  888. // Malay
  889. @"ms" : @[ @"ms_MY" ],
  890. // Maltese
  891. @"ms" : @[ @"mt_MT" ],
  892. // Polish
  893. @"pl" : @[ @"pl", @"pl_PL", @"pl-PL" ],
  894. // Romanian
  895. @"ro" : @[ @"ro", @"ro_RO" ],
  896. // Russian
  897. @"ru" : @[ @"ru_RU", @"ru", @"ru_BY", @"ru_KZ", @"ru-RU" ],
  898. // Slovak
  899. @"sk" : @[ @"sk", @"sk_SK" ],
  900. // Slovenian
  901. @"sl" : @[ @"sl_SI" ],
  902. // Swedish
  903. @"sv" : @[ @"sv", @"sv_SE", @"sv-SE" ],
  904. // Turkish
  905. @"tr" : @[ @"tr", @"tr-TR", @"tr_TR" ],
  906. // Ukrainian
  907. @"uk" : @[ @"uk", @"uk_UA" ],
  908. // Vietnamese
  909. @"vi" : @[ @"vi", @"vi_VN" ],
  910. // The following are groups of locales or locales that sub-divide a
  911. // language).
  912. // Arabic
  913. @"ar" : @[
  914. @"ar",
  915. @"ar_DZ",
  916. @"ar_BH",
  917. @"ar_EG",
  918. @"ar_IQ",
  919. @"ar_JO",
  920. @"ar_KW",
  921. @"ar_LB",
  922. @"ar_LY",
  923. @"ar_MA",
  924. @"ar_OM",
  925. @"ar_QA",
  926. @"ar_SA",
  927. @"ar_SD",
  928. @"ar_SY",
  929. @"ar_TN",
  930. @"ar_AE",
  931. @"ar_YE",
  932. @"ar_GB",
  933. @"ar-IQ",
  934. @"ar_US"
  935. ],
  936. // Simplified Chinese
  937. @"zh_Hans" : @[ @"zh_CN", @"zh_SG", @"zh-Hans" ],
  938. // Traditional Chinese
  939. @"zh_Hant" : @[ @"zh_HK", @"zh_TW", @"zh-Hant", @"zh-HK", @"zh-TW" ],
  940. // Dutch
  941. @"nl" : @[ @"nl", @"nl_BE", @"nl_NL", @"nl-NL" ],
  942. // English
  943. @"en" : @[
  944. @"en",
  945. @"en_AU",
  946. @"en_CA",
  947. @"en_IN",
  948. @"en_IE",
  949. @"en_MT",
  950. @"en_NZ",
  951. @"en_PH",
  952. @"en_SG",
  953. @"en_ZA",
  954. @"en_GB",
  955. @"en_US",
  956. @"en_AE",
  957. @"en-AE",
  958. @"en_AS",
  959. @"en-AU",
  960. @"en_BD",
  961. @"en-CA",
  962. @"en_EG",
  963. @"en_ES",
  964. @"en_GB",
  965. @"en-GB",
  966. @"en_HK",
  967. @"en_ID",
  968. @"en-IN",
  969. @"en_NG",
  970. @"en-PH",
  971. @"en_PK",
  972. @"en-SG",
  973. @"en-US"
  974. ],
  975. // French
  976. @"fr" : @[
  977. @"fr",
  978. @"fr_BE",
  979. @"fr_CA",
  980. @"fr_FR",
  981. @"fr_LU",
  982. @"fr_CH",
  983. @"fr-CA",
  984. @"fr-FR",
  985. @"fr_MA"
  986. ],
  987. // German
  988. @"de" : @[ @"de", @"de_AT", @"de_DE", @"de_LU", @"de_CH", @"de-DE" ],
  989. // Greek
  990. @"el" : @[ @"el", @"el_CY", @"el_GR" ],
  991. // Italian
  992. @"it" : @[ @"it", @"it_IT", @"it_CH", @"it-IT" ],
  993. // Japanese
  994. @"ja" : @[ @"ja", @"ja_JP", @"ja_JP_JP", @"ja-JP" ],
  995. // Norwegian
  996. @"no" : @[ @"nb", @"no_NO", @"no_NO_NY", @"nb_NO" ],
  997. // Brazilian Portuguese
  998. @"pt_BR" : @[ @"pt_BR", @"pt-BR" ],
  999. // European Portuguese
  1000. @"pt_PT" : @[ @"pt", @"pt_PT", @"pt-PT" ],
  1001. // Serbian
  1002. @"sr" : @[
  1003. @"sr_BA",
  1004. @"sr_ME",
  1005. @"sr_RS",
  1006. @"sr_Latn_BA",
  1007. @"sr_Latn_ME",
  1008. @"sr_Latn_RS"
  1009. ],
  1010. // European Spanish
  1011. @"es_ES" : @[ @"es", @"es_ES", @"es-ES" ],
  1012. // Mexican Spanish
  1013. @"es_MX" : @[ @"es-MX", @"es_MX", @"es_US", @"es-US" ],
  1014. // Latin American Spanish
  1015. @"es_419" : @[
  1016. @"es_AR",
  1017. @"es_BO",
  1018. @"es_CL",
  1019. @"es_CO",
  1020. @"es_CR",
  1021. @"es_DO",
  1022. @"es_EC",
  1023. @"es_SV",
  1024. @"es_GT",
  1025. @"es_HN",
  1026. @"es_NI",
  1027. @"es_PA",
  1028. @"es_PY",
  1029. @"es_PE",
  1030. @"es_PR",
  1031. @"es_UY",
  1032. @"es_VE",
  1033. @"es-AR",
  1034. @"es-CL",
  1035. @"es-CO"
  1036. ],
  1037. // Thai
  1038. @"th" : @[ @"th", @"th_TH", @"th_TH_TH" ],
  1039. };
  1040. }
  1041. @end