// // NativeBridge.m // hola // // Created by Leon on 2024/4/28. // #import #import "NativeBridge.h" #import "AppDelegate.h" @interface NativeBridge() @end @implementation NativeBridge @synthesize bridge = _bridge; RCT_EXPORT_MODULE() -(NSArray*)supportedEvents { return @[@"receive"]; //EventReminder 是监听的标识,类似 iOS 发通知 需要一个标识去识别,通过这个标识发送通知调用 RN方法 } RCT_EXPORT_METHOD(getNotificationAuthStatus){ dispatch_async(dispatch_get_main_queue(), ^{ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { UNAuthorizationStatus status = settings.authorizationStatus; switch (status) { case UNAuthorizationStatusAuthorized: NSLog(@"用户已授权通知权限"); [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"authorized"]; break; case UNAuthorizationStatusDenied: NSLog(@"用户已拒绝通知权限"); [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"denied"]; break; case UNAuthorizationStatusNotDetermined: NSLog(@"通知权限未确定"); [self.bridge.eventDispatcher sendAppEventWithName:@"notificationResult" body:@"not_determined"]; break; default: break; } }]; }); } RCT_EXPORT_METHOD(authNotification){ dispatch_async(dispatch_get_main_queue(), ^{ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"authorized"]; dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; }); } else { // 用户拒绝通知或发生错误 [self.bridge.eventDispatcher sendAppEventWithName:@"operateNotificationResult" body:@"denied"]; } }]; }); } RCT_EXPORT_METHOD(addLocalPush:(NSString *)title withBody:(NSString *)body withTime:(NSString *)time){ dispatch_async(dispatch_get_main_queue(), ^{ // 1. 设置 notification 内容 UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = title; content.body = body; content.sound = [UNNotificationSound defaultSound]; // 按冒号分割字符串为数组 NSArray *timeArray = [time componentsSeparatedByString:@":"]; // 从数组中取出时分秒的值 NSInteger hour = [[timeArray objectAtIndex:0] integerValue]; NSInteger minute = [[timeArray objectAtIndex:1] integerValue]; NSInteger second = [[timeArray objectAtIndex:2] integerValue]; // 2. 设置触发条件 - 每天 9:00 AM NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; dateComponents.hour = hour; dateComponents.minute = minute; UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES]; // 3. 创建 notification request UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"DailyReminder" content:content trigger:trigger]; // 4. 添加 notification request 到通知中心 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Error adding notification request: %@", error); } }]; }); } @end