FIRMessaging.m 43 KB

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