Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

enable automatic permission request by user for notifications when ch… #2843

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ios/AppDelegate+notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/ios/AppDelegate+notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
17 changes: 13 additions & 4 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,19 @@ - (void)hasPermission:(CDVInvokedUrlCommand *)command
id<UIApplicationDelegate> 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];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should try to avoid dublicate code, because these 4 lines are the same if already granted or the completionHandler is called.

[message setObject:[NSNumber numberWithBool:isEnabled] forKey:@"isEnabled"];
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message];
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
}
}];
}
}
Expand Down