From 4f3c755d70b14d6527f6e7156f60844d02de66c0 Mon Sep 17 00:00:00 2001 From: Alex Corn Date: Wed, 21 Aug 2024 01:03:52 -0400 Subject: [PATCH 1/2] feat(android-foreground-service): add ability to define notification channel options --- packages/android-foreground-service/README.md | 48 +++++++++++++++++++ .../foregroundservice/ForegroundService.java | 38 ++++++++------- .../ForegroundServicePlugin.java | 16 +++++++ .../src/definitions.ts | 46 ++++++++++++++++++ 4 files changed, 131 insertions(+), 17 deletions(-) diff --git a/packages/android-foreground-service/README.md b/packages/android-foreground-service/README.md index 2499eff..359647d 100644 --- a/packages/android-foreground-service/README.md +++ b/packages/android-foreground-service/README.md @@ -69,6 +69,7 @@ const stopForegroundService = async () => { * [`moveToForeground()`](#movetoforeground) +* [`createNotificationChannel(...)`](#createnotificationchannel) * [`startForegroundService(...)`](#startforegroundservice) * [`stopForegroundService()`](#stopforegroundservice) * [`checkPermissions()`](#checkpermissions) @@ -79,6 +80,7 @@ const stopForegroundService = async () => { * [`removeAllListeners()`](#removealllisteners) * [Interfaces](#interfaces) * [Type Aliases](#type-aliases) +* [Enums](#enums) @@ -105,6 +107,27 @@ Only available on Android. -------------------- +### createNotificationChannel(...) + +```typescript +createNotificationChannel(options: CreateNotificationChannelOptions) => Promise +``` + +Creates a notification channel. If you don't explicitly create a channel, +then the plugin will create a default channel with the name "Default" and +the description "Default". + +Only available on Android. + +| Param | Type | +| ------------- | --------------------------------------------------------------------------------------------- | +| **`options`** | CreateNotificationChannelOptions | + +**Since:** 6.1.0 + +-------------------- + + ### startForegroundService(...) ```typescript @@ -249,6 +272,15 @@ Remove all listeners for this plugin. ### Interfaces +#### CreateNotificationChannelOptions + +| Prop | Type | Description | Default | Since | +| ----------------- | ------------------------------------------------------------------------- | ------------------------ | ------------------------------------------- | ----- | +| **`name`** | string | The channel name. | "Default" | 6.1.0 | +| **`description`** | string | The channel description. | "Default" | 6.1.0 | +| **`importance`** | NotificationImportance | The channel importance. | NotificationImportance.DEFAULT | 6.1.0 | + + #### StartForegroundServiceOptions | Prop | Type | Description | Since | @@ -308,6 +340,22 @@ Remove all listeners for this plugin. (event: ButtonClickedEvent): void + +### Enums + + +#### NotificationImportance + +| Members | Value | +| ----------------- | ------------------ | +| **`DEFAULT`** | 3 | +| **`HIGH`** | 4 | +| **`LOW`** | 2 | +| **`MAX`** | 5 | +| **`MIN`** | 1 | +| **`NONE`** | 0 | +| **`UNSPECIFIED`** | -1000 | + ## Changelog diff --git a/packages/android-foreground-service/android/src/main/java/io/capawesome/capacitorjs/plugins/foregroundservice/ForegroundService.java b/packages/android-foreground-service/android/src/main/java/io/capawesome/capacitorjs/plugins/foregroundservice/ForegroundService.java index 67d9f6d..82f43e7 100644 --- a/packages/android-foreground-service/android/src/main/java/io/capawesome/capacitorjs/plugins/foregroundservice/ForegroundService.java +++ b/packages/android-foreground-service/android/src/main/java/io/capawesome/capacitorjs/plugins/foregroundservice/ForegroundService.java @@ -21,13 +21,33 @@ public class ForegroundService { @Nullable private PowerManager.WakeLock activeWakeLock; + private boolean notificationChannelCreated = false; public ForegroundService(ForegroundServicePlugin plugin) { this.plugin = plugin; - createNotificationChannel(); + } + + public void createNotificationChannel(String name, String description, int importance) { + if (notificationChannelCreated) { + return; + } + + // Create the NotificationChannel, but only on API 26+ because + // the NotificationChannel class is new and not in the support library + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(DEFAULT_NOTIFICATION_CHANNEL_ID, name, importance); + channel.setDescription(description); + // Register the channel with the system; you can't change the importance + // or other notification behaviors after this + NotificationManager notificationManager = plugin.getContext().getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + + notificationChannelCreated = true; } public void startForegroundService(String body, String icon, int id, String title, ArrayList buttons) { + createNotificationChannel("Default", "Default", NotificationManager.IMPORTANCE_DEFAULT); acquireWakeLock(); int iconResourceId = AssetUtil.getResourceID(plugin.getContext(), AssetUtil.getResourceBaseName(icon), "drawable"); Bundle notificationBundle = new Bundle(); @@ -75,20 +95,4 @@ private void releaseWakeLock() { activeWakeLock.release(); activeWakeLock = null; } - - private void createNotificationChannel() { - // Create the NotificationChannel, but only on API 26+ because - // the NotificationChannel class is new and not in the support library - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - CharSequence name = "Default"; - String description = "Default"; - int importance = NotificationManager.IMPORTANCE_DEFAULT; - NotificationChannel channel = new NotificationChannel(DEFAULT_NOTIFICATION_CHANNEL_ID, name, importance); - channel.setDescription(description); - // Register the channel with the system; you can't change the importance - // or other notification behaviors after this - NotificationManager notificationManager = plugin.getContext().getSystemService(NotificationManager.class); - notificationManager.createNotificationChannel(channel); - } - } } diff --git a/packages/android-foreground-service/android/src/main/java/io/capawesome/capacitorjs/plugins/foregroundservice/ForegroundServicePlugin.java b/packages/android-foreground-service/android/src/main/java/io/capawesome/capacitorjs/plugins/foregroundservice/ForegroundServicePlugin.java index 75aab7f..cb10a02 100644 --- a/packages/android-foreground-service/android/src/main/java/io/capawesome/capacitorjs/plugins/foregroundservice/ForegroundServicePlugin.java +++ b/packages/android-foreground-service/android/src/main/java/io/capawesome/capacitorjs/plugins/foregroundservice/ForegroundServicePlugin.java @@ -1,5 +1,6 @@ package io.capawesome.capacitorjs.plugins.foregroundservice; +import android.app.NotificationManager; import android.Manifest; import android.content.Intent; import android.net.Uri; @@ -61,6 +62,21 @@ public void moveToForeground(PluginCall call) { } } + @PluginMethod + public void createNotificationChannel(PluginCall call) { + try { + implementation.createNotificationChannel( + call.getString("name", "Default"), + call.getString("description", "Default"), + call.getInt("importance", NotificationManager.IMPORTANCE_DEFAULT) + ); + call.resolve(); + } catch (Exception exception) { + call.reject(exception.getMessage()); + Logger.error(ForegroundServicePlugin.TAG, exception.getMessage(), exception); + } + } + @PluginMethod public void startForegroundService(PluginCall call) { try { diff --git a/packages/android-foreground-service/src/definitions.ts b/packages/android-foreground-service/src/definitions.ts index d49cb18..6b1ad92 100644 --- a/packages/android-foreground-service/src/definitions.ts +++ b/packages/android-foreground-service/src/definitions.ts @@ -15,6 +15,16 @@ export interface ForegroundServicePlugin { * @experimental This method is experimental and may not work as expected. */ moveToForeground(): Promise; + /** + * Creates a notification channel. If you don't explicitly create a channel, + * then the plugin will create a default channel with the name "Default" and + * the description "Default". + * + * Only available on Android. + * + * @since 6.1.0 + */ + createNotificationChannel(options: CreateNotificationChannelOptions): Promise; /** * Starts the foreground service. * @@ -86,6 +96,42 @@ export interface ForegroundServicePlugin { removeAllListeners(): Promise; } +export enum NotificationImportance { + DEFAULT = 3, + HIGH = 4, + LOW = 2, + MAX = 5, + MIN = 1, + NONE = 0, + UNSPECIFIED = -1000, +} + +export interface CreateNotificationChannelOptions { + /** + * The channel name. + * + * @since 6.1.0 + * @default "Default" + */ + name?: string; + + /** + * The channel description. + * + * @since 6.1.0 + * @default "Default" + */ + description?: string; + + /** + * The channel importance. + * + * @since 6.1.0 + * @default NotificationImportance.DEFAULT + */ + importance?: NotificationImportance; +} + export interface StartForegroundServiceOptions { /** * The body of the notification, shown below the title. From 72f321687e5ea6f8914d4981dc7c3dd5b419ece8 Mon Sep 17 00:00:00 2001 From: Alex Corn Date: Wed, 21 Aug 2024 01:13:19 -0400 Subject: [PATCH 2/2] chore: create changeset --- .changeset/hungry-hornets-impress.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hungry-hornets-impress.md diff --git a/.changeset/hungry-hornets-impress.md b/.changeset/hungry-hornets-impress.md new file mode 100644 index 0000000..bc650a6 --- /dev/null +++ b/.changeset/hungry-hornets-impress.md @@ -0,0 +1,5 @@ +--- +'@capawesome-team/capacitor-android-foreground-service': minor +--- + +Added createNotificationChannel method, allowing you to specify notification channel options