AppDelegate.mm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 <CoreLocation/CoreLocation.h>
  7. //#import <RCTJPushModule.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. #import <AVFoundation/AVFoundation.h>
  17. static NSString *const kRNConcurrentRoot = @"concurrentRoot";
  18. @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> {
  19. RCTTurboModuleManager *_turboModuleManager;
  20. RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
  21. std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
  22. facebook::react::ContextContainer::Shared _contextContainer;
  23. }
  24. @end
  25. #endif
  26. @interface AppDelegate()
  27. @property (nonatomic, strong) AVAudioPlayer *audioPlayer;
  28. @end
  29. @implementation AppDelegate
  30. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  31. {
  32. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenNotificationSettingsURLString]];
  33. // NSString *url = UIApplicationOpenNotificationSettingsURLString;
  34. // APNS
  35. // BOOL isLocation = [CLLocationManager locationServicesEnabled];
  36. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  37. [center removeAllPendingNotificationRequests];
  38. self.rnLoaded = NO;
  39. NSDate *date = [NSDate date];
  40. //zone为当前时区信息 在我的程序中打印的是@"Asia/Shanghai"
  41. NSTimeZone *zone = [NSTimeZone systemTimeZone];
  42. //所在地区时间与协调世界时差距
  43. NSInteger interval = [zone secondsFromGMTForDate: date];
  44. //加上时差,得到本地时间
  45. NSDate *localeDate = [date dateByAddingTimeInterval: interval];
  46. // JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  47. // if (@available(iOS 12.0, *)) {
  48. // entity.types = JPAuthorizationOptionNone; //JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSou//nd|JPAuthorizationOptionProvidesAppNotificationSettings;
  49. // }
  50. // [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  51. RCTAppSetupPrepareApp(application);
  52. RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
  53. // UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  54. center.delegate = self;
  55. #if RCT_NEW_ARCH_ENABLED
  56. _contextContainer = std::make_shared<facebook::react::ContextContainer const>();
  57. _reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();
  58. _contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
  59. _bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
  60. bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
  61. #endif
  62. NSDictionary *initProps = [self prepareInitialProps];
  63. UIView *rootView = [self.reactDelegate createRootViewWithBridge:bridge moduleName:@"taroDemo" initialProperties:initProps];
  64. if (@available(iOS 13.0, *)) {
  65. rootView.backgroundColor = [UIColor systemBackgroundColor];
  66. } else {
  67. rootView.backgroundColor = [UIColor blackColor];
  68. }
  69. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  70. self.window.backgroundColor = [UIColor blackColor];
  71. UIViewController *rootViewController = [self.reactDelegate createRootViewController];
  72. rootViewController.view = rootView;
  73. rootViewController.view.backgroundColor = [UIColor blackColor];
  74. self.window.rootViewController = rootViewController;
  75. [self.window makeKeyAndVisible];
  76. [super application:application didFinishLaunchingWithOptions:launchOptions];
  77. [self clearAllDeliveredNotifications];
  78. // [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(demoPush) userInfo:nil repeats:NO];
  79. // [self mixAudio];
  80. // [self demoPush];
  81. return YES;
  82. }
  83. -(void)demoPush{
  84. UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"actionIdentifier" title:@"Action Title" options:UNNotificationActionOptionAuthenticationRequired];
  85. // 接着定义一个分类,并将动作添加到分类中
  86. UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"categoryIdentifier" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  87. // 注册分类
  88. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];
  89. // 创建本地推送内容
  90. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  91. content.title = @"Notification Title";
  92. content.body = @"This is the notification body";
  93. content.categoryIdentifier = @"categoryIdentifier"; // 使用你刚刚定义的分类标识符
  94. // 创建推送请求
  95. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
  96. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notificationIdentifier" content:content trigger:trigger];
  97. // 将推送请求添加到通知中心
  98. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil];
  99. }
  100. - (void)mixAudio{
  101. NSError *error = nil;
  102. AVMutableComposition *composition = [AVMutableComposition composition];
  103. // 创建音轨
  104. AVMutableCompositionTrack *compositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
  105. // 声音文件路径
  106. NSString *soundFilePath1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"m4a"];
  107. NSString *soundFilePath2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"m4a"];
  108. // 加载音频文件
  109. AVAsset *asset1 = [AVAsset assetWithURL:[NSURL fileURLWithPath:soundFilePath1]];
  110. AVAsset *asset2 = [AVAsset assetWithURL:[NSURL fileURLWithPath:soundFilePath2]];
  111. // 获取音频文件的时长
  112. CMTimeRange timeRange1 = CMTimeRangeMake(kCMTimeZero, asset1.duration);
  113. CMTimeRange timeRange2 = CMTimeRangeMake(kCMTimeZero, asset2.duration);
  114. // 将音频文件的音轨添加到合成的音轨中
  115. [compositionTrack insertTimeRange:timeRange1 ofTrack:[[asset1 tracksWithMediaType:AVMediaTypeAudio] firstObject] atTime:kCMTimeZero error:&error];
  116. if (error) {
  117. // 错误处理
  118. NSLog(@"Error composing sound: %@", [error localizedDescription]);
  119. }
  120. // 在第一段声音之后继续添加第二段声音
  121. [compositionTrack insertTimeRange:timeRange2 ofTrack:[[asset2 tracksWithMediaType:AVMediaTypeAudio] firstObject] atTime:asset1.duration error:&error];
  122. if (error) {
  123. // 错误处理
  124. NSLog(@"Error composing sound: %@", [error localizedDescription]);
  125. }
  126. // 输出文件路径
  127. NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"outputSound.m4a"];
  128. NSFileManager *fileManager = [NSFileManager defaultManager];
  129. BOOL fileExists = [fileManager fileExistsAtPath:outputFilePath];
  130. if (fileExists) {
  131. // 文件存在,删除文件
  132. NSError *error;
  133. BOOL success = [fileManager removeItemAtPath:outputFilePath error:&error];
  134. if (success) {
  135. NSLog(@"文件删除成功");
  136. } else {
  137. // 处理错误
  138. NSLog(@"文件删除失败: %@", [error localizedDescription]);
  139. }
  140. } else {
  141. NSLog(@"文件不存在");
  142. }
  143. NSURL *outputURL = [NSURL fileURLWithPath:outputFilePath];
  144. // 导出合成的音频
  145. AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
  146. exportSession.outputURL = outputURL;
  147. exportSession.outputFileType = AVFileTypeAppleM4A;
  148. [exportSession exportAsynchronouslyWithCompletionHandler:^{
  149. if (exportSession.status == AVAssetExportSessionStatusCompleted) {
  150. // 导出成功,可以播放或者使用输出的文件
  151. NSLog(@"Export success: %@", outputFilePath);
  152. NSURL *fileURL = [NSURL fileURLWithPath:outputFilePath];
  153. NSError *error;
  154. self->_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
  155. if (error) {
  156. NSLog(@"Error creating audio player: %@", [error localizedDescription]);
  157. } else {
  158. [self->_audioPlayer play];
  159. }
  160. } else {
  161. // 导出失败,处理错误
  162. NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
  163. }
  164. }];
  165. }
  166. - (void)applicationDidBecomeActive:(UIApplication *)application {
  167. [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  168. }
  169. - (void)registerForPushNotifications {
  170. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  171. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  172. if (granted) {
  173. dispatch_async(dispatch_get_main_queue(), ^{
  174. [[UIApplication sharedApplication] registerForRemoteNotifications];
  175. });
  176. } else {
  177. // 用户拒绝通知或发生错误
  178. }
  179. }];
  180. }
  181. /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
  182. ///
  183. /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
  184. /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
  185. /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
  186. - (BOOL)concurrentRootEnabled
  187. {
  188. // Switch this bool to turn on and off the concurrent root
  189. return true;
  190. }
  191. - (NSDictionary *)prepareInitialProps
  192. {
  193. NSMutableDictionary *initProps = [NSMutableDictionary new];
  194. #ifdef RCT_NEW_ARCH_ENABLED
  195. initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
  196. #endif
  197. return initProps;
  198. }
  199. - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
  200. {
  201. // return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  202. #if DEBUGaa
  203. return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
  204. #else
  205. return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
  206. #endif
  207. }
  208. #if RCT_NEW_ARCH_ENABLED
  209. #pragma mark - RCTCxxBridgeDelegate
  210. - (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
  211. {
  212. _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
  213. delegate:self
  214. jsInvoker:bridge.jsCallInvoker];
  215. return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
  216. }
  217. #pragma mark RCTTurboModuleManagerDelegate
  218. - (Class)getModuleClassFromName:(const char *)name
  219. {
  220. return RCTCoreModulesClassProvider(name);
  221. }
  222. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  223. jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
  224. {
  225. return nullptr;
  226. }
  227. - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
  228. initParams:
  229. (const facebook::react::ObjCTurboModule::InitParams &)params
  230. {
  231. return nullptr;
  232. }
  233. - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
  234. {
  235. return RCTAppSetupDefaultModuleFromClass(moduleClass);
  236. }
  237. #endif
  238. //************************************************JPush start************************************************
  239. //注册 APNS 成功并上报 DeviceToken
  240. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  241. // [JPUSHService registerDeviceToken:deviceToken];
  242. }
  243. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
  244. NSLog(@"get notification error");
  245. }
  246. //iOS 7 APNS
  247. - (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  248. // iOS 10 以下 Required
  249. NSLog(@"iOS 7 APNS");
  250. // [JPUSHService handleRemoteNotification:userInfo];
  251. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  252. completionHandler(UIBackgroundFetchResultNewData);
  253. }
  254. //iOS 10 前台收到消息
  255. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  256. NSDictionary * userInfo = notification.request.content.userInfo;
  257. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  258. // Apns
  259. NSLog(@"iOS 10 APNS 前台收到消息");
  260. // [JPUSHService handleRemoteNotification:userInfo];
  261. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  262. }
  263. else {
  264. // 本地通知 todo
  265. NSLog(@"iOS 10 本地通知 前台收到消息");
  266. // [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
  267. }
  268. //需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
  269. completionHandler(UNNotificationPresentationOptionAlert);
  270. }
  271. - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
  272. }
  273. //再实现UNUserNotificationCenterDelegate代理的方法
  274. //- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  275. //{
  276. // //应用在前台时候接收到本地推送通知、远程推送通知调用此方法
  277. //}
  278. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
  279. {
  280. //应用程序在后台,用户通过点击本地推送、远程推送进入app时调用此方法
  281. self.timestamp = [[NSDate date] timeIntervalSince1970]*1000;
  282. self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(postNotificationData:) userInfo:response repeats:YES];
  283. }
  284. //iOS 10 消息事件回调
  285. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
  286. NSDictionary * userInfo = response.notification.request.content.userInfo;
  287. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  288. // Apns
  289. NSLog(@"iOS 10 APNS 消息事件回调");
  290. // [JPUSHService handleRemoteNotification:userInfo];
  291. // // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  292. // [[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
  293. // [[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
  294. }
  295. else {
  296. // 本地通知
  297. NSLog(@"iOS 10 本地通知 消息事件回调");
  298. // 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
  299. // [[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
  300. // [[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
  301. }
  302. self.timestamp = [[NSDate date] timeIntervalSince1970]*1000;
  303. self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(postNotificationData:) userInfo:response repeats:YES];
  304. // NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
  305. // [self.nativeBridge.bridge.eventDispatcher sendAppEventWithName:@"notificationReceive" body:@{@"category_id":categoryIdentifier,@"action_id":response.actionIdentifier}];
  306. // 处理用户交互
  307. // if ([response.actionIdentifier isEqualToString:@"ALLOW_ACTION"]) {
  308. // // 用户点击了"允许"按钮,发起网络请求
  309. // NSLog(@"User allow the request");
  310. //
  311. //// [self.nativeBridge.bridge.eventDispatcher sendAppEventWithName:@"notificationReceive" body:@{@"name":@"1",@"value":@"2"}];
  312. //
  313. //// [self makeNetworkRequest];
  314. // } else if ([response.actionIdentifier isEqualToString:@"DENY_ACTION"]) {
  315. // // 用户点击了"拒绝"按钮
  316. // NSLog(@"User denied the request");
  317. //// [self makeNetworkRequest];
  318. // } else if ([response.actionIdentifier isEqualToString:@"START_TIMER_NOW"]){
  319. // if ([categoryIdentifier isEqualToString:@"REMINDER_FS_START_FAST"]){
  320. //
  321. // }
  322. // else if ([categoryIdentifier isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  323. //
  324. // }
  325. // } else if ([response.actionIdentifier isEqualToString:@"PICK_EARLIER_START"]){
  326. // if ([categoryIdentifier isEqualToString:@"REMINDER_FS_START_FAST"]){
  327. //
  328. // }
  329. // else if ([categoryIdentifier isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  330. //
  331. // }
  332. // }
  333. // else if ([response.actionIdentifier isEqualToString:@"END_TIMER_NOW"]){
  334. //
  335. // }
  336. // else if ([response.actionIdentifier isEqualToString:@"PICK_EARLIER_END"]){
  337. //
  338. // }
  339. // else if ([response.actionIdentifier isEqualToString:@"SKIP"]){
  340. //
  341. // }
  342. // 系统要求执行这个方法
  343. completionHandler();
  344. }
  345. - (void)postNotificationData:(NSTimer *)timerInfo{
  346. if (self.rnLoaded){
  347. UNNotificationResponse *response = timerInfo.userInfo;
  348. [self.timer invalidate];
  349. self.timer = nil;
  350. NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
  351. NSString *identifier = response.notification.request.identifier;
  352. if ([identifier hasPrefix:@"REMINDER_FS_"]){
  353. NSString *strId = [identifier substringFromIndex:12];
  354. [self.nativeBridge.bridge.eventDispatcher sendAppEventWithName:@"notificationReceive" body:@{@"category_id":categoryIdentifier,@"action_id":response.actionIdentifier,@"id":strId,@"timestamp":@(self.timestamp)}];
  355. [self clearAllDeliveredNotifications];
  356. }
  357. }
  358. else {
  359. }
  360. }
  361. - (void)jumpNotificationSettingPage{
  362. if (self.rnLoaded){
  363. [self.timer invalidate];
  364. self.timer = nil;
  365. [self.nativeBridge.bridge.eventDispatcher sendAppEventWithName:@"openNotificationSetting" body:@{}];
  366. }
  367. else {
  368. }
  369. }
  370. - (void)makeNetworkRequest{
  371. //https://api.fast.dev.liveplus.fun/api/static-resource-urls
  372. NSURL *url = [NSURL URLWithString:@"https://api.fast.dev.liveplus.fun/api/static-resource-urls"];
  373. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  374. NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  375. if (error) {
  376. NSLog(@"Error: %@", error.localizedDescription);
  377. } else {
  378. // 处理返回的数据
  379. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  380. NSLog(@"Response: %@", json);
  381. }
  382. }];
  383. [dataTask resume];
  384. }
  385. //自定义消息
  386. - (void)networkDidReceiveMessage:(NSNotification *)notification {
  387. NSDictionary * userInfo = [notification userInfo];
  388. // [[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
  389. }
  390. - (void)demo{
  391. // 1. 设置 notification 内容
  392. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  393. content.title = @"Daily Reminder";
  394. content.body = @"It's 9:00 AM, time to start your day!";
  395. content.sound = [UNNotificationSound defaultSound];
  396. // 2. 设置触发条件 - 每天 9:00 AM
  397. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  398. dateComponents.hour = 9;
  399. dateComponents.minute = 0;
  400. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  401. // 3. 创建 notification request
  402. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder" content:content trigger:trigger];
  403. // 4. 添加 notification request 到通知中心
  404. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  405. [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  406. if (error != nil) {
  407. NSLog(@"Error adding notification request: %@", error);
  408. }
  409. }];
  410. }
  411. - (void)scheduleCalendarNotificationWithTitle:(NSString *)title body:(NSString *)body date:(NSDate *)date repeats:(BOOL)repeats identifier:(NSString *)identifier {
  412. NSLog(@"%s", __FUNCTION__);
  413. UNMutableNotificationContent *content = [UNMutableNotificationContent new];
  414. content.title = title;
  415. content.body = body;
  416. NSCalendar *calendar = NSCalendar.currentCalendar;
  417. NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
  418. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:repeats];
  419. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  420. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  421. if (error) {
  422. NSLog(@"%@", error);
  423. }
  424. }];
  425. }
  426. - (void)clearAllDeliveredNotifications {
  427. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  428. [center removeAllDeliveredNotifications];
  429. }
  430. - (void)userNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(nullable UNNotification *)notification{
  431. self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(jumpNotificationSettingPage) userInfo:nil repeats:YES];
  432. }
  433. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
  434. NSLog(@"%@",notification);
  435. // [UIApplication sharedApplication].applicationIconBadgeNumber = 100;
  436. completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
  437. }
  438. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  439. // 处理推送消息
  440. NSLog(@"Received a notification: %@", notification.alertBody);
  441. }
  442. @end