Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

🐧 Fix Android O channel confusing name/description #2142

Open
wants to merge 2 commits into
base: master
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
8 changes: 5 additions & 3 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,15 @@ PushNotification.createChannel(
},
{
id: 'testchannel1',
description: 'My first test channel',
name: 'First channel',
description: 'This is my very first notification channel',
importance: 3,
vibration: true
}
);
```

The above will create a channel for your app. You'll need to provide the `id`, `description` and `importance` properties.
The above will create a channel for your app. You'll need to provide the `id`, `name` and `importance` properties, `description` is optional. The importance property goes from 1 = Lowest, 2 = Low, 3 = Normal, 4 = High and 5 = Highest.

A default channel with the id "PushPluginChannel" is created automatically. To make changes to the default channel's settings, create a channel with the id "PushPluginChannel" before calling the PushNotification.init function.

Expand All @@ -219,7 +220,8 @@ A default channel with the id "PushPluginChannel" is created automatically. To m
| Property | Type | Description |
| -------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `String` | The id of the channel. Must be unique per package. The value may be truncated if it is too long. |
| `description` | `String` | The user visible name of the channel. The recommended maximum length is 40 characters; the value may be truncated if it is too long. |
| `name` | `String` | The user visible name of the channel. The recommended maximum length is 40 characters; the value may be truncated if it is too long. |
| `description` | `String` | The user visible description of the channel. The recommended maximum length is 300 characters; the value may be truncated if it is too long. |
| `importance` | `Int` | The importance of the channel. This controls how interruptive notifications posted to this channel are. The importance property goes from 1 = Lowest, 2 = Low, 3 = Normal, 4 = High and 5 = Highest. |
| `sound` | `String` | The name of the sound file to be played upon receipt of the notification in this channel. Cannot be changed after channel is created. |
| `vibration` | `Boolean` or `Array` | Boolean sets whether notification posted to this channel should vibrate. Array sets custom vibration pattern. Example - vibration: `[2000, 1000, 500, 500]`. Cannot be changed after channel is created. |
Expand Down
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public interface PushConstants {
public static final String DEFAULT_CHANNEL_ID = "PushPluginChannel";
public static final String CHANNELS = "channels";
public static final String CHANNEL_ID = "id";
public static final String CHANNEL_NAME = "name";
public static final String CHANNEL_DESCRIPTION = "description";
public static final String CHANNEL_IMPORTANCE = "importance";
public static final String CHANNEL_LIGHT_COLOR = "lightColor";
Expand Down
11 changes: 9 additions & 2 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private JSONArray listChannels() throws JSONException {
for (NotificationChannel notificationChannel : notificationChannels) {
JSONObject channel = new JSONObject();
channel.put(CHANNEL_ID, notificationChannel.getId());
channel.put(CHANNEL_NAME, notificationChannel.getName());
channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription());
channels.put(channel);
}
Expand All @@ -94,9 +95,14 @@ private void createChannel(JSONObject channel) throws JSONException {

String packageName = getApplicationContext().getPackageName();
NotificationChannel mChannel = new NotificationChannel(channel.getString(CHANNEL_ID),
channel.optString(CHANNEL_DESCRIPTION, ""),
channel.optString(CHANNEL_NAME, ""),
channel.optInt(CHANNEL_IMPORTANCE, NotificationManager.IMPORTANCE_DEFAULT));

String desc = channel.optString(CHANNEL_DESCRIPTION, "");
if(desc != null && !desc.isEmpty()) {
mChannel.setDescription(desc);
}

int lightColor = channel.optInt(CHANNEL_LIGHT_COLOR, -1);
if (lightColor != -1) {
mChannel.setLightColor(lightColor);
Expand Down Expand Up @@ -157,7 +163,8 @@ private void createDefaultNotificationChannelIfNeeded(JSONObject options) {
}
try {
options.put(CHANNEL_ID, DEFAULT_CHANNEL_ID);
options.putOpt(CHANNEL_DESCRIPTION, "PhoneGap PushPlugin");
options.putOpt(CHANNEL_NAME, "PhoneGap PushPlugin");
options.putOpt(CHANNEL_DESCRIPTION, "Default channel");
createChannel(options);
} catch (JSONException e) {
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
Expand Down
2 changes: 1 addition & 1 deletion src/js/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PushNotification {
this.handlers = {
registration: [],
notification: [],
error: [],
error: []
};

// require options parameter
Expand Down
68 changes: 68 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,49 @@ declare namespace PhonegapPluginPush {
additionalData: NotificationEventAdditionalData
}

/**
* Android only
* Notification channel
*/
interface Channel {
/**
* The id of the channel. Must be unique per package.
* The value may be truncated if it is too long.
*/
id: string;
/**
* The user visible name of the channel.
* The recommended maximum length is 40 characters; the value may be truncated if it is too long.
*/
name: string;
/**
* The user visible description of the channel.
* The recommended maximum length is 300 characters; the value may be truncated if it is too long.
*/
description?: string;
/**
* The importance of the channel. This controls how interruptive notifications posted to this channel are.
* The importance property goes from 1 = Lowest, 2 = Low, 3 = Normal, 4 = High and 5 = Highest.
*/
importance: number;
/**
* The name of the sound file to be played upon receipt of the notification in this channel.
* Cannot be changed after channel is created.
*/
sound?: string;
/**
* Boolean sets whether notification posted to this channel should vibrate.
* Array sets custom vibration pattern. Example - vibration: [2000, 1000, 500, 500].
* Cannot be changed after channel is created.
*/
vibration?: boolean | number[];
/**
* Sets whether notifications posted to this channel appear on the lockscreen or not, and if so, whether they appear in a redacted form.
* 0 = Private, 1 = Public, -1 = Secret.
*/
visibility?: number;
}

/**
* TODO: document all possible properties (I only got the android ones)
*
Expand Down Expand Up @@ -293,6 +336,31 @@ declare namespace PhonegapPluginPush {
interface PushNotificationStatic {
init(options: InitOptions): PushNotification
new (options: InitOptions): PushNotification

/**
* Android only
* Create a new notification channel for Android O and above.
* @param successHandler Is called when the api successfully creates a channel.
* @param errorHandler Is called when the api fails to create a channel.
* @param channel The options for the channel.
*/
createChannel(successHandler: () => any, errorHandler: () => any, channel: Channel): void

/**
* Android only
* Delete a notification channel for Android O and above.
* @param successHandler Is called when the api successfully delete a channel.
* @param errorHandler Is called when the api fails to delete a channel.
* @param channelId The ID of the channel.
*/
deleteChannel(successHandler: () => any, errorHandler: () => any, channelId: string): void

/**
* Android only
* Returns a list of currently configured channels.
* @param successHandler Is called when the api successfully retrieves the list of channels.
*/
listChannels(successHandler: (channels: Channel[]) => any): void
}
}

Expand Down