AppDelegate.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. self.moduleName = @"Strides";
  32. // You can add your custom initial props in the dictionary below.
  33. // They will be passed down to the ViewController used by React Native.
  34. self.initialProps = @{};
  35. //Firebase推送配置
  36. [FIRApp configure];
  37. [FIRMessaging messaging].delegate = self;
  38. //谷歌地图配置
  39. [GMSServices provideAPIKey:@"AIzaSyAVzs860l2Iuu1zG80IT1Zu4w7OvbVmJ4g"]; // add this line using the api key obtained from Google Console
  40. //远程推送配置
  41. // Define UNUserNotificationCenter
  42. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  43. center.delegate = self;
  44. UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
  45. UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  46. [[UNUserNotificationCenter currentNotificationCenter]
  47. requestAuthorizationWithOptions:authOptions
  48. completionHandler:^(BOOL granted, NSError * _Nullable error) {
  49. // ...
  50. }];
  51. [application registerForRemoteNotifications];
  52. //return YES;
  53. return [super application:application didFinishLaunchingWithOptions:launchOptions];
  54. }
  55. //Called when a notification is delivered to a foreground app.
  56. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  57. {
  58. completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
  59. }
  60. - (void) messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
  61. //NSLog(@"FCM registration token: %@", fcmToken);
  62. // Notify about received token.
  63. NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
  64. [[NSNotificationCenter defaultCenter] postNotificationName:
  65. @"FCMToken" object:nil userInfo:dataDict];
  66. // TODO: 如有必要,将令牌发送到应用程序服务器。
  67. //注意:每次应用程序启动时以及每当生成新令牌时,都会触发此回调。
  68. //NSData *deviceToken = [fcmToken dataUsingEncoding:NSUTF8StringEncoding];
  69. //或者,您也可以监听名为 kFIRMessagingRegistrationTokenRefreshNotification 的 NSNotification,而不提供委托方法。该令牌属性始终具有当前令牌值。
  70. //[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  71. }
  72. //- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  73. // NSLog(@"error: %@", error);
  74. //}
  75. //Required for the register event.
  76. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  77. {
  78. // NSLog(@"APNs registration token: %@", hexString);
  79. if ([FIRMessaging messaging].FCMToken != nil) {
  80. [FIRMessaging messaging].APNSToken = deviceToken;
  81. //NSLog(@"FIRMessaging token: %@", [FIRMessaging messaging].FCMToken);
  82. //将FCMToken发送到RN端
  83. [[NSNotificationCenter defaultCenter] postNotificationName:
  84. @"RemoteNotificationsRegistered" object:self
  85. userInfo:@{@"deviceToken" : [[FIRMessaging messaging].FCMToken copy]}];
  86. //[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  87. }
  88. }
  89. // Required for the notification event. You must call the completion handler after handling the remote notification.
  90. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  91. fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  92. {
  93. [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
  94. }
  95. // Required for the registrationError event.
  96. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
  97. {
  98. NSLog(@"didFailToRegisterForRemoteNotificationsWithError: %@", error);
  99. [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
  100. }
  101. // Required for localNotification event
  102. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  103. didReceiveNotificationResponse:(UNNotificationResponse *)response
  104. withCompletionHandler:(void (^)(void))completionHandler
  105. {
  106. [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  107. //completionHandler();
  108. }
  109. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  110. {
  111. #if DEBUG
  112. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  113. #else
  114. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  115. #endif
  116. }
  117. @end