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

Added Channel Group #2607

Open
wants to merge 25 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
108 changes: 106 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- [.createChannel() - Android only](#pushnotificationcreatechannel)
- [.deleteChannel() - Android only](#pushnotificationdeletechannel)
- [.listChannels() - Android only](#pushnotificationlistchannels)
- [.createChannelGroup() - Android only](#pushnotificationcreatechannelgroup)
- [.deleteChannelGroup() - Android only](#pushnotificationdeletechannelgroup)
- [.listChannelGroups() - Android only](#pushnotificationlistchannelgroups)
- [push.on()](#pushonevent-callback)
- [push.on('registration')](#pushonregistration-callback)
- [push.on('notification')](#pushonnotification-callback)
Expand Down Expand Up @@ -224,6 +227,7 @@ A default channel with the id "PushPluginChannel" is created automatically. To m
| `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. |
| `visibility` | `Int` | 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. |
| `group_id` | `String` | Sets channel group id. Assign it to a Channel Group Created. Cannot be changed once set. |

## PushNotification.deleteChannel(successHandler, failureHandler, channelId)

Expand Down Expand Up @@ -274,11 +278,111 @@ Returns a list of currently configured channels.
```javascript
PushNotification.listChannels(channels => {
for (let channel of channels) {
console.log(`ID: ${channel.id} Description: ${channel.description}`);
console.log(`ID: ${channel.id} Description: ${channel.description} GroupID: ${channel.group_id}`);
}
});
```

## PushNotification.createChannelGroup(successHandler, failureHandler, channelGroup)

Create a new notification channel group for Android O and above.

### Parameters

| Parameter | Type | Default | Description |
| ---------------- | ---------- | ------- | ------------------------------------------------------ |
| `successHandler` | `Function` | | Is called when the api successfully creates a channel. |
| `failureHandler` | `Function` | | Is called when the api fails to create a channel. |
| `channelGroup` | `Object` | | The options for the channel group. |

### Example

```javascript
PushNotification.createChannelGroup(
() => {
console.log('success');
},
() => {
console.log('error');
},
{
id: 'testchannelgroup',
name: 'My first test channel group'
}
);
```

The above will create a channel group for your app. You'll need to provide the `id` and `name` properties.



### Channel Group properties

| Property | Type | Description |
| -------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `String` | The id of the channel group. |
| `name` | `String` | The user visible name of the channel group. |

## PushNotification.deleteChannelGroup(successHandler, failureHandler, channelGroupId)

Delete a notification channel for Android O and above.

### Parameters

| Parameter | Type | Default | Description |
| ---------------- | ---------- | ------- | ------------------------------------------------------------ |
| `successHandler` | `Function` | | Is called when the api successfully creates a channel group. |
| `failureHandler` | `Function` | | Is called when the api fails to create a channel group. |
| `channelGroupId` | `String` | | The ID of the channel group. |

### Example

```javascript
PushNotification.deleteChannelGroup(
() => {
console.log('success');
},
() => {
console.log('error');
},
'testchannelgroup'
);
```

## PushNotification.listChannelGroups(successHandler)

Returns a list of currently configured channel groups.

### Parameters

| Parameter | Type | Default | Description |
| ---------------- | ---------- | ------- | ------------------------------------------------------------------- |
| `successHandler` | `Function` | | Is called when the api successfully retrieves the list of channels. |

### Callback parameters

#### `successHandler`

| Parameter | Type | Description |
| ---------- | ------------ | ------------------------------ |
| `channelGroups` | `JSONArrary` | List of channel group objects. |

### Example

```javascript
PushNotification.listChannelGroups(channelGroups => {
for (let channelGroup of channelGroups) {
console.log(`ID: ${channelGroup.id} Description: ${channelGroup.name}`);
}
});
```







## push.on(event, callback)

### Parameters
Expand Down Expand Up @@ -612,4 +716,4 @@ push.clearNotification(() => {
}, () => {
console.log('error');
}, 145);
```
```
6 changes: 6 additions & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ public interface PushConstants {
public static final String ONGOING = "ongoing";
public static final String LIST_CHANNELS = "listChannels";
public static final String CLEAR_NOTIFICATION = "clearNotification";
public static final String CREATE_CHANNEL_GROUP = "createChannelGroup";
public static final String LIST_CHANNEL_GROUPS = "listChannelGroups";
public static final String DELETE_CHANNEL_GROUP = "deleteChannelGroup";
public static final String GROUP_ID = "id";
public static final String GROUP_NAME= "name";
public static final String CHANNEL_GROUP_ID = "group_id";
}
87 changes: 85 additions & 2 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private JSONArray listChannels() throws JSONException {
JSONObject channel = new JSONObject();
channel.put(CHANNEL_ID, notificationChannel.getId());
channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription());
channel.put(CHANNEL_GROUP_ID, notificationChannel.getGroup());
channels.put(channel);
}
}
Expand Down Expand Up @@ -96,7 +97,12 @@ private void createChannel(JSONObject channel) throws JSONException {
NotificationChannel mChannel = new NotificationChannel(channel.getString(CHANNEL_ID),
channel.optString(CHANNEL_DESCRIPTION, ""),
channel.optInt(CHANNEL_IMPORTANCE, NotificationManager.IMPORTANCE_DEFAULT));


String groupId=channel.optString(CHANNEL_GROUP_ID, "");
if(groupId != null && !groupId.isEmpty() ) {
mChannel.setGroup(groupId);
}

int lightColor = channel.optInt(CHANNEL_LIGHT_COLOR, -1);
if (lightColor != -1) {
mChannel.setLightColor(lightColor);
Expand Down Expand Up @@ -164,6 +170,45 @@ private void createDefaultNotificationChannelIfNeeded(JSONObject options) {
}
}
}

@TargetApi(26)
private void createChannelGroup(JSONObject group) throws JSONException {
// only call on Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannelGroup mGroup = new NotificationChannelGroup(group.optString(GROUP_ID, ""), group.optString(GROUP_NAME, ""));
notificationManager.createNotificationChannelGroup(mGroup);
}
}

@TargetApi(26)
private JSONArray listChannelGroups() throws JSONException {
JSONArray channelGroups = new JSONArray();
// only call on Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
List<NotificationChannelGroup> notificationChannelGroups = notificationManager.getNotificationChannelGroups();
for (NotificationChannelGroup notificationChannelGroup : notificationChannelGroups) {
JSONObject channelGroup = new JSONObject();
channelGroup.put(GROUP_ID, notificationChannelGroup.getId());
channelGroup.put(GROUP_NAME, notificationChannelGroup.getName());
channelGroups.put(channelGroup);
}
}
return channelGroups;
}

@TargetApi(26)
private void deleteChannelGroup(String channelGroupId) {
// only call on Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannelGroup(channelGroupId);
}
}

@Override
public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) {
Expand Down Expand Up @@ -431,7 +476,45 @@ public void run() {
}
}
});
} else {
} else if (CREATE_CHANNEL_GROUP.equals(action)) {
// un-subscribing for a topic
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
// call create channel
createChannelGroup(data.getJSONObject(0));
callbackContext.success();
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}
});
} else if (LIST_CHANNEL_GROUPS.equals(action)) {
// un-subscribing for a topic
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
callbackContext.success(listChannelGroups());
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}
});
} else if (DELETE_CHANNEL_GROUP.equals(action)) {
// un-subscribing for a topic
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
String channelGroupId = data.getString(0);
deleteChannelGroup(channelGroupId);
callbackContext.success();
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}
});
}
else {
Log.e(LOG_TAG, "Invalid action : " + action);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
return false;
Expand Down
12 changes: 12 additions & 0 deletions src/js/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ module.exports = {
listChannels: (successCallback, errorCallback) => {
exec(successCallback, errorCallback, 'PushNotification', 'listChannels', []);
},
createChannelGroup: (successCallback, errorCallback, channelGroup) => {
exec(successCallback, errorCallback, 'PushNotification', 'createChannelGroup', [channelGroup]);
},

deleteChannelGroup: (successCallback, errorCallback, channelGroupId) => {
exec(successCallback, errorCallback, 'PushNotification', 'deleteChannelGroup', [channelGroupId]);
},

listChannelGroups: (successCallback, errorCallback) => {
exec(successCallback, errorCallback, 'PushNotification', 'listChannelGroups', []);
},


/**
* PushNotification Object.
Expand Down
57 changes: 56 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,65 @@ declare namespace PhonegapPluginPush {
from?: string
notId?: string
}

/**
* Set Channel Variable,once created cannot be changed programmtically for importance and vibration
*/
interface Channel {
/**
* The id of the channel. Must be unique per package. The value may be truncated if it is too long.
*/
id?: string
/**
* camillebeaumont name and description fix
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these notes to fix later? We don't have the name attribute yet, so we don't have an official doc to copy from, so it would make sense.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup based on a previous pull request

*/
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 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
/**
OliverOng1995 marked this conversation as resolved.
Show resolved Hide resolved
* 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
/**
* 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
/**
* Sets channel group id. Assign it to a Channel Group Created. Cannot be changed once set.
*/
groupId:string
OliverOng1995 marked this conversation as resolved.
Show resolved Hide resolved
}

interface ChannelGroup {
/**
* The id of the channel group.
*/
id?: string
/**
* The user visible name of the channel group.
*/
name?: string
}

interface PushNotificationStatic {
init(options: InitOptions): PushNotification
new (options: InitOptions): PushNotification
hasPermission(successHandler: () => any): void
createChannel(successHandler: () => any, errorHandler: () => any,channel): void
listChannels(successHandler: () => any): void
deleteChannel(successHandler: () => any, errorHandler: () => any, channelId: string ): void
createChannelGroup(successHandler: () => any, errorHandler: () => any, channelGroup: ChannelGroup): void
listChannelGroups(successHandler: () => any): void
deleteChannelGroup(successHandler: () => any, errorHandler: () => any, channelGroupId: string ): void
}
}

Expand Down
15 changes: 14 additions & 1 deletion www/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,19 @@ module.exports = {
listChannels: function listChannels(successCallback, errorCallback) {
exec(successCallback, errorCallback, 'PushNotification', 'listChannels', []);
},

createChannelGroup: function createChannel(successCallback, errorCallback, channelGroup) {
exec(successCallback, errorCallback, 'PushNotification', 'createChannelGroup', [channelGroup]);
},

deleteChannelGroup: function deleteChannel(successCallback, errorCallback, channelGroupId) {
exec(successCallback, errorCallback, 'PushNotification', 'deleteChannelGroup', [channelGroupId]);
},

listChannelGroups: function listChannels(successCallback, errorCallback) {
exec(successCallback, errorCallback, 'PushNotification', 'listChannelGroups', []);
},


/**
* PushNotification Object.
Expand All @@ -390,4 +403,4 @@ module.exports = {
* .init helper method.
*/
PushNotification: PushNotification
};
};