Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(ios) Fix foreground notifications not being displayed after calling grantPermission when permissions haven't been previously granted #800

Merged
merged 1 commit into from
May 22, 2023

Conversation

vneenz
Copy link

@vneenz vneenz commented Apr 6, 2023

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Documentation changes
  • Other... Please describe:

PR Checklist

Please check your PR fulfills the following requirements:

Bugfixes:

  • Regression testing has been carried out using the example project to ensure the intended bug is fixed and no regression bugs have been inadvertently introduced.

What is the purpose of this PR?

Currently, calling grantPermission on ios while permissions have not been granted will cause received foreground notifications (notifications with "notification_foreground": "true") to not be displayed until app is restarted. This seems to happen because the grantPermission function changes the notification center delegate if called and permissions have not been already granted:

[self _hasPermission:^(BOOL enabled) {
@try {
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;

Meanwhile the completionHandler for the permission request never sets this back to the original value

completionHandler:^(BOOL granted, NSError * _Nullable error) {
@try {
NSLog(@"requestAuthorizationWithOptions: granted=%@", granted ? @"YES" : @"NO");
if (error == nil && granted) {
[self registerForRemoteNotifications];
}
[self handleBoolResultWithPotentialError:error command:command result:granted];
}@catch (NSException *exception) {
[self handlePluginExceptionWithContext:exception :command];
}

The notification center delegate is first set here during app startup:

[UNUserNotificationCenter currentNotificationCenter].delegate = self;

The fix included in this PR simply sets the notification center delegate back to the AppDelegate if permissions were granted successfully.

Does this PR introduce a breaking change?

  • Yes
  • No

What testing has been done on the changes in the PR?

This change has been tested in a production app

What testing has been done on existing functionality?

Other information

…grantPermission when permissions haven't been previously granted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants