From 89299909a6fff7876fd42afb29765fce640a180e Mon Sep 17 00:00:00 2001 From: camillebeaumont Date: Thu, 11 Jan 2018 14:11:55 +0100 Subject: [PATCH 1/2] :penguin: Fix Android O channel confusing name/description --- docs/API.md | 8 +++++--- .../com/adobe/phonegap/push/PushConstants.java | 1 + src/android/com/adobe/phonegap/push/PushPlugin.java | 11 +++++++++-- src/js/push.js | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/docs/API.md b/docs/API.md index 0b1a9ea86..61bec73bb 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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. @@ -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. | diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index 9b4656a00..8dcda34ba 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -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"; diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index 690b37776..7e7276e1d 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -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); } @@ -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); @@ -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()); diff --git a/src/js/push.js b/src/js/push.js index f952cacbf..092f4a691 100644 --- a/src/js/push.js +++ b/src/js/push.js @@ -18,7 +18,7 @@ class PushNotification { this.handlers = { registration: [], notification: [], - error: [], + error: [] }; // require options parameter From 898e2f0f3658c6aa7dfcc9b11bc7d638f3f7ad6d Mon Sep 17 00:00:00 2001 From: Camille Beaumont Date: Fri, 2 Nov 2018 16:29:13 +0100 Subject: [PATCH 2/2] Update types definition --- types/index.d.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index 33a003f4e..5d755bc76 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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) * @@ -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 } }