| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- //
- // NativeBridge.m
- // hola
- //
- // Created by Leon on 2024/4/28.
- //
- #import <Foundation/Foundation.h>
- #import "NativeBridge.h"
- #import "AppDelegate.h"
- @interface NativeBridge()
- @end
- @implementation NativeBridge
- @synthesize bridge = _bridge;
- RCT_EXPORT_MODULE()
- -(NSArray<NSString *>*)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
|