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

feat(push-notifications): Helper for opening native notification settings #1777

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
895f22c
feat(push-notifications): Helper for opening native notification sett…
MegaMaddin Sep 3, 2023
2edb750
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Sep 8, 2023
f5848ea
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Sep 13, 2023
6623a8f
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Sep 25, 2023
4ab8deb
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Oct 6, 2023
2eaf28f
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Jan 16, 2024
2371332
feat(push-notifications): Add plugin method after Swift refactoring
MegaMaddin Jan 16, 2024
89c33f2
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Jan 18, 2024
d1e49c7
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Jan 30, 2024
199f177
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Feb 22, 2024
952ee17
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Mar 1, 2024
eea040c
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Mar 7, 2024
a160cb7
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Apr 2, 2024
561e433
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin Apr 16, 2024
61828e7
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin May 1, 2024
ef32a67
Merge branch 'main' into plugins-push-notificatons-native-settings
MegaMaddin May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions push-notifications/README.md
Expand Up @@ -194,6 +194,7 @@ const getDeliveredNotifications = async () => {
* [`addListener('pushNotificationReceived', ...)`](#addlistenerpushnotificationreceived-)
* [`addListener('pushNotificationActionPerformed', ...)`](#addlistenerpushnotificationactionperformed-)
* [`removeAllListeners()`](#removealllisteners)
* [`openSettings()`](#opensettings)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)

Expand Down Expand Up @@ -472,6 +473,18 @@ Remove all native listeners for this plugin.

--------------------

### openSettings()

```typescript
openSettings() => Promise<{success: boolean}>
```

Open native notification settings.

**Since:** 5.0.0

--------------------


### Interfaces

Expand Down
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -333,4 +339,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);
}
}
Expand Up @@ -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
Expand Down Expand Up @@ -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])
})
}
}
}
7 changes: 7 additions & 0 deletions push-notifications/src/definitions.ts
Expand Up @@ -175,6 +175,13 @@ export interface PushNotificationsPlugin {
* @since 1.0.0
*/
removeAllListeners(): Promise<void>;

/**
* Open native notification settings.
*
* @since 5.0.0
*/
openSettings(): Promise<{ success: boolean }>;
}

export interface PushNotificationSchema {
Expand Down