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(android-foreground-service): add ability to define notification channel options #267

Open
wants to merge 2 commits into
base: main
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
5 changes: 5 additions & 0 deletions .changeset/hungry-hornets-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome-team/capacitor-android-foreground-service': minor
---

Added createNotificationChannel method, allowing you to specify notification channel options
48 changes: 48 additions & 0 deletions packages/android-foreground-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const stopForegroundService = async () => {
<docgen-index>

* [`moveToForeground()`](#movetoforeground)
* [`createNotificationChannel(...)`](#createnotificationchannel)
* [`startForegroundService(...)`](#startforegroundservice)
* [`stopForegroundService()`](#stopforegroundservice)
* [`checkPermissions()`](#checkpermissions)
Expand All @@ -79,6 +80,7 @@ const stopForegroundService = async () => {
* [`removeAllListeners()`](#removealllisteners)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)
* [Enums](#enums)

</docgen-index>

Expand All @@ -105,6 +107,27 @@ Only available on Android.
--------------------


### createNotificationChannel(...)

```typescript
createNotificationChannel(options: CreateNotificationChannelOptions) => Promise<void>
```

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`** | <code><a href="#createnotificationchanneloptions">CreateNotificationChannelOptions</a></code> |

**Since:** 6.1.0

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


### startForegroundService(...)

```typescript
Expand Down Expand Up @@ -249,6 +272,15 @@ Remove all listeners for this plugin.
### Interfaces


#### CreateNotificationChannelOptions

| Prop | Type | Description | Default | Since |
| ----------------- | ------------------------------------------------------------------------- | ------------------------ | ------------------------------------------- | ----- |
| **`name`** | <code>string</code> | The channel name. | <code>"Default"</code> | 6.1.0 |
| **`description`** | <code>string</code> | The channel description. | <code>"Default"</code> | 6.1.0 |
| **`importance`** | <code><a href="#notificationimportance">NotificationImportance</a></code> | The channel importance. | <code>NotificationImportance.DEFAULT</code> | 6.1.0 |


#### StartForegroundServiceOptions

| Prop | Type | Description | Since |
Expand Down Expand Up @@ -308,6 +340,22 @@ Remove all listeners for this plugin.

<code>(event: <a href="#buttonclickedevent">ButtonClickedEvent</a>): void</code>


### Enums


#### NotificationImportance

| Members | Value |
| ----------------- | ------------------ |
| **`DEFAULT`** | <code>3</code> |
| **`HIGH`** | <code>4</code> |
| **`LOW`** | <code>2</code> |
| **`MAX`** | <code>5</code> |
| **`MIN`** | <code>1</code> |
| **`NONE`** | <code>0</code> |
| **`UNSPECIFIED`** | <code>-1000</code> |

</docgen-api>

## Changelog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bundle> buttons) {
createNotificationChannel("Default", "Default", NotificationManager.IMPORTANCE_DEFAULT);
acquireWakeLock();
int iconResourceId = AssetUtil.getResourceID(plugin.getContext(), AssetUtil.getResourceBaseName(icon), "drawable");
Bundle notificationBundle = new Bundle();
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
46 changes: 46 additions & 0 deletions packages/android-foreground-service/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ export interface ForegroundServicePlugin {
* @experimental This method is experimental and may not work as expected.
*/
moveToForeground(): Promise<void>;
/**
* 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<void>;
/**
* Starts the foreground service.
*
Expand Down Expand Up @@ -86,6 +96,42 @@ export interface ForegroundServicePlugin {
removeAllListeners(): Promise<void>;
}

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.
Expand Down