RNPermissionHandlerNotifications.m 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #import "RNPermissionHandlerNotifications.h"
  2. @import UserNotifications;
  3. @import UIKit;
  4. @interface RNPermissionHandlerNotifications()
  5. @property (nonatomic, strong) void (^resolve)(RNPermissionStatus status);
  6. @property (nonatomic, strong) void (^reject)(NSError *error);
  7. @end
  8. @implementation RNPermissionHandlerNotifications
  9. + (NSArray<NSString *> * _Nonnull)usageDescriptionKeys {
  10. return @[];
  11. }
  12. + (NSString * _Nonnull)handlerUniqueId {
  13. return @"ios.permission.NOTIFICATIONS";
  14. }
  15. - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  16. rejecter:(void (__unused ^ _Nonnull)(NSError * _Nonnull))reject {
  17. if (@available(iOS 10.0, *)) {
  18. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  19. [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  20. switch (settings.authorizationStatus) {
  21. case UNAuthorizationStatusNotDetermined:
  22. #ifdef __IPHONE_12_0
  23. case UNAuthorizationStatusProvisional:
  24. #endif
  25. return resolve(RNPermissionStatusNotDetermined);
  26. case UNAuthorizationStatusDenied:
  27. return resolve(RNPermissionStatusDenied);
  28. case UNAuthorizationStatusAuthorized:
  29. return resolve(RNPermissionStatusAuthorized);
  30. }
  31. }];
  32. } else {
  33. UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
  34. if ([settings types] != UIUserNotificationTypeNone) {
  35. return resolve(RNPermissionStatusAuthorized);
  36. } else if ([RNPermissions hasAlreadyBeenRequested:self]) {
  37. return resolve(RNPermissionStatusDenied);
  38. } else {
  39. return resolve(RNPermissionStatusNotDetermined);
  40. }
  41. }
  42. }
  43. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  44. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  45. NSArray<NSString *> *options = [[NSArray alloc] initWithObjects:@"alert", @"badge", @"sound", nil];
  46. [self requestWithResolver:resolve rejecter:reject options:options];
  47. }
  48. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  49. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject
  50. options:(NSArray<NSString *> * _Nonnull)options {
  51. bool alert = [options containsObject:@"alert"];
  52. bool badge = [options containsObject:@"badge"];
  53. bool sound = [options containsObject:@"sound"];
  54. bool criticalAlert = [options containsObject:@"criticalAlert"];
  55. bool carPlay = [options containsObject:@"carPlay"];
  56. bool provisional = [options containsObject:@"provisional"];
  57. if (@available(iOS 10.0, *)) {
  58. UNAuthorizationOptions types = UNAuthorizationOptionNone;
  59. if (alert) types += UNAuthorizationOptionAlert;
  60. if (badge) types += UNAuthorizationOptionBadge;
  61. if (sound) types += UNAuthorizationOptionSound;
  62. if (carPlay) types += UNAuthorizationOptionCarPlay;
  63. if (@available(iOS 12.0, *)) {
  64. if (criticalAlert) types += UNAuthorizationOptionCriticalAlert;
  65. if (provisional) types += UNAuthorizationOptionProvisional;
  66. }
  67. if (!alert && !badge && !sound && !criticalAlert && !carPlay && !provisional) {
  68. types += UNAuthorizationOptionAlert;
  69. types += UNAuthorizationOptionBadge;
  70. types += UNAuthorizationOptionSound;
  71. }
  72. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  73. [center requestAuthorizationWithOptions:types
  74. completionHandler:^(BOOL granted, NSError * _Nullable error) {
  75. if (error != nil) {
  76. return reject(error);
  77. }
  78. if (granted) {
  79. dispatch_async(dispatch_get_main_queue(), ^{
  80. [[UIApplication sharedApplication] registerForRemoteNotifications];
  81. });
  82. }
  83. [self checkWithResolver:resolve rejecter:reject];
  84. }];
  85. } else {
  86. UIUserNotificationType types = UIUserNotificationTypeNone;
  87. if (alert) types += UIUserNotificationTypeAlert;
  88. if (badge) types += UIUserNotificationTypeBadge;
  89. if (sound) types += UIUserNotificationTypeSound;
  90. if (!alert && !badge && !sound) {
  91. types += UIUserNotificationTypeAlert;
  92. types += UIUserNotificationTypeBadge;
  93. types += UIUserNotificationTypeSound;
  94. }
  95. _resolve = resolve;
  96. _reject = reject;
  97. [[NSNotificationCenter defaultCenter] addObserver:self
  98. selector:@selector(onApplicationDidBecomeActive)
  99. name:UIApplicationDidBecomeActiveNotification
  100. object:nil];
  101. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
  102. [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  103. [[UIApplication sharedApplication] registerForRemoteNotifications];
  104. }
  105. }
  106. - (void)onApplicationDidBecomeActive {
  107. [[NSNotificationCenter defaultCenter] removeObserver:self
  108. name:UIApplicationDidBecomeActiveNotification
  109. object:nil];
  110. [self checkWithResolver:_resolve rejecter:_reject];
  111. }
  112. - (void)getSettingsWithResolver:(void (^ _Nonnull)(NSDictionary * _Nonnull settings))resolve {
  113. if (@available(iOS 10.0, *)) {
  114. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  115. [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  116. NSMutableDictionary *result = [NSMutableDictionary new];
  117. bool alert = settings.alertSetting == UNNotificationSettingEnabled;
  118. bool badge = settings.badgeSetting == UNNotificationSettingEnabled;
  119. bool sound = settings.soundSetting == UNNotificationSettingEnabled;
  120. bool lockScreen = settings.lockScreenSetting == UNNotificationSettingEnabled;
  121. bool carPlay = settings.carPlaySetting == UNNotificationSettingEnabled;
  122. if (settings.alertSetting != UNNotificationSettingNotSupported)
  123. [result setValue:@(alert) forKey:@"alert"];
  124. if (settings.badgeSetting != UNNotificationSettingNotSupported)
  125. [result setValue:@(badge) forKey:@"badge"];
  126. if (settings.soundSetting != UNNotificationSettingNotSupported)
  127. [result setValue:@(sound) forKey:@"sound"];
  128. if (settings.lockScreenSetting != UNNotificationSettingNotSupported)
  129. [result setValue:@(lockScreen) forKey:@"lockScreen"];
  130. if (settings.carPlaySetting != UNNotificationSettingNotSupported)
  131. [result setValue:@(carPlay) forKey:@"carPlay"];
  132. if (@available(iOS 12.0, *)) {
  133. bool criticalAlert = settings.criticalAlertSetting == UNNotificationSettingEnabled;
  134. if (settings.criticalAlertSetting != UNNotificationSettingNotSupported)
  135. [result setValue:@(criticalAlert) forKey:@"criticalAlert"];
  136. }
  137. resolve(result);
  138. }];
  139. } else {
  140. UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
  141. resolve(@{
  142. @"alert": @((bool)(types & UIUserNotificationTypeAlert)),
  143. @"badge": @((bool)(types & UIUserNotificationTypeBadge)),
  144. @"sound": @((bool)(types & UIUserNotificationTypeSound)),
  145. });
  146. }
  147. }
  148. @end