Flutter-permission-handler: iOS notification permission request returns wrong results.

Created on 19 Dec 2019  路  5Comments  路  Source: Baseflow/flutter-permission-handler

馃悰 Bug Report

When you start a permission request for notifications on iOS there are two problems.

  1. The result is returned before the user has actually answered the system permission dialog.
  2. The resulting permission status for that permission group is always granted.

Expected behavior

The permission request should wait until the user has answered the system permission dialog and then give the correct permission granted / denied based on users decision.

Additional notes

I already had a look into the method requestPermission of the NotificationPermissionStrategy class and found that the completion block of the requestAuthorizationWithOptions call only handles the PermissionStatusDenied case. Instead of sending the PermissionStatusGranted from inside the result block it is sent at the end of the method. Which means it does not wait for the user to finish its input.

- (void)requestPermission:(PermissionGroup)permission completionHandler:(PermissionStatusHandler)completionHandler {
  PermissionStatus status = [self checkPermissionStatus:permission];
  if (status != PermissionStatusUnknown) {
    completionHandler(status);
    return;
  }
  dispatch_async(dispatch_get_main_queue(), ^{
    if(@available(iOS 10.0, *)) {
      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
      UNAuthorizationOptions authorizationOptions = 0;
      authorizationOptions += UNAuthorizationOptionSound;
      authorizationOptions += UNAuthorizationOptionAlert;
      authorizationOptions += UNAuthorizationOptionBadge;
      [center requestAuthorizationWithOptions:(authorizationOptions) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!granted || error != nil) {
          completionHandler(PermissionStatusDenied);
          return;
        }
      }];
    } else {
      UIUserNotificationType notificationTypes = 0;
      notificationTypes |= UIUserNotificationTypeSound;
      notificationTypes |= UIUserNotificationTypeAlert;
      notificationTypes |= UIUserNotificationTypeBadge;
      UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
      [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }

// I think this code belongs into the authorisation requests completion handler.
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    completionHandler(PermissionStatusGranted);
  });
}

Version: 4.0
Platform:

  • [X ] :iphone: iOS 12+
  • [ ] :robot: Android

Most helpful comment

Any new updates regarding this issue? This still happens to the latest version.

All 5 comments

I'm having the exact same issue

I'm having the exact same issue.

Any new updates regarding this issue? This still happens to the latest version.

I am testing 5.0.0+hotfix.3, it has the same issue. request returns wrong results on android(not sure in ios).
I this dependency last some update is not good for any project and also unstable.

I'm not able to reproduce this with Flutter 1.20.3 and permission_handler 5.0.1+1. If this, or a different, issue occurs, please submit a new issue.

Was this page helpful?
0 / 5 - 0 ratings