NativeBridge.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // NativeBridge.m
  3. // hola
  4. //
  5. // Created by Leon on 2024/4/28.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "NativeBridge.h"
  9. #import "AppDelegate.h"
  10. @interface NativeBridge()
  11. @end
  12. @implementation NativeBridge
  13. @synthesize bridge = _bridge;
  14. RCT_EXPORT_MODULE()
  15. - (instancetype)init {
  16. self = [super init];
  17. if (self) {
  18. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  19. appDelegate.nativeBridge = self;
  20. // 在这里进行初始化操作
  21. }
  22. return self;
  23. }
  24. -(NSArray<NSString *>*)supportedEvents
  25. {
  26. return @[@"receive"];
  27. //EventReminder 是监听的标识,类似 iOS 发通知 需要一个标识去识别,通过这个标识发送通知调用 RN方法
  28. }
  29. RCT_EXPORT_METHOD(rnPageLoaded){
  30. dispatch_async(dispatch_get_main_queue(), ^{
  31. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  32. appDelegate.rnLoaded = YES;
  33. });
  34. }
  35. RCT_EXPORT_METHOD(getNotificationAuthStatus){
  36. dispatch_async(dispatch_get_main_queue(), ^{
  37. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  38. [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  39. UNAuthorizationStatus status = settings.authorizationStatus;
  40. switch (status) {
  41. case UNAuthorizationStatusAuthorized:
  42. NSLog(@"用户已授权通知权限");
  43. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"authorized"];
  44. break;
  45. case UNAuthorizationStatusDenied:
  46. NSLog(@"用户已拒绝通知权限");
  47. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"denied"];
  48. break;
  49. case UNAuthorizationStatusNotDetermined:
  50. NSLog(@"通知权限未确定");
  51. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"not_determined"];
  52. break;
  53. default:
  54. break;
  55. }
  56. }];
  57. });
  58. }
  59. RCT_EXPORT_METHOD(authNotification){
  60. dispatch_async(dispatch_get_main_queue(), ^{
  61. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  62. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  63. if (granted) {
  64. [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"authorized"];
  65. dispatch_async(dispatch_get_main_queue(), ^{
  66. [[UIApplication sharedApplication] registerForRemoteNotifications];
  67. });
  68. } else {
  69. // 用户拒绝通知或发生错误
  70. [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"denied"];
  71. }
  72. }];
  73. });
  74. }
  75. RCT_EXPORT_METHOD(addLocalPush2:(id)detail){
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. //设置通知内容
  78. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  79. content.title = @"a1";
  80. content.body = @"b1";
  81. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW1" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  82. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START2" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  83. NSArray *actions = @[action1,action2];
  84. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"c1" actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  85. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  86. content.categoryIdentifier = @"c1";
  87. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
  88. NSString * identifier = @"e1";
  89. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  90. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  91. }];
  92. UNMutableNotificationContent * content2 = [[UNMutableNotificationContent alloc] init];
  93. content2.title = @"a12";
  94. content2.body = @"b12";
  95. NSArray *actions2;
  96. UNNotificationAction *action12 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  97. UNNotificationAction *action22 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  98. UNNotificationAction *action32 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  99. actions2 = @[action12,action22,action32];
  100. UNNotificationCategory *categroy2 = [UNNotificationCategory categoryWithIdentifier:@"c12" actions:actions2 intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  101. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy2]];
  102. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:@[categroy,categroy2]]];
  103. content2.categoryIdentifier = @"c12";
  104. UNTimeIntervalNotificationTrigger * trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:15 repeats:NO];
  105. NSString * identifier2 = @"e12";
  106. UNNotificationRequest * request2 = [UNNotificationRequest requestWithIdentifier:identifier2 content:content2 trigger:trigger2];
  107. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request2 withCompletionHandler:^(NSError * _Nullable error) {
  108. }];
  109. });
  110. }
  111. RCT_EXPORT_METHOD(addLocalPush:(id)array){
  112. dispatch_async(dispatch_get_main_queue(), ^{
  113. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  114. [center removeAllPendingNotificationRequests];
  115. NSMutableArray *list = [NSMutableArray new];
  116. for (int i=0;i<[array count];i++){
  117. id detail = array[i];
  118. NSString *mode = detail[@"mode"];
  119. if (![mode isEqualToString:@"RECURRING"]){
  120. NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳
  121. // 获取当前时间
  122. NSDate *currentDate = [NSDate date];
  123. // 计算当前时间与给定时间戳之间的时间差(秒)
  124. NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
  125. if (timeInterval<0){
  126. continue;
  127. }
  128. }
  129. //设置通知内容
  130. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  131. content.title = detail[@"title"];
  132. content.body = [NSString stringWithFormat:@"%@\nPress for actions >>",detail[@"body"]];
  133. NSString *category_id = detail[@"category_id"];
  134. NSString *message_id = detail[@"id"];
  135. NSArray *actions;
  136. if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  137. //消息的处理按钮
  138. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  139. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  140. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  141. actions = @[action1,action2,action3];
  142. }
  143. else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){
  144. //消息的处理按钮
  145. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground];
  146. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground];
  147. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  148. actions = @[action1,action2,action3];
  149. }
  150. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  151. [list addObject:categroy];
  152. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  153. content.categoryIdentifier = category_id;
  154. if ([mode isEqualToString:@"RECURRING"]){
  155. //设置推送的触发机制
  156. NSArray *timeArray = [detail[@"recurring"][@"time"] componentsSeparatedByString:@":"];
  157. NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  158. NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  159. NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  160. // 2. 设置触发条件
  161. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  162. dateComponents.hour = hour;
  163. dateComponents.minute = minute;
  164. dateComponents.second = second;
  165. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  166. NSString * identifier = message_id;
  167. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  168. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  169. }];
  170. }
  171. else {
  172. NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳
  173. // 获取当前时间
  174. NSDate *currentDate = [NSDate date];
  175. // 计算当前时间与给定时间戳之间的时间差(秒)
  176. NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
  177. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
  178. NSString * identifier = message_id;
  179. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  180. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  181. }];
  182. }
  183. }
  184. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:list]];
  185. });
  186. }
  187. RCT_EXPORT_METHOD(addLocalPush3:(id)detail){
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. //设置通知内容
  190. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  191. content.title = detail[@"title"];
  192. content.body = detail[@"body"];
  193. NSString *category_id = detail[@"category_id"];
  194. NSString *message_id = detail[@"id"];
  195. NSString *mode = detail[@"mode"];
  196. NSArray *actions;
  197. if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  198. //消息的处理按钮
  199. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  200. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  201. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  202. actions = @[action1,action2,action3];
  203. }
  204. else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){
  205. //消息的处理按钮
  206. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground];
  207. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground];
  208. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  209. actions = @[action1,action2,action3];
  210. }
  211. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  212. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  213. content.categoryIdentifier = category_id;
  214. if ([mode isEqualToString:@"RECURRING"]){
  215. //设置推送的触发机制
  216. NSArray *timeArray = [detail[@"recurring"][@"time"] componentsSeparatedByString:@":"];
  217. NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  218. NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  219. NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  220. // 2. 设置触发条件
  221. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  222. dateComponents.hour = hour;
  223. dateComponents.minute = minute;
  224. dateComponents.second = second;
  225. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  226. NSString * identifier = message_id;
  227. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  228. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  229. }];
  230. }
  231. else {
  232. NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳
  233. // 获取当前时间
  234. NSDate *currentDate = [NSDate date];
  235. // 计算当前时间与给定时间戳之间的时间差(秒)
  236. NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
  237. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
  238. NSString * identifier = message_id;
  239. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  240. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  241. }];
  242. }
  243. });
  244. }
  245. @end