NativeBridge.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. content2.categoryIdentifier = @"c12";
  103. UNTimeIntervalNotificationTrigger * trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:15 repeats:NO];
  104. NSString * identifier2 = @"e12";
  105. UNNotificationRequest * request2 = [UNNotificationRequest requestWithIdentifier:identifier2 content:content2 trigger:trigger2];
  106. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request2 withCompletionHandler:^(NSError * _Nullable error) {
  107. }];
  108. });
  109. }
  110. RCT_EXPORT_METHOD(addLocalPush:(id)detail){
  111. dispatch_async(dispatch_get_main_queue(), ^{
  112. //设置通知内容
  113. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  114. content.title = detail[@"title"];
  115. content.body = detail[@"body"];
  116. NSString *category_id = detail[@"category_id"];
  117. NSString *message_id = detail[@"id"];
  118. NSString *mode = detail[@"mode"];
  119. NSArray *actions;
  120. if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  121. //消息的处理按钮
  122. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  123. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  124. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  125. actions = @[action1,action2,action3];
  126. }
  127. else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){
  128. //消息的处理按钮
  129. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground];
  130. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground];
  131. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  132. actions = @[action1,action2,action3];
  133. }
  134. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  135. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  136. content.categoryIdentifier = category_id;
  137. if ([mode isEqualToString:@"RECURRING"]){
  138. //设置推送的触发机制
  139. NSArray *timeArray = [detail[@"recurring"][@"time"] componentsSeparatedByString:@":"];
  140. NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  141. NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  142. NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  143. // 2. 设置触发条件
  144. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  145. dateComponents.hour = hour;
  146. dateComponents.minute = minute;
  147. dateComponents.second = second;
  148. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  149. NSString * identifier = message_id;
  150. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  151. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  152. }];
  153. }
  154. else {
  155. NSTimeInterval timestamp = [detail[@"once"] doubleValue]/1000; // 毫秒级时间戳
  156. // 获取当前时间
  157. NSDate *currentDate = [NSDate date];
  158. // 计算当前时间与给定时间戳之间的时间差(秒)
  159. NSTimeInterval timeInterval = timestamp - [currentDate timeIntervalSince1970];
  160. UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
  161. NSString * identifier = message_id;
  162. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  163. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  164. }];
  165. }
  166. });
  167. }
  168. @end