AppDelegate.mm 5.8 KB

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