You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.3 KiB

6 years ago
  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. #import "FIRMessaging+FIRApp.h"
  17. #import <FirebaseCore/FIRAppInternal.h>
  18. #import <FirebaseCore/FIROptionsInternal.h>
  19. #import "FIRMessagingConstants.h"
  20. #import "FIRMessagingLogger.h"
  21. #import "FIRMessagingPubSub.h"
  22. #import "FIRMessagingRemoteNotificationsProxy.h"
  23. #import "FIRMessagingVersionUtilities.h"
  24. #import "FIRMessaging_Private.h"
  25. @interface FIRMessaging ()
  26. @property(nonatomic, readwrite, strong) NSString *fcmSenderID;
  27. @end
  28. @implementation FIRMessaging (FIRApp)
  29. + (void)load {
  30. // FIRMessaging by default removes itself from observing any notifications.
  31. [[NSNotificationCenter defaultCenter] addObserver:self
  32. selector:@selector(didReceiveConfigureSDKNotification:)
  33. name:kFIRAppReadyToConfigureSDKNotification
  34. object:[FIRApp class]];
  35. }
  36. + (void)didReceiveConfigureSDKNotification:(NSNotification *)notification {
  37. NSDictionary *appInfoDict = notification.userInfo;
  38. NSNumber *isDefaultApp = appInfoDict[kFIRAppIsDefaultAppKey];
  39. if (![isDefaultApp boolValue]) {
  40. // Only configure for the default FIRApp.
  41. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeFIRApp001,
  42. @"Firebase Messaging only works with the default app.");
  43. return;
  44. }
  45. NSString *appName = appInfoDict[kFIRAppNameKey];
  46. FIRApp *app = [FIRApp appNamed:appName];
  47. [[FIRMessaging messaging] configureMessaging:app];
  48. }
  49. - (void)configureMessaging:(FIRApp *)app {
  50. FIROptions *options = app.options;
  51. NSError *error;
  52. if (!options.GCMSenderID.length) {
  53. error =
  54. [FIRApp errorForSubspecConfigurationFailureWithDomain:kFirebaseCloudMessagingErrorDomain
  55. errorCode:FIRErrorCodeCloudMessagingFailed
  56. service:kFIRServiceMessaging
  57. reason:@"Google Sender ID must not be nil"
  58. @" or empty."];
  59. [self exitApp:app withError:error];
  60. return;
  61. }
  62. self.fcmSenderID = [options.GCMSenderID copy];
  63. self.globalAutomaticDataCollectionEnabled = [app isAutomaticDataCollectionEnabled];
  64. // Swizzle remote-notification-related methods (app delegate and UNUserNotificationCenter)
  65. if ([FIRMessagingRemoteNotificationsProxy canSwizzleMethods]) {
  66. NSString *docsURLString = @"https://firebase.google.com/docs/cloud-messaging/ios/client"
  67. @"#method_swizzling_in_firebase_messaging";
  68. FIRMessagingLoggerNotice(kFIRMessagingMessageCodeFIRApp000,
  69. @"FIRMessaging Remote Notifications proxy enabled, will swizzle "
  70. @"remote notification receiver handlers. If you'd prefer to manually "
  71. @"integrate Firebase Messaging, add \"%@\" to your Info.plist, "
  72. @"and set it to NO. Follow the instructions at:\n%@\nto ensure "
  73. @"proper integration.",
  74. kFIRMessagingRemoteNotificationsProxyEnabledInfoPlistKey,
  75. docsURLString);
  76. [FIRMessagingRemoteNotificationsProxy swizzleMethods];
  77. }
  78. }
  79. - (void)exitApp:(FIRApp *)app withError:(NSError *)error {
  80. [app sendLogsWithServiceName:kFIRServiceMessaging
  81. version:FIRMessagingCurrentLibraryVersion()
  82. error:error];
  83. if (error) {
  84. NSString *message = nil;
  85. if (app.options.usingOptionsFromDefaultPlist) {
  86. // Configured using plist file
  87. message = [NSString stringWithFormat:@"Firebase Messaging has stopped your project because "
  88. @"there are missing or incorrect values provided in %@.%@ that may prevent "
  89. @"your app from behaving as expected:\n\n"
  90. @"Error: %@\n\n"
  91. @"Please fix these issues to ensure that Firebase is correctly configured in "
  92. @"your project.",
  93. kServiceInfoFileName,
  94. kServiceInfoFileType,
  95. error.localizedFailureReason];
  96. } else {
  97. // Configured manually
  98. message = [NSString stringWithFormat:@"Firebase Messaging has stopped your project because "
  99. @"there are missing or incorrect values in Firebase's configuration options "
  100. @"that may prevent your app from behaving as expected:\n\n"
  101. @"Error:%@\n\n"
  102. @"Please fix these issues to ensure that Firebase is correctly configured in "
  103. @"your project.",
  104. error.localizedFailureReason];
  105. }
  106. [NSException raise:kFirebaseCloudMessagingErrorDomain format:@"%@", message];
  107. }
  108. }
  109. @end