// // 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"]; } }]; }); } @end