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