AppDelegate.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #import "AppDelegate.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTBundleURLProvider.h>
  4. #import <React/RCTRootView.h>
  5. #import <GoogleMaps/GoogleMaps.h>
  6. #import <UserNotifications/UserNotifications.h>
  7. #import <RNCPushNotificationIOS.h>
  8. #ifdef FB_SONARKIT_ENABLED
  9. #import <FlipperKit/FlipperClient.h>
  10. #import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
  11. #import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
  12. #import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
  13. #import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
  14. #import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>
  15. static void InitializeFlipper(UIApplication *application) {
  16. FlipperClient *client = [FlipperClient sharedClient];
  17. SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
  18. [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]];
  19. [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
  20. [client addPlugin:[FlipperKitReactPlugin new]];
  21. [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
  22. [client start];
  23. }
  24. #endif
  25. @import Firebase;
  26. @interface AppDelegate () <FIRMessagingDelegate>
  27. @end
  28. @implementation AppDelegate
  29. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  30. {
  31. #ifdef FB_SONARKIT_ENABLED
  32. InitializeFlipper(application);
  33. #endif
  34. RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  35. RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
  36. moduleName:@"JuicePlus"
  37. initialProperties:nil];
  38. if (@available(iOS 13.0, *)) {
  39. rootView.backgroundColor = [UIColor systemBackgroundColor];
  40. } else {
  41. rootView.backgroundColor = [UIColor whiteColor];
  42. }
  43. //Firebase推送配置
  44. [FIRApp configure];
  45. [FIRMessaging messaging].delegate = self;
  46. //谷歌地图配置
  47. [GMSServices provideAPIKey:@"AIzaSyAVzs860l2Iuu1zG80IT1Zu4w7OvbVmJ4g"]; // add this line using the api key obtained from Google Console
  48. //远程推送配置
  49. // Define UNUserNotificationCenter
  50. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  51. center.delegate = self;
  52. UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
  53. UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  54. [[UNUserNotificationCenter currentNotificationCenter]
  55. requestAuthorizationWithOptions:authOptions
  56. completionHandler:^(BOOL granted, NSError * _Nullable error) {
  57. // ...
  58. }];
  59. [application registerForRemoteNotifications];
  60. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  61. UIViewController *rootViewController = [UIViewController new];
  62. rootViewController.view = rootView;
  63. self.window.rootViewController = rootViewController;
  64. [self.window makeKeyAndVisible];
  65. return YES;
  66. }
  67. //Called when a notification is delivered to a foreground app.
  68. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  69. {
  70. completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
  71. }
  72. - (void) messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
  73. //NSLog(@"FCM registration token: %@", fcmToken);
  74. // Notify about received token.
  75. NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
  76. [[NSNotificationCenter defaultCenter] postNotificationName:
  77. @"FCMToken" object:nil userInfo:dataDict];
  78. // TODO: 如有必要,将令牌发送到应用程序服务器。
  79. //注意:每次应用程序启动时以及每当生成新令牌时,都会触发此回调。
  80. //NSData *deviceToken = [fcmToken dataUsingEncoding:NSUTF8StringEncoding];
  81. //或者,您也可以监听名为 kFIRMessagingRegistrationTokenRefreshNotification 的 NSNotification,而不提供委托方法。该令牌属性始终具有当前令牌值。
  82. //[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  83. }
  84. //- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  85. // NSLog(@"error: %@", error);
  86. //}
  87. //Required for the register event.
  88. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  89. {
  90. // NSMutableString *hexString = [NSMutableString string];
  91. // NSUInteger deviceTokenLength = deviceToken.length;
  92. // const unsigned char *bytes = deviceToken.bytes;
  93. // for (NSUInteger i = 0; i < deviceTokenLength; i++) {
  94. // [hexString appendFormat:@"%02x", bytes[i]];
  95. // }
  96. // NSLog(@"APNs registration token: %@", hexString);
  97. if ([FIRMessaging messaging].FCMToken != nil) {
  98. [FIRMessaging messaging].APNSToken = deviceToken;
  99. //NSLog(@"FIRMessaging token: %@", [FIRMessaging messaging].FCMToken);
  100. //将FCMToken发送到RN端
  101. [[NSNotificationCenter defaultCenter] postNotificationName:
  102. @"RemoteNotificationsRegistered" object:self
  103. userInfo:@{@"deviceToken" : [[FIRMessaging messaging].FCMToken copy]}];
  104. //[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  105. }
  106. }
  107. // Required for the notification event. You must call the completion handler after handling the remote notification.
  108. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  109. fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  110. {
  111. [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
  112. }
  113. // Required for the registrationError event.
  114. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
  115. {
  116. NSLog(@"didFailToRegisterForRemoteNotificationsWithError: %@", error);
  117. [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
  118. }
  119. // Required for localNotification event
  120. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  121. didReceiveNotificationResponse:(UNNotificationResponse *)response
  122. withCompletionHandler:(void (^)(void))completionHandler
  123. {
  124. [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  125. completionHandler();
  126. }
  127. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  128. {
  129. #if DEBUG
  130. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  131. #else
  132. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  133. #endif
  134. }
  135. @end