Skip to content

Commit

Permalink
fix(ios): request permission when new options added and requires
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Dec 7, 2024
1 parent f858ab6 commit 246137c
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -735,9 +735,9 @@ - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authoriza

__weak UNUserNotificationCenter *weakCenter = center;
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
// Get the current notification permission settings
UNAuthorizationOptions currentGrantedOptions = UNAuthorizationOptionNone;

// Check for current granted permissions
if (settings.badgeSetting == UNNotificationSettingEnabled) {
currentGrantedOptions |= UNAuthorizationOptionBadge;
}
Expand All @@ -753,12 +753,12 @@ - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authoriza
}
}

// Compare the permissions between requested and current. Figure out which permissions are missing.
// Compare the requested with granded permissions. Find which are missing.
UNAuthorizationOptions newAuthorizationOptions = authorizationOptions & ~currentGrantedOptions;

// Request missing new permissions
if (newAuthorizationOptions != UNAuthorizationOptionNone) {
[weakCenter requestAuthorizationWithOptions:newAuthorizationOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
// If the status is not determined, request permissions
[weakCenter requestAuthorizationWithOptions:authorizationOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"[PushPlugin] Error during authorization request: %@", error.localizedDescription);
}
Expand All @@ -773,10 +773,28 @@ - (void)handleNotificationSettingsWithAuthorizationOptions:(NSNumber *)authoriza
}
}];
} else {
NSLog(@"[PushPlugin] All requested permissions were processed.");
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
// If permissions are already granted, request any missing ones
if (newAuthorizationOptions != UNAuthorizationOptionNone) {
[weakCenter requestAuthorizationWithOptions:newAuthorizationOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"[PushPlugin] Error during authorization request: %@", error.localizedDescription);
}

if (granted) {
NSLog(@"[PushPlugin] New notification permissions granted.");
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
} else {
NSLog(@"[PushPlugin] User denied new notification permissions.");
}
}];
} else {
NSLog(@"[PushPlugin] All requested permissions were processed.");
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}
}];
}
Expand Down

0 comments on commit 246137c

Please sign in to comment.