AppDelegate.mm 9.0 KB

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