AppDelegate.mm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #import "AppDelegate.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTBundleURLProvider.h>
  4. #import <React/RCTRootView.h>
  5. #import <React/RCTAppSetupUtils.h>
  6. #import <RCTJPushModule.h>
  7. #if RCT_NEW_ARCH_ENABLED
  8. #import <React/CoreModulesPlugins.h>
  9. #import <React/RCTCxxBridgeDelegate.h>
  10. #import <React/RCTFabricSurfaceHostingProxyRootView.h>
  11. #import <React/RCTSurfacePresenter.h>
  12. #import <React/RCTSurfacePresenterBridgeAdapter.h>
  13. #import <ReactCommon/RCTTurboModuleManager.h>
  14. #import <react/config/ReactNativeConfig.h>
  15. static NSString *const kRNConcurrentRoot = @"concurrentRoot";
  16. @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> {
  17. RCTTurboModuleManager *_turboModuleManager;
  18. RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
  19. std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
  20. facebook::react::ContextContainer::Shared _contextContainer;
  21. }
  22. @end
  23. #endif
  24. @implementation AppDelegate
  25. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  26. {
  27. // APNS
  28. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  29. if (@available(iOS 12.0, *)) {
  30. entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
  31. }
  32. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  33. RCTAppSetupPrepareApp(application);
  34. RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
  35. #if RCT_NEW_ARCH_ENABLED
  36. _contextContainer = std::make_shared<facebook::react::ContextContainer const>();
  37. _reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();
  38. _contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
  39. _bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
  40. bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
  41. #endif
  42. NSDictionary *initProps = [self prepareInitialProps];
  43. UIView *rootView = [self.reactDelegate createRootViewWithBridge:bridge moduleName:@"taroDemo" initialProperties:initProps];
  44. if (@available(iOS 13.0, *)) {
  45. rootView.backgroundColor = [UIColor systemBackgroundColor];
  46. } else {
  47. rootView.backgroundColor = [UIColor whiteColor];
  48. }
  49. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  50. UIViewController *rootViewController = [self.reactDelegate createRootViewController];
  51. rootViewController.view = rootView;
  52. self.window.rootViewController = rootViewController;
  53. [self.window makeKeyAndVisible];
  54. [super application:application didFinishLaunchingWithOptions:launchOptions];
  55. return YES;
  56. }
  57. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  58. ///
  59. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  60. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  61. /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
  62. - (BOOL)concurrentRootEnabled
  63. {
  64. // Switch this bool to turn on and off the concurrent root
  65. return true;
  66. }
  67. - (NSDictionary *)prepareInitialProps
  68. {
  69. NSMutableDictionary *initProps = [NSMutableDictionary new];
  70. #ifdef RCT_NEW_ARCH_ENABLED
  71. initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
  72. #endif
  73. return initProps;
  74. }
  75. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  76. {
  77. // return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  78. #if DEBUG
  79. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  80. #else
  81. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  82. #endif
  83. }
  84. #if RCT_NEW_ARCH_ENABLED
  85. #pragma mark - RCTCxxBridgeDelegate
  86. - (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
  87. {
  88. _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
  89. delegate:self
  90. jsInvoker:bridge.jsCallInvoker];
  91. return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
  92. }
  93. #pragma mark RCTTurboModuleManagerDelegate
  94. - (Class)getModuleClassFromName:(const char *)name
  95. {
  96. return RCTCoreModulesClassProvider(name);
  97. }
  98. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  99. jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
  100. {
  101. return nullptr;
  102. }
  103. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  104. initParams:
  105. (const facebook::react::ObjCTurboModule::InitParams &)params
  106. {
  107. return nullptr;
  108. }
  109. - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
  110. {
  111. return RCTAppSetupDefaultModuleFromClass(moduleClass);
  112. }
  113. #endif
  114. //************************************************JPush start************************************************
  115. //注册 APNS 成功并上报 DeviceToken
  116. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  117. [JPUSHService registerDeviceToken:deviceToken];
  118. }
  119. //iOS 7 APNS
  120. - (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  121. // iOS 10 以下 Required
  122. NSLog(@"iOS 7 APNS");
  123. [JPUSHService handleRemoteNotification:userInfo];
  124. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  125. completionHandler(UIBackgroundFetchResultNewData);
  126. }
  127. //iOS 10 前台收到消息
  128. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  129. NSDictionary * userInfo = notification.request.content.userInfo;
  130. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  131. // Apns
  132. NSLog(@"iOS 10 APNS 前台收到消息");
  133. [JPUSHService handleRemoteNotification:userInfo];
  134. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  135. }
  136. else {
  137. // 本地通知 todo
  138. NSLog(@"iOS 10 本地通知 前台收到消息");
  139. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  140. }
  141. //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  142. completionHandler(UNNotificationPresentationOptionAlert);
  143. }
  144. //iOS 10 消息事件回调
  145. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
  146. NSDictionary * userInfo = response.notification.request.content.userInfo;
  147. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  148. // Apns
  149. NSLog(@"iOS 10 APNS 消息事件回调");
  150. [JPUSHService handleRemoteNotification:userInfo];
  151. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  152. [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
  153. [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
  154. }
  155. else {
  156. // 本地通知
  157. NSLog(@"iOS 10 本地通知 消息事件回调");
  158. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  159. [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
  160. [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
  161. }
  162. // 系统要求执行这个方法
  163. completionHandler();
  164. }
  165. //自定义消息
  166. - (void)networkDidReceiveMessage:(NSNotification *)notification {
  167. NSDictionary * userInfo = [notification userInfo];
  168. [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
  169. }
  170. @end