|
@@ -19,35 +19,21 @@ static NSString* RNPDidAskForNotification = @"RNPDidAskForNotification";
|
|
|
+ (NSString *)getStatus
|
|
+ (NSString *)getStatus
|
|
|
{
|
|
{
|
|
|
BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:RNPDidAskForNotification];
|
|
BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:RNPDidAskForNotification];
|
|
|
|
|
+ BOOL isRegistered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
|
|
|
|
|
+ BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
|
|
|
|
|
|
|
|
- if (didAskForPermission) {
|
|
|
|
|
- if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
|
|
|
|
|
- // iOS8+
|
|
|
|
|
- BOOL isRegistered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
|
|
|
|
|
- BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
|
|
|
|
|
- if (isRegistered || isEnabled) {
|
|
|
|
|
- return isEnabled ? RNPStatusAuthorized : RNPStatusDenied;
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- return RNPStatusDenied;
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {
|
|
|
|
|
- return RNPStatusDenied;
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- return RNPStatusAuthorized;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (isRegistered || isEnabled) {
|
|
|
|
|
+ return isEnabled ? RNPStatusAuthorized : RNPStatusDenied;
|
|
|
} else {
|
|
} else {
|
|
|
- return RNPStatusUndetermined;
|
|
|
|
|
|
|
+ return didAskForPermission ? RNPStatusDenied : RNPStatusUndetermined;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- (void)request:(UIUserNotificationType)types completionHandler:(void (^)(NSString*))completionHandler
|
|
- (void)request:(UIUserNotificationType)types completionHandler:(void (^)(NSString*))completionHandler
|
|
|
{
|
|
{
|
|
|
- BOOL didAskForPermission = [[NSUserDefaults standardUserDefaults] boolForKey:RNPDidAskForNotification];
|
|
|
|
|
- if (!didAskForPermission) {
|
|
|
|
|
|
|
+ NSString *status = [self.class getStatus];
|
|
|
|
|
+
|
|
|
|
|
+ if (status == RNPStatusUndetermined) {
|
|
|
self.completionHandler = completionHandler;
|
|
self.completionHandler = completionHandler;
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
@@ -55,19 +41,14 @@ static NSString* RNPDidAskForNotification = @"RNPDidAskForNotification";
|
|
|
name:UIApplicationDidBecomeActiveNotification
|
|
name:UIApplicationDidBecomeActiveNotification
|
|
|
object:nil];
|
|
object:nil];
|
|
|
|
|
|
|
|
- if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
|
|
|
|
|
- // iOS8+
|
|
|
|
|
- UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
|
|
|
|
|
- [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
|
|
|
|
|
- [[UIApplication sharedApplication] registerForRemoteNotifications];
|
|
|
|
|
- } else {
|
|
|
|
|
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)types];
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
|
|
|
|
|
+ [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
|
|
|
|
|
+ [[UIApplication sharedApplication] registerForRemoteNotifications];
|
|
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:RNPDidAskForNotification];
|
|
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:RNPDidAskForNotification];
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
} else {
|
|
} else {
|
|
|
- completionHandler([self.class getStatus]);
|
|
|
|
|
|
|
+ completionHandler(status);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|