From 246137ce23c9c602b889b397c6f93bf05cd946cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A8=E3=83=AA=E3=82=B9?= Date: Wed, 27 Nov 2024 00:36:21 +0900 Subject: [PATCH] fix(ios): request permission when new options added and requires --- src/ios/PushPlugin.m | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index dd3da425e..1081fcc7e 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -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; } @@ -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); } @@ -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]; + }); + } } }]; }