AppDelegate2.mm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #import "AppDelegate2.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. #import <JPUSHService.h>
  8. #if RCT_NEW_ARCH_ENABLED
  9. #import <React/CoreModulesPlugins.h>
  10. #import <React/RCTCxxBridgeDelegate.h>
  11. #import <React/RCTFabricSurfaceHostingProxyRootView.h>
  12. #import <React/RCTSurfacePresenter.h>
  13. #import <React/RCTSurfacePresenterBridgeAdapter.h>
  14. #import <ReactCommon/RCTTurboModuleManager.h>
  15. #import <react/config/ReactNativeConfig.h>
  16. static NSString *const kRNConcurrentRoot = @"concurrentRoot";
  17. @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate,JPUSHRegisterDelegate> {
  18. RCTTurboModuleManager *_turboModuleManager;
  19. RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
  20. std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
  21. facebook::react::ContextContainer::Shared _contextContainer;
  22. }
  23. @end
  24. #endif
  25. @implementation AppDelegate
  26. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  27. {
  28. // APNS
  29. JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  30. if (@available(iOS 12.0, *)) {
  31. entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
  32. }
  33. [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  34. RCTAppSetupPrepareApp(application);
  35. RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
  36. #if RCT_NEW_ARCH_ENABLED
  37. _contextContainer = std::make_shared<facebook::react::ContextContainer const>();
  38. _reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();
  39. _contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
  40. _bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
  41. bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
  42. #endif
  43. NSDictionary *initProps = [self prepareInitialProps];
  44. UIView *rootView = [self.reactDelegate createRootViewWithBridge:bridge moduleName:@"taroDemo" initialProperties:initProps];
  45. if (@available(iOS 13.0, *)) {
  46. rootView.backgroundColor = [UIColor systemBackgroundColor];
  47. } else {
  48. rootView.backgroundColor = [UIColor whiteColor];
  49. }
  50. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  51. UIViewController *rootViewController = [self.reactDelegate createRootViewController];
  52. rootViewController.view = rootView;
  53. self.window.rootViewController = rootViewController;
  54. [self.window makeKeyAndVisible];
  55. [super application:application didFinishLaunchingWithOptions:launchOptions];
  56. return YES;
  57. }
  58. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  59. ///
  60. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  61. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  62. /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
  63. - (BOOL)concurrentRootEnabled
  64. {
  65. // Switch this bool to turn on and off the concurrent root
  66. return true;
  67. }
  68. - (NSDictionary *)prepareInitialProps
  69. {
  70. NSMutableDictionary *initProps = [NSMutableDictionary new];
  71. #ifdef RCT_NEW_ARCH_ENABLED
  72. initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
  73. #endif
  74. return initProps;
  75. }
  76. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  77. {
  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