diff --git a/push-notifications/README.md b/push-notifications/README.md index 41c24c334..b79af926b 100644 --- a/push-notifications/README.md +++ b/push-notifications/README.md @@ -202,6 +202,7 @@ const getDeliveredNotifications = async () => { * [`addListener('pushNotificationReceived', ...)`](#addlistenerpushnotificationreceived-) * [`addListener('pushNotificationActionPerformed', ...)`](#addlistenerpushnotificationactionperformed-) * [`removeAllListeners()`](#removealllisteners) +* [`openSettings()`](#opensettings) * [Interfaces](#interfaces) * [Type Aliases](#type-aliases) @@ -480,6 +481,18 @@ Remove all native listeners for this plugin. -------------------- +### openSettings() + +```typescript +openSettings() => Promise<{success: boolean}> +``` + +Open native notification settings. + +**Since:** 5.0.0 + +-------------------- + ### Interfaces diff --git a/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java b/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java index f338f4995..a67a7c87d 100644 --- a/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java +++ b/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java @@ -1,6 +1,7 @@ package com.capacitorjs.plugins.pushnotifications; import android.Manifest; +import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; @@ -10,8 +11,13 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.provider.Settings; import android.service.notification.StatusBarNotification; +import androidx.activity.result.ActivityResult; +import androidx.core.app.NotificationCompat; + import com.getcapacitor.*; +import com.getcapacitor.annotation.ActivityCallback; import com.getcapacitor.annotation.CapacitorPlugin; import com.getcapacitor.annotation.Permission; import com.getcapacitor.annotation.PermissionCallback; @@ -322,4 +328,25 @@ private Bundle getBundleLegacy() { return null; } } + + @PluginMethod + public void openSettings(PluginCall call) { + Intent intent = new Intent(); + intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS); + intent.putExtra(Settings.EXTRA_APP_PACKAGE, getContext().getPackageName()); + startActivityForResult(call, intent, "activityCallback"); + } + + @ActivityCallback + private void activityCallback(PluginCall call, ActivityResult result) { + JSObject rvalue = new JSObject(); + + if (result.getResultCode() == Activity.RESULT_OK) { + rvalue.put("success", true); + } else { + rvalue.put("success", false); + } + + call.resolve(rvalue); + } } diff --git a/push-notifications/ios/Sources/PushNotificationsPlugin/PushNotificationsPlugin.swift b/push-notifications/ios/Sources/PushNotificationsPlugin/PushNotificationsPlugin.swift index 9044c7493..4d10cb69d 100644 --- a/push-notifications/ios/Sources/PushNotificationsPlugin/PushNotificationsPlugin.swift +++ b/push-notifications/ios/Sources/PushNotificationsPlugin/PushNotificationsPlugin.swift @@ -27,7 +27,8 @@ public class PushNotificationsPlugin: CAPPlugin, CAPBridgedPlugin { CAPPluginMethod(name: "removeDeliveredNotifications", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "createChannel", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "listChannels", returnType: CAPPluginReturnPromise), - CAPPluginMethod(name: "deleteChannel", returnType: CAPPluginReturnPromise) + CAPPluginMethod(name: "deleteChannel", returnType: CAPPluginReturnPromise), + CAPPluginMethod(name: "openSettings", returnType: CAPPluginReturnPromise), ] private let notificationDelegateHandler = PushNotificationsHandler() private var appDelegateRegistrationCalled: Bool = false @@ -208,4 +209,23 @@ public class PushNotificationsPlugin: CAPPlugin, CAPBridgedPlugin { "error": error.localizedDescription ]) } + + @objc func openSettings(_ call: CAPPluginCall) { + var urlString = UIApplication.openSettingsURLString + + if #available(iOS 16.0, *) { + urlString = UIApplication.openNotificationSettingsURLString + } + + guard let url = URL(string: urlString) else { + call.reject("Can't open settings") + return + } + + DispatchQueue.main.async { + UIApplication.shared.open(url, completionHandler: { success in + call.resolve(["success": success]) + }) + } + } } diff --git a/push-notifications/src/definitions.ts b/push-notifications/src/definitions.ts index 7cd11b460..747fe709a 100644 --- a/push-notifications/src/definitions.ts +++ b/push-notifications/src/definitions.ts @@ -170,6 +170,13 @@ export interface PushNotificationsPlugin { * @since 1.0.0 */ removeAllListeners(): Promise; + + /** + * Open native notification settings. + * + * @since 5.0.0 + */ + openSettings(): Promise<{ success: boolean }>; } export interface PushNotificationSchema {