From 895f22c32cfd6ccc9dad126e3fdceb5c50a94296 Mon Sep 17 00:00:00 2001 From: Martin Mazein Date: Sun, 3 Sep 2023 13:49:06 +0200 Subject: [PATCH 1/2] feat(push-notifications): Helper for opening native notification settings Add a helper for opening the native notification settings, e.g. for the case when user declined push permissions and need to be routed to the native settings to enable them again. Signed-off-by: Martin Mazein --- push-notifications/README.md | 13 ++++++++++ .../PushNotificationsPlugin.java | 25 +++++++++++++++++++ .../ios/Plugin/PushNotificationsPlugin.m | 1 + .../ios/Plugin/PushNotificationsPlugin.swift | 19 ++++++++++++++ push-notifications/src/definitions.ts | 7 ++++++ 5 files changed, 65 insertions(+) diff --git a/push-notifications/README.md b/push-notifications/README.md index 8ba95c04c..29ba3d1d3 100644 --- a/push-notifications/README.md +++ b/push-notifications/README.md @@ -173,6 +173,7 @@ const getDeliveredNotifications = async () => { * [`addListener('pushNotificationReceived', ...)`](#addlistenerpushnotificationreceived) * [`addListener('pushNotificationActionPerformed', ...)`](#addlistenerpushnotificationactionperformed) * [`removeAllListeners()`](#removealllisteners) +* [`openSettings()`](#opensettings) * [Interfaces](#interfaces) * [Type Aliases](#type-aliases) @@ -451,6 +452,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 b631971f6..4a8ef74a8 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,9 +11,12 @@ 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; @@ -327,4 +331,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/Plugin/PushNotificationsPlugin.m b/push-notifications/ios/Plugin/PushNotificationsPlugin.m index 8daf40e2f..d3f6df0d8 100644 --- a/push-notifications/ios/Plugin/PushNotificationsPlugin.m +++ b/push-notifications/ios/Plugin/PushNotificationsPlugin.m @@ -15,4 +15,5 @@ CAP_PLUGIN_METHOD(createChannel, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(listChannels, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(deleteChannel, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(openSettings, CAPPluginReturnPromise); ) diff --git a/push-notifications/ios/Plugin/PushNotificationsPlugin.swift b/push-notifications/ios/Plugin/PushNotificationsPlugin.swift index 5fb344b8f..6818ec330 100644 --- a/push-notifications/ios/Plugin/PushNotificationsPlugin.swift +++ b/push-notifications/ios/Plugin/PushNotificationsPlugin.swift @@ -194,4 +194,23 @@ public class PushNotificationsPlugin: CAPPlugin { "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 e744e5ea7..f7f476fdc 100644 --- a/push-notifications/src/definitions.ts +++ b/push-notifications/src/definitions.ts @@ -175,6 +175,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 { From 23713326ddde0c9cc2b7d223e9d6c9b6c7c95236 Mon Sep 17 00:00:00 2001 From: Martin Mazein Date: Tue, 16 Jan 2024 10:59:04 +0100 Subject: [PATCH 2/2] feat(push-notifications): Add plugin method after Swift refactoring Signed-off-by: Martin Mazein --- .../PushNotificationsPlugin/PushNotificationsPlugin.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/push-notifications/ios/Sources/PushNotificationsPlugin/PushNotificationsPlugin.swift b/push-notifications/ios/Sources/PushNotificationsPlugin/PushNotificationsPlugin.swift index f5fff48fb..ac1462c60 100644 --- a/push-notifications/ios/Sources/PushNotificationsPlugin/PushNotificationsPlugin.swift +++ b/push-notifications/ios/Sources/PushNotificationsPlugin/PushNotificationsPlugin.swift @@ -28,6 +28,7 @@ public class PushNotificationsPlugin: CAPPlugin, CAPBridgedPlugin { CAPPluginMethod(name: "createChannel", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "listChannels", returnType: CAPPluginReturnPromise), CAPPluginMethod(name: "deleteChannel", returnType: CAPPluginReturnPromise), + CAPPluginMethod(name: "openSettings", returnType: CAPPluginReturnPromise), ] private let notificationDelegateHandler = PushNotificationsHandler() private var appDelegateRegistrationCalled: Bool = false