FIRMessaging.m 43 KB

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