From 260912bf94602913388196d8ec8d64ded418a1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berna=CC=81t?= Date: Thu, 24 Oct 2019 10:53:03 +0200 Subject: [PATCH] enable automatic permission request by user for notifications when checkpermission called --- src/ios/AppDelegate+notification.h | 1 + src/ios/AppDelegate+notification.m | 8 ++++++++ src/ios/PushPlugin.m | 17 +++++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ios/AppDelegate+notification.h b/src/ios/AppDelegate+notification.h index 9479b1839..217478810 100644 --- a/src/ios/AppDelegate+notification.h +++ b/src/ios/AppDelegate+notification.h @@ -17,6 +17,7 @@ extern NSString *const pushPluginApplicationDidBecomeActiveNotification; - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler; - (void)pushPluginOnApplicationDidBecomeActive:(UIApplication *)application; - (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler; +- (void)requestUserPermissionRemoteNotificationsWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler; - (id) getCommandInstance:(NSString*)className; @property (nonatomic, retain) NSDictionary *launchNotification; diff --git a/src/ios/AppDelegate+notification.m b/src/ios/AppDelegate+notification.m index b04c47687..d559109de 100644 --- a/src/ios/AppDelegate+notification.m +++ b/src/ios/AppDelegate+notification.m @@ -151,6 +151,14 @@ - (void)checkUserHasRemoteNotificationsEnabledWithCompletionHandler:(nonnull voi }]; } +- (void)requestUserPermissionRemoteNotificationsWithCompletionHandler:(nonnull void (^)(BOOL))completionHandler +{ + UNAuthorizationOptions options = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert); + [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError* e) { + completionHandler(granted); + }]; +} + - (void)pushPluginOnApplicationDidBecomeActive:(NSNotification *)notification { NSLog(@"active"); diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 9aca1d0d9..ad66919ea 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -544,10 +544,19 @@ - (void)hasPermission:(CDVInvokedUrlCommand *)command id appDelegate = [UIApplication sharedApplication].delegate; if ([appDelegate respondsToSelector:@selector(checkUserHasRemoteNotificationsEnabledWithCompletionHandler:)]) { [appDelegate performSelector:@selector(checkUserHasRemoteNotificationsEnabledWithCompletionHandler:) withObject:^(BOOL isEnabled) { - NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:1]; - [message setObject:[NSNumber numberWithBool:isEnabled] forKey:@"isEnabled"]; - CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message]; - [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; + if(!isEnabled){ + [appDelegate performSelector:@selector(requestUserPermissionRemoteNotificationsWithCompletionHandler:) withObject:^(BOOL granted) { + NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:1]; + [message setObject:[NSNumber numberWithBool:granted] forKey:@"isEnabled"]; + CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message]; + [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; + }]; + }else{ + NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:1]; + [message setObject:[NSNumber numberWithBool:isEnabled] forKey:@"isEnabled"]; + CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message]; + [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; + } }]; } }