NativeBridge.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. @end