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

Added iOS implementation of switchToNotificationSettings function #517

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 8 additions & 0 deletions cordova.plugins.diagnostic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/ios/Diagnostic_Notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
- (void) isRegisteredForRemoteNotifications: (CDVInvokedUrlCommand*)command;
- (void) getRemoteNotificationsAuthorizationStatus: (CDVInvokedUrlCommand*)command;
- (void) requestRemoteNotificationsAuthorization: (CDVInvokedUrlCommand*)command;
- (void) switchToNotificationSettings: (CDVInvokedUrlCommand*)command;

@end
26 changes: 26 additions & 0 deletions src/ios/Diagnostic_Notifications.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions www/ios/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions www/ios/diagnostic.notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down