NativeBridge.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. -(NSArray<NSString *>*)supportedEvents
  16. {
  17. return @[@"receive"];
  18. //EventReminder 是监听的标识,类似 iOS 发通知 需要一个标识去识别,通过这个标识发送通知调用 RN方法
  19. }
  20. RCT_EXPORT_METHOD(getNotificationAuthStatus){
  21. dispatch_async(dispatch_get_main_queue(), ^{
  22. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  23. [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  24. UNAuthorizationStatus status = settings.authorizationStatus;
  25. switch (status) {
  26. case UNAuthorizationStatusAuthorized:
  27. NSLog(@"用户已授权通知权限");
  28. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"authorized"];
  29. break;
  30. case UNAuthorizationStatusDenied:
  31. NSLog(@"用户已拒绝通知权限");
  32. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"denied"];
  33. break;
  34. case UNAuthorizationStatusNotDetermined:
  35. NSLog(@"通知权限未确定");
  36. [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"not_determined"];
  37. break;
  38. default:
  39. break;
  40. }
  41. }];
  42. });
  43. }
  44. RCT_EXPORT_METHOD(authNotification){
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  47. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  48. if (granted) {
  49. [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"authorized"];
  50. dispatch_async(dispatch_get_main_queue(), ^{
  51. [[UIApplication sharedApplication] registerForRemoteNotifications];
  52. });
  53. } else {
  54. // 用户拒绝通知或发生错误
  55. [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"denied"];
  56. }
  57. }];
  58. });
  59. }
  60. RCT_EXPORT_METHOD(addLocalPush:(NSString *)title
  61. withBody:(NSString *)body
  62. withTime:(NSString *)time){
  63. dispatch_async(dispatch_get_main_queue(), ^{
  64. // 1. 设置 notification 内容
  65. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  66. content.title = title;
  67. content.body = body;
  68. content.sound = [UNNotificationSound defaultSound];
  69. // 按冒号分割字符串为数组
  70. NSArray *timeArray = [time componentsSeparatedByString:@":"];
  71. // 从数组中取出时分秒的值
  72. NSInteger hour = [[timeArray objectAtIndex:0] integerValue];
  73. NSInteger minute = [[timeArray objectAtIndex:1] integerValue];
  74. NSInteger second = [[timeArray objectAtIndex:2] integerValue];
  75. // 2. 设置触发条件 - 每天 9:00 AM
  76. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  77. dateComponents.hour = hour;
  78. dateComponents.minute = minute;
  79. UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
  80. // 3. 创建 notification request
  81. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder"
  82. content:content
  83. trigger:trigger];
  84. // 4. 添加 notification request 到通知中心
  85. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  86. [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  87. if (error != nil) {
  88. NSLog(@"Error adding notification request: %@", error);
  89. }
  90. }];
  91. });
  92. }
  93. @end