|
|
|
|
19
|
+ (NSString *)getStatus
|
19
|
+ (NSString *)getStatus
|
20
|
{
|
20
|
{
|
21
|
BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:RNPDidAskForNotification];
|
21
|
BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:RNPDidAskForNotification];
|
|
|
22
|
+ BOOL isRegistered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
|
|
|
23
|
+ BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
|
22
|
|
24
|
|
23
|
- if (didAskForPermission) {
|
|
|
24
|
- if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
|
|
|
25
|
- // iOS8+
|
|
|
26
|
- BOOL isRegistered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
|
|
|
27
|
- BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
|
|
|
28
|
- if (isRegistered || isEnabled) {
|
|
|
29
|
- return isEnabled ? RNPStatusAuthorized : RNPStatusDenied;
|
|
|
30
|
- }
|
|
|
31
|
- else {
|
|
|
32
|
- return RNPStatusDenied;
|
|
|
33
|
- }
|
|
|
34
|
- } else {
|
|
|
35
|
- if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {
|
|
|
36
|
- return RNPStatusDenied;
|
|
|
37
|
- }
|
|
|
38
|
- else {
|
|
|
39
|
- return RNPStatusAuthorized;
|
|
|
40
|
- }
|
|
|
41
|
- }
|
|
|
|
|
25
|
+ if (isRegistered || isEnabled) {
|
|
|
26
|
+ return isEnabled ? RNPStatusAuthorized : RNPStatusDenied;
|
42
|
} else {
|
27
|
} else {
|
43
|
- return RNPStatusUndetermined;
|
|
|
|
|
28
|
+ return didAskForPermission ? RNPStatusDenied : RNPStatusUndetermined;
|
44
|
}
|
29
|
}
|
45
|
}
|
30
|
}
|
46
|
|
31
|
|
47
|
- (void)request:(UIUserNotificationType)types completionHandler:(void (^)(NSString*))completionHandler
|
32
|
- (void)request:(UIUserNotificationType)types completionHandler:(void (^)(NSString*))completionHandler
|
48
|
{
|
33
|
{
|
49
|
- BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:RNPDidAskForNotification];
|
|
|
50
|
- if (!didAskForPermission) {
|
|
|
|
|
34
|
+ NSString *status = [self.class getStatus];
|
|
|
35
|
+
|
|
|
36
|
+ if (status == RNPStatusUndetermined) {
|
51
|
self.completionHandler = completionHandler;
|
37
|
self.completionHandler = completionHandler;
|
52
|
|
38
|
|
53
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
39
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
|
55
|
name:UIApplicationDidBecomeActiveNotification
|
41
|
name:UIApplicationDidBecomeActiveNotification
|
56
|
object:nil];
|
42
|
object:nil];
|
57
|
|
43
|
|
58
|
- if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
|
|
|
59
|
- // iOS8+
|
|
|
60
|
- UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
|
|
|
61
|
- [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
|
|
|
62
|
- [[UIApplication sharedApplication] registerForRemoteNotifications];
|
|
|
63
|
- } else {
|
|
|
64
|
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)types];
|
|
|
65
|
- }
|
|
|
|
|
44
|
+ UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
|
|
|
45
|
+ [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
|
|
|
46
|
+ [[UIApplication sharedApplication] registerForRemoteNotifications];
|
66
|
|
47
|
|
67
|
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:RNPDidAskForNotification];
|
48
|
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:RNPDidAskForNotification];
|
68
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
49
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
69
|
} else {
|
50
|
} else {
|
70
|
- completionHandler([self.class getStatus]);
|
|
|
|
|
51
|
+ completionHandler(status);
|
71
|
}
|
52
|
}
|
72
|
}
|
53
|
}
|
73
|
|
54
|
|