diff --git a/README.md b/README.md index fd0a5be1..4eb6c508 100644 --- a/README.md +++ b/README.md @@ -2689,11 +2689,11 @@ Requests remote notifications authorization for the application. ### switchToNotificationSettings() -Platforms: Android +Platforms: Android & iOS Open notification settings for your app -On Android versions lower than O, this will open the same page as `switchToSettings()`. +On Android versions lower than O and on iOS versions lower than 15.4, this will open the same page as `switchToSettings()`. cordova.plugins.diagnostic.switchToNotificationSettings(); diff --git a/cordova.plugins.diagnostic.d.ts b/cordova.plugins.diagnostic.d.ts index 05684f82..5e4a2a4a 100644 --- a/cordova.plugins.diagnostic.d.ts +++ b/cordova.plugins.diagnostic.d.ts @@ -1015,6 +1015,14 @@ interface Diagnostic { errorCallback: (error: string) => void ) => void; + /** + * Opens the notification settings page for this app. + */ + switchToNotificationSettings?: ( + successCallback: () => void, + errorCallback: (error: string) => void + ) => void; + /** * ANDROID ONLY * Checks if ADB mode(debug mode) is enabled. diff --git a/src/ios/Diagnostic_Notifications.h b/src/ios/Diagnostic_Notifications.h index 54baa975..55bb1952 100644 --- a/src/ios/Diagnostic_Notifications.h +++ b/src/ios/Diagnostic_Notifications.h @@ -18,5 +18,6 @@ - (void) isRegisteredForRemoteNotifications: (CDVInvokedUrlCommand*)command; - (void) getRemoteNotificationsAuthorizationStatus: (CDVInvokedUrlCommand*)command; - (void) requestRemoteNotificationsAuthorization: (CDVInvokedUrlCommand*)command; +- (void) switchToNotificationSettings: (CDVInvokedUrlCommand*)command; @end diff --git a/src/ios/Diagnostic_Notifications.m b/src/ios/Diagnostic_Notifications.m index b8511777..71960118 100644 --- a/src/ios/Diagnostic_Notifications.m +++ b/src/ios/Diagnostic_Notifications.m @@ -223,6 +223,32 @@ - (void) requestRemoteNotificationsAuthorization: (CDVInvokedUrlCommand*)command }]; } +- (void) switchToNotificationSettings: (CDVInvokedUrlCommand*)command +{ + @try { + if (@available(iOS 15.4, *)) { + [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenNotificationSettingsURLString] options:@{} completionHandler:^(BOOL success) { + if (success) { + [diagnostic sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] :command]; + }else{ + [diagnostic sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR] :command]; + } + }]; + } else { + [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) { + if (success) { + [diagnostic sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] :command]; + }else{ + [diagnostic sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR] :command]; + } + }]; + } + } + @catch (NSException *exception) { + [diagnostic handlePluginException:exception :command]; + } +} + - (void) _isRegisteredForRemoteNotifications:(void (^)(BOOL result))completeBlock { dispatch_async(dispatch_get_main_queue(), ^{ BOOL registered = [UIApplication sharedApplication].isRegisteredForRemoteNotifications; diff --git a/www/ios/diagnostic.js b/www/ios/diagnostic.js index 235612b6..2e8be4a6 100644 --- a/www/ios/diagnostic.js +++ b/www/ios/diagnostic.js @@ -695,6 +695,21 @@ var Diagnostic = (function(){ } }; + /** + * Switches to the notification settings page in the Settings app + * + * @param {Function} successCallback - The callback which will be called when switch to settings is successful. + * @param {Function} errorCallback - The callback which will be called when switch to settings encounters an error. + * This callback function is passed a single string parameter containing the error message. + */ + Diagnostic.switchToNotificationSettings = function(successCallback, errorCallback) { + if (cordova.plugins.diagnostic.notifications){ + cordova.plugins.diagnostic.notifications.switchToNotificationSettings.apply(this, arguments); + } else { + throw "Diagnostic Notification module is not installed"; + } + }; + /** * Indicates the current setting of notification types for the app in the Settings app. * Note: if "Allow Notifications" switch is OFF, all types will be returned as disabled. diff --git a/www/ios/diagnostic.notifications.js b/www/ios/diagnostic.notifications.js index 3740e343..b59ac653 100644 --- a/www/ios/diagnostic.notifications.js +++ b/www/ios/diagnostic.notifications.js @@ -183,6 +183,17 @@ var Diagnostic_Notifications = (function(){ [params.types, params.omitRegistration ? 1 : 0]); }; + /** + * Switches to the notifications page in the Settings app + */ + Diagnostic_Notifications.switchToNotificationSettings = function(successCallback, errorCallback) { + return cordova.exec(successCallback, + errorCallback, + 'Diagnostic_Notifications', + 'switchToNotificationSettings', + []); + }; + return Diagnostic_Notifications; });