Browse Source

Fix when remote notifications are registered before user settings

For example, when you register the device for remote notifications to
enable silent pushes `content-available: 1` and later ask the user for
the push permission but this function incorrectly returns `denied`.
Alexander Jarvis 7 years ago
parent
commit
ca71450b26
1 changed files with 9 additions and 9 deletions
  1. 9
    9
      permissions/RNPNotification.m

+ 9
- 9
permissions/RNPNotification.m View File

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(), ^{