Skip to content

Commit

Permalink
If grantPermission() is called when permission is already granted, re…
Browse files Browse the repository at this point in the history
…turn an error (rather than attempting to grant permission again which causes issues - see �#72).
  • Loading branch information
Dave Alden committed Aug 5, 2019
1 parent 90e77a9 commit 0a0dd89
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ @implementation FirebasePlugin

static NSInteger const kNotificationStackSize = 10;
static FirebasePlugin *firebasePlugin;
static BOOL registeredForRemoteNotifications = NO;

+ (FirebasePlugin *) firebasePlugin {
return firebasePlugin;
Expand All @@ -30,6 +31,9 @@ + (FirebasePlugin *) firebasePlugin {
- (void)pluginInitialize {
NSLog(@"Starting Firebase plugin");
firebasePlugin = self;

// Check for permission and register for remote notifications if granted
[self _hasPermission:^(BOOL result) {}];
}

- (void)getId:(CDVInvokedUrlCommand *)command {
Expand Down Expand Up @@ -59,42 +63,60 @@ - (void)getToken:(CDVInvokedUrlCommand *)command {
}

- (void)hasPermission:(CDVInvokedUrlCommand *)command {
[self _hasPermission:^(BOOL enabled) {
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:enabled];
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
}];
}

-(void)_hasPermission:(void (^)(BOOL result))completeBlock {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
BOOL enabled = NO;
if (settings.alertSetting == UNNotificationSettingEnabled) {
enabled = YES;
[self registerForRemoteNotifications];
}
NSLog(@"hasPermission: %@", enabled ? @"YES" : @"NO");
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:enabled];
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
NSLog(@"_hasPermission: %@", enabled ? @"YES" : @"NO");
completeBlock(enabled);
}];
}

- (void)grantPermission:(CDVInvokedUrlCommand *)command {
NSLog(@"grantPermission");
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate> _Nullable) self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"requestAuthorizationWithOptions: granted=%@", granted ? @"YES" : @"NO");
if(granted){
[self registerForRemoteNotifications];
[self _hasPermission:^(BOOL enabled) {
if(enabled){
NSString* message = @"Permission is already granted - call hasPermission() to check before calling grantPermission()";
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:message];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}else{
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate> _Nullable) self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"requestAuthorizationWithOptions: granted=%@", granted ? @"YES" : @"NO");
if(granted){
[self registerForRemoteNotifications];
}
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:granted] callbackId:command.callbackId];
}
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:granted] callbackId:command.callbackId];
];
}
];
}];
}

- (void)registerForRemoteNotifications {
NSLog(@"registerForRemoteNotifications");
if(registeredForRemoteNotifications) return;

if (![NSThread isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
registeredForRemoteNotifications = YES;
});
} else {
[[UIApplication sharedApplication] registerForRemoteNotifications];
registeredForRemoteNotifications = YES;
}
}

Expand Down

0 comments on commit 0a0dd89

Please sign in to comment.