AppDelegate.mm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. self.rnLoaded = NO;
  31. NSDate *date = [NSDate date];
  32. //zone为当前时区信息 在我的程序中打印的是@"Asia/Shanghai"
  33. NSTimeZone *zone = [NSTimeZone systemTimeZone];
  34. //所在地区时间与协调世界时差距
  35. NSInteger interval = [zone secondsFromGMTForDate: date];
  36. //加上时差,得到本地时间
  37. NSDate *localeDate = [date dateByAddingTimeInterval: interval];
  38. // JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  39. // if (@available(iOS 12.0, *)) {
  40. // entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
  41. // }
  42. // [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  43. RCTAppSetupPrepareApp(application);
  44. RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
  45. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  46. center.delegate = self;
  47. #if RCT_NEW_ARCH_ENABLED
  48. _contextContainer = std::make_shared<facebook::react::ContextContainer const>();
  49. _reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();
  50. _contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
  51. _bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
  52. bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
  53. #endif
  54. NSDictionary *initProps = [self prepareInitialProps];
  55. UIView *rootView = [self.reactDelegate createRootViewWithBridge:bridge moduleName:@"taroDemo" initialProperties:initProps];
  56. if (@available(iOS 13.0, *)) {
  57. rootView.backgroundColor = [UIColor systemBackgroundColor];
  58. } else {
  59. rootView.backgroundColor = [UIColor blackColor];
  60. }
  61. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  62. self.window.backgroundColor = [UIColor blackColor];
  63. UIViewController *rootViewController = [self.reactDelegate createRootViewController];
  64. rootViewController.view = rootView;
  65. rootViewController.view.backgroundColor = [UIColor blackColor];
  66. self.window.rootViewController = rootViewController;
  67. [self.window makeKeyAndVisible];
  68. [super application:application didFinishLaunchingWithOptions:launchOptions];
  69. [self clearAllDeliveredNotifications];
  70. return YES;
  71. }
  72. - (void)registerForPushNotifications {
  73. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  74. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  75. if (granted) {
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. [[UIApplication sharedApplication] registerForRemoteNotifications];
  78. });
  79. } else {
  80. // 用户拒绝通知或发生错误
  81. }
  82. }];
  83. }
  84. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  85. ///
  86. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  87. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  88. /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
  89. - (BOOL)concurrentRootEnabled
  90. {
  91. // Switch this bool to turn on and off the concurrent root
  92. return true;
  93. }
  94. - (NSDictionary *)prepareInitialProps
  95. {
  96. NSMutableDictionary *initProps = [NSMutableDictionary new];
  97. #ifdef RCT_NEW_ARCH_ENABLED
  98. initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
  99. #endif
  100. return initProps;
  101. }
  102. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  103. {
  104. // return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  105. #if DEBUG
  106. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  107. #else
  108. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  109. #endif
  110. }
  111. #if RCT_NEW_ARCH_ENABLED
  112. #pragma mark - RCTCxxBridgeDelegate
  113. - (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
  114. {
  115. _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
  116. delegate:self
  117. jsInvoker:bridge.jsCallInvoker];
  118. return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
  119. }
  120. #pragma mark RCTTurboModuleManagerDelegate
  121. - (Class)getModuleClassFromName:(const char *)name
  122. {
  123. return RCTCoreModulesClassProvider(name);
  124. }
  125. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  126. jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
  127. {
  128. return nullptr;
  129. }
  130. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  131. initParams:
  132. (const facebook::react::ObjCTurboModule::InitParams &)params
  133. {
  134. return nullptr;
  135. }
  136. - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
  137. {
  138. return RCTAppSetupDefaultModuleFromClass(moduleClass);
  139. }
  140. #endif
  141. //************************************************JPush start************************************************
  142. //注册 APNS 成功并上报 DeviceToken
  143. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  144. // [JPUSHService registerDeviceToken:deviceToken];
  145. }
  146. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
  147. NSLog(@"get notification error");
  148. }
  149. //iOS 7 APNS
  150. - (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  151. // iOS 10 以下 Required
  152. NSLog(@"iOS 7 APNS");
  153. // [JPUSHService handleRemoteNotification:userInfo];
  154. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  155. completionHandler(UIBackgroundFetchResultNewData);
  156. }
  157. //iOS 10 前台收到消息
  158. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  159. NSDictionary * userInfo = notification.request.content.userInfo;
  160. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  161. // Apns
  162. NSLog(@"iOS 10 APNS 前台收到消息");
  163. // [JPUSHService handleRemoteNotification:userInfo];
  164. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  165. }
  166. else {
  167. // 本地通知 todo
  168. NSLog(@"iOS 10 本地通知 前台收到消息");
  169. // [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  170. }
  171. //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  172. completionHandler(UNNotificationPresentationOptionAlert);
  173. }
  174. - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
  175. }
  176. //再实现UNUserNotificationCenterDelegate代理的方法
  177. //- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  178. //{
  179. // //应用在前台时候接收到本地推送通知、远程推送通知调用此方法
  180. //}
  181. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
  182. {
  183. //应用程序在后台,用户通过点击本地推送、远程推送进入app时调用此方法
  184. self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(postNotificationData:) userInfo:response repeats:YES];
  185. }
  186. //iOS 10 消息事件回调
  187. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
  188. NSDictionary * userInfo = response.notification.request.content.userInfo;
  189. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  190. // Apns
  191. NSLog(@"iOS 10 APNS 消息事件回调");
  192. // [JPUSHService handleRemoteNotification:userInfo];
  193. // // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  194. // [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
  195. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
  196. }
  197. else {
  198. // 本地通知
  199. NSLog(@"iOS 10 本地通知 消息事件回调");
  200. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  201. // [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
  202. // [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
  203. }
  204. self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(postNotificationData:) userInfo:response repeats:YES];
  205. // NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
  206. // [self.nativeBridge.bridge.eventDispatcher sendAppEventWithName:@"notificationReceive" body:@{@"category_id":categoryIdentifier,@"action_id":response.actionIdentifier}];
  207. // 处理用户交互
  208. // if ([response.actionIdentifier isEqualToString:@"ALLOW_ACTION"]) {
  209. // // 用户点击了"允许"按钮,发起网络请求
  210. // NSLog(@"User allow the request");
  211. //
  212. //// [self.nativeBridge.bridge.eventDispatcher sendAppEventWithName:@"notificationReceive" body:@{@"name":@"1",@"value":@"2"}];
  213. //
  214. //// [self makeNetworkRequest];
  215. // } else if ([response.actionIdentifier isEqualToString:@"DENY_ACTION"]) {
  216. // // 用户点击了"拒绝"按钮
  217. // NSLog(@"User denied the request");
  218. //// [self makeNetworkRequest];
  219. // } else if ([response.actionIdentifier isEqualToString:@"START_TIMER_NOW"]){
  220. // if ([categoryIdentifier isEqualToString:@"REMINDER_FS_START_FAST"]){
  221. //
  222. // }
  223. // else if ([categoryIdentifier isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  224. //
  225. // }
  226. // } else if ([response.actionIdentifier isEqualToString:@"PICK_EARLIER_START"]){
  227. // if ([categoryIdentifier isEqualToString:@"REMINDER_FS_START_FAST"]){
  228. //
  229. // }
  230. // else if ([categoryIdentifier isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  231. //
  232. // }
  233. // }
  234. // else if ([response.actionIdentifier isEqualToString:@"END_TIMER_NOW"]){
  235. //
  236. // }
  237. // else if ([response.actionIdentifier isEqualToString:@"PICK_EARLIER_END"]){
  238. //
  239. // }
  240. // else if ([response.actionIdentifier isEqualToString:@"SKIP"]){
  241. //
  242. // }
  243. // 系统要求执行这个方法
  244. completionHandler();
  245. }
  246. - (void)postNotificationData:(NSTimer *)timerInfo{
  247. if (self.rnLoaded){
  248. UNNotificationResponse *response = timerInfo.userInfo;
  249. [self.timer invalidate];
  250. self.timer = nil;
  251. NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
  252. NSString *identifier = response.notification.request.identifier;
  253. [self.nativeBridge.bridge.eventDispatcher sendAppEventWithName:@"notificationReceive" body:@{@"category_id":categoryIdentifier,@"action_id":response.actionIdentifier,@"id":identifier}];
  254. [self clearAllDeliveredNotifications];
  255. }
  256. else {
  257. }
  258. }
  259. - (void)makeNetworkRequest{
  260. //https://api.fast.dev.liveplus.fun/api/static-resource-urls
  261. NSURL *url = [NSURL URLWithString:@"https://api.fast.dev.liveplus.fun/api/static-resource-urls"];
  262. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  263. NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  264. if (error) {
  265. NSLog(@"Error: %@", error.localizedDescription);
  266. } else {
  267. // 处理返回的数据
  268. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  269. NSLog(@"Response: %@", json);
  270. }
  271. }];
  272. [dataTask resume];
  273. }
  274. //自定义消息
  275. - (void)networkDidReceiveMessage:(NSNotification *)notification {
  276. NSDictionary * userInfo = [notification userInfo];
  277. // [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
  278. }
  279. - (void)demo{
  280. // 1. 设置 notification 内容
  281. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  282. content.title = @"Daily Reminder";
  283. content.body = @"It's 9:00 AM, time to start your day!";
  284. content.sound = [UNNotificationSound defaultSound];
  285. // 2. 设置触发条件 - 每天 9:00 AM
  286. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  287. dateComponents.hour = 9;
  288. dateComponents.minute = 0;
  289. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  290. // 3. 创建 notification request
  291. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder" content:content trigger:trigger];
  292. // 4. 添加 notification request 到通知中心
  293. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  294. [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  295. if (error != nil) {
  296. NSLog(@"Error adding notification request: %@", error);
  297. }
  298. }];
  299. }
  300. - (void)scheduleCalendarNotificationWithTitle:(NSString *)title body:(NSString *)body date:(NSDate *)date repeats:(BOOL)repeats identifier:(NSString *)identifier {
  301. NSLog(@"%s", __FUNCTION__);
  302. UNMutableNotificationContent *content = [UNMutableNotificationContent new];
  303. content.title = title;
  304. content.body = body;
  305. NSCalendar *calendar = NSCalendar.currentCalendar;
  306. NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
  307. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:repeats];
  308. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  309. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  310. if (error) {
  311. NSLog(@"%@", error);
  312. }
  313. }];
  314. }
  315. - (void)clearAllDeliveredNotifications {
  316. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  317. [center removeAllDeliveredNotifications];
  318. }
  319. @end