|
|
|
|
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
|
BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
|
24
|
-
|
|
|
25
|
- if (isRegistered || isEnabled) {
|
|
|
26
|
- return isEnabled ? RNPStatusAuthorized : RNPStatusDenied;
|
|
|
|
|
23
|
+
|
|
|
24
|
+ if (isEnabled) {
|
|
|
25
|
+ return RNPStatusAuthorized;
|
27
|
} else {
|
26
|
} else {
|
28
|
return didAskForPermission ? RNPStatusDenied : RNPStatusUndetermined;
|
27
|
return didAskForPermission ? RNPStatusDenied : RNPStatusUndetermined;
|
29
|
}
|
28
|
}
|
30
|
}
|
29
|
}
|
31
|
|
30
|
|
|
|
31
|
+
|
32
|
- (void)request:(UIUserNotificationType)types completionHandler:(void (^)(NSString*))completionHandler
|
32
|
- (void)request:(UIUserNotificationType)types completionHandler:(void (^)(NSString*))completionHandler
|
33
|
{
|
33
|
{
|
34
|
NSString *status = [self.class getStatus];
|
34
|
NSString *status = [self.class getStatus];
|
35
|
-
|
|
|
|
|
35
|
+
|
36
|
if (status == RNPStatusUndetermined) {
|
36
|
if (status == RNPStatusUndetermined) {
|
37
|
self.completionHandler = completionHandler;
|
37
|
self.completionHandler = completionHandler;
|
38
|
-
|
|
|
|
|
38
|
+
|
39
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
39
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
40
|
selector:@selector(applicationDidBecomeActive)
|
40
|
selector:@selector(applicationDidBecomeActive)
|
41
|
name:UIApplicationDidBecomeActiveNotification
|
41
|
name:UIApplicationDidBecomeActiveNotification
|
42
|
object:nil];
|
42
|
object:nil];
|
43
|
-
|
|
|
|
|
43
|
+
|
44
|
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
|
44
|
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
|
45
|
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
|
45
|
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
|
46
|
[[UIApplication sharedApplication] registerForRemoteNotifications];
|
46
|
[[UIApplication sharedApplication] registerForRemoteNotifications];
|
47
|
-
|
|
|
|
|
47
|
+
|
48
|
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:RNPDidAskForNotification];
|
48
|
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:RNPDidAskForNotification];
|
49
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
49
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
50
|
} else {
|
50
|
} else {
|
|
|
|
|
57
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
57
|
[[NSNotificationCenter defaultCenter] removeObserver:self
|
58
|
name:UIApplicationDidBecomeActiveNotification
|
58
|
name:UIApplicationDidBecomeActiveNotification
|
59
|
object:nil];
|
59
|
object:nil];
|
60
|
-
|
|
|
|
|
60
|
+
|
61
|
if (self.completionHandler) {
|
61
|
if (self.completionHandler) {
|
62
|
//for some reason, checking permission right away returns denied. need to wait a tiny bit
|
62
|
//for some reason, checking permission right away returns denied. need to wait a tiny bit
|
63
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
63
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|