NativeBridge.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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(addLocalPush:(NSString *)title
  76. withBody:(NSString *)body
  77. withTime:(NSString *)time
  78. withDetail:(id)detail){
  79. dispatch_async(dispatch_get_main_queue(), ^{
  80. //设置通知内容
  81. UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  82. content.title = title;
  83. content.body = body;
  84. NSString *category_id = detail[@"category_id"];
  85. NSString *message_id = detail[@"id"];
  86. NSString *mode = detail[@"mode"];
  87. NSArray *actions;
  88. if ([category_id isEqualToString:@"REMINDER_FS_START_FAST"]||[category_id isEqualToString:@"REMINDER_FS_START_SLEEP"]){
  89. //消息的处理按钮
  90. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"Start timer now with 1-tap" options:UNNotificationActionOptionForeground];
  91. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"Pick an earlier start" options:UNNotificationActionOptionForeground];
  92. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"Skip" options:UNNotificationActionOptionAuthenticationRequired];
  93. actions = @[action1,action2,action3];
  94. }
  95. else if([category_id isEqualToString:@"REMINDER_FS_END_FAST"]||[category_id isEqualToString:@"REMINDER_FS_END_SLEEP"]){
  96. //消息的处理按钮
  97. UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"END_TIMER_NOW" title:@"End timer now with 1-tap" options:UNNotificationActionOptionForeground];
  98. UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_END" title:@"Pick an earlier end" options:UNNotificationActionOptionForeground];
  99. UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"Skip" title:@"SKIP" options:UNNotificationActionOptionAuthenticationRequired];
  100. actions = @[action1,action2,action3];
  101. }
  102. UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:category_id actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  103. [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  104. content.categoryIdentifier = category_id;
  105. //设置推送的触发机制
  106. NSArray *timeArray = [@"19:07:00" componentsSeparatedByString:@":"];
  107. NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  108. NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  109. NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  110. // 2. 设置触发条件 - 每天 9:00 AM
  111. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  112. dateComponents.hour = hour;
  113. dateComponents.minute = minute;
  114. dateComponents.second = second;
  115. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  116. NSString * identifier = @"notification_onlytextid";
  117. UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  118. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  119. }];
  120. // // 1. 设置 notification 内容
  121. // UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  122. // content.title = title;
  123. // content.body = body;
  124. // content.sound = [UNNotificationSound defaultSound];
  125. //
  126. // // 按冒号分割字符串为数组
  127. // NSArray *timeArray = [@"10:51:00" componentsSeparatedByString:@":"];
  128. //
  129. // // 从数组中取出时分秒的值
  130. // NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  131. // NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  132. // NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  133. //
  134. // // 2. 设置触发条件 - 每天 9:00 AM
  135. // NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  136. // dateComponents.hour = hour;
  137. // dateComponents.minute = minute;
  138. // dateComponents.second = second;
  139. // UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  140. //
  141. // // 3. 创建 notification request
  142. // UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder" content:content trigger:trigger];
  143. //
  144. // // 4. 添加ations
  145. // // 创建允许按钮动作
  146. // UNNotificationAction *allowAction = [UNNotificationAction actionWithIdentifier:@"ALLOW_ACTION" title:@"Allow" options:UNNotificationActionOptionForeground];
  147. //
  148. // // 创建拒绝按钮动作
  149. // UNNotificationAction *denyAction = [UNNotificationAction actionWithIdentifier:@"DENY_ACTION" title:@"Deny" options:UNNotificationActionOptionDestructive];
  150. //
  151. // // 创建动作集合
  152. // UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"NOTIFICATION_CATEGORY" actions:@[allowAction, denyAction] intentIdentifiers:@[@"ALLOW_ACTION",@"DENY_ACTION"] options:UNNotificationCategoryOptionCustomDismissAction];
  153. //
  154. // // 5. 添加 notification request 到通知中心
  155. // UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  156. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];
  157. //
  158. // [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  159. // if (error != nil) {
  160. // NSLog(@"Error adding notification request: %@", error);
  161. // }
  162. // }];
  163. });
  164. }
  165. //RCT_EXPORT_METHOD(addLocalPush:(NSString *)title
  166. // withBody:(NSString *)body
  167. // withTime:(NSString *)time
  168. // withDetail:(id)detail){
  169. // dispatch_async(dispatch_get_main_queue(), ^{
  170. // //设置通知内容
  171. // UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
  172. // content.title = title;
  173. // content.body = body;
  174. // // content.subtitle = @"推送的子标题";
  175. // // content.badge = [NSNumber numberWithInteger:0];// 红标
  176. // // content.sound = [UNNotificationSound defaultSound];
  177. // //[UNNotificationSound soundNamed:@""]; 自定义推送声音
  178. //
  179. // // //给通知添加本地图片或者视频,写法同下
  180. // // NSString *path = [[NSBundle mainBundle] pathForResource:@"rank2" ofType:@"png"];
  181. // // NSError *error = nil;
  182. // // /*
  183. // // UNNotificationAttachment是指可以包含音频,图像或视频内容。使用本地通知时,可以在通知创建时,将附件加入即可。对于远程通知,则必须实现使用UNNotificationServiceExtension类通知服务扩展。
  184. // // */
  185. // // UNNotificationAttachment *img_attachment = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
  186. // // if (error) {
  187. // // NSLog(@"%@", error);
  188. // // }
  189. // // content.attachments = @[img_attachment];//默认只显示第一个
  190. // // content.launchImageName = @"launch2";
  191. //
  192. // //消息的处理按钮
  193. // UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"START_TIMER_NOW" title:@"START_TIMER_NOW" options:UNNotificationActionOptionForeground];
  194. //
  195. // UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"PICK_EARLIER_START" title:@"PICK_EARLIER_START" options:UNNotificationActionOptionForeground];
  196. //
  197. // UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"SKIP" title:@"SKIP" options:UNNotificationActionOptionAuthenticationRequired];
  198. //
  199. // UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"REMINDER_FS_START_FAST" actions:@[action1,action2,action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
  200. //
  201. // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
  202. // content.categoryIdentifier = @"REMINDER_FS_START_FAST";
  203. //
  204. // //设置推送的触发机制
  205. // UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
  206. //
  207. // //日历形式,
  208. // // NSDateComponents * dateComp = [[NSDateComponents alloc] init];
  209. // // dateComp.weekday = 2;//周一,默认第一天是周日
  210. // // dateComp.month = 12;//月份,您可以在这里设置具体的时分秒
  211. // // dateComp.hour = 10;//小时
  212. // // dateComp.minute = 14;//分钟
  213. // // UNCalendarNotificationTrigger * calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComp repeats:YES];
  214. //
  215. // //地理位置推送.没实际应用过
  216. // // CLLocationCoordinate2D location ;
  217. // // location.latitude = 123;
  218. // // location.longitude = 111;
  219. // //
  220. // // CLCircularRegion * region = [[CLCircularRegion alloc] initWithCenter:location radius:100.0 identifier:@"regionid"];
  221. // //
  222. // // UNLocationNotificationTrigger * locationTrigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:YES];
  223. //
  224. // NSString * identifier = @"notification_onlytextid";
  225. // UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  226. // [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  227. //
  228. // }];
  229. //
  230. // // // 1. 设置 notification 内容
  231. // // UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  232. // // content.title = title;
  233. // // content.body = body;
  234. // // content.sound = [UNNotificationSound defaultSound];
  235. // //
  236. // // // 按冒号分割字符串为数组
  237. // // NSArray *timeArray = [@"10:51:00" componentsSeparatedByString:@":"];
  238. // //
  239. // // // 从数组中取出时分秒的值
  240. // // NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  241. // // NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  242. // // NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  243. // //
  244. // // // 2. 设置触发条件 - 每天 9:00 AM
  245. // // NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  246. // // dateComponents.hour = hour;
  247. // // dateComponents.minute = minute;
  248. // // dateComponents.second = second;
  249. // // UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  250. // //
  251. // // // 3. 创建 notification request
  252. // // UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder" content:content trigger:trigger];
  253. // //
  254. // // // 4. 添加ations
  255. // // // 创建允许按钮动作
  256. // // UNNotificationAction *allowAction = [UNNotificationAction actionWithIdentifier:@"ALLOW_ACTION" title:@"Allow" options:UNNotificationActionOptionForeground];
  257. // //
  258. // // // 创建拒绝按钮动作
  259. // // UNNotificationAction *denyAction = [UNNotificationAction actionWithIdentifier:@"DENY_ACTION" title:@"Deny" options:UNNotificationActionOptionDestructive];
  260. // //
  261. // // // 创建动作集合
  262. // // UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"NOTIFICATION_CATEGORY" actions:@[allowAction, denyAction] intentIdentifiers:@[@"ALLOW_ACTION",@"DENY_ACTION"] options:UNNotificationCategoryOptionCustomDismissAction];
  263. // //
  264. // // // 5. 添加 notification request 到通知中心
  265. // // UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  266. // // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];
  267. // //
  268. // // [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  269. // // if (error != nil) {
  270. // // NSLog(@"Error adding notification request: %@", error);
  271. // // }
  272. // // }];
  273. // });
  274. //}
  275. @end