Skip to content

Commit d8e70e3

Browse files
committed
✨ Issue phonegap#1949: add a listChannels method
1 parent a301e47 commit d8e70e3

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed

docs/API.md

+29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [.hasPermission()](#pushnotificationhaspermissionsuccesshandler)
55
- [.createChannel() - Android only](#pushnotificationcreatechannel)
66
- [.deleteChannel() - Android only](#pushnotificationdeletechannel)
7+
- [.listChannels() - Android only](#pushnotificationlistchannels)
78
- [push.on()](#pushonevent-callback)
89
- [push.on('registration')](#pushonregistration-callback)
910
- [push.on('notification')](#pushonnotification-callback)
@@ -194,6 +195,34 @@ PushNotification.deleteChannel(() => {
194195
});
195196
```
196197

198+
## PushNotification.listChannels(successHandler)
199+
200+
Returns a list of currently configured channels.
201+
202+
### Parameters
203+
204+
Parameter | Type | Default | Description
205+
--------- | ---- | ------- | -----------
206+
`successHandler` | `Function` | | Is called when the api successfully retrieves the list of channels.
207+
208+
### Callback parameters
209+
210+
#### `successHandler`
211+
212+
Parameter | Type | Description
213+
--------- | ---- | -----------
214+
`channels` | `JSONArrary` | List of channel objects.
215+
216+
### Example
217+
218+
```javascript
219+
PushNotification.listChannels((channels) => {
220+
for(let channel of channels) {
221+
console.log(`ID: ${channel.id} Description: ${channel.description}`);
222+
}
223+
});
224+
```
225+
197226
## push.on(event, callback)
198227

199228
### Parameters

spec/index.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ describe('phonegap-plugin-push', function () {
4747
expect(typeof PushNotification.deleteChannel === 'function').toBe(true);
4848
});
4949

50+
it('should contain a listChannels function', function () {
51+
expect(PushNotification.listChannels).toBeDefined();
52+
expect(typeof PushNotification.listChannels === 'function').toBe(true);
53+
});
54+
5055
it('should contain a unregister function', function () {
5156
var push = PushNotification.init({});
5257
expect(push.unregister).toBeDefined();

src/android/com/adobe/phonegap/push/PushConstants.java

+1
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,5 @@ public interface PushConstants {
9797
public static final String CREATE_CHANNEL = "createChannel";
9898
public static final String DELETE_CHANNEL = "deleteChannel";
9999
public static final String ONGOING = "ongoing";
100+
public static final String LIST_CHANNELS = "listChannels";
100101
}

src/android/com/adobe/phonegap/push/PushPlugin.java

+29
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ private Context getApplicationContext() {
5656
return this.cordova.getActivity().getApplicationContext();
5757
}
5858

59+
@TargetApi(26)
60+
private JSONArray listChannels() throws JSONException {
61+
JSONArray channels = new JSONArray();
62+
// only call on Android O and above
63+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
64+
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
65+
.getSystemService(Context.NOTIFICATION_SERVICE);
66+
List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
67+
for (NotificationChannel notificationChannel : notificationChannels) {
68+
JSONObject channel = new JSONObject();
69+
channel.put(CHANNEL_ID, notificationChannel.getId());
70+
channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription());
71+
channels.put(channel);
72+
}
73+
}
74+
return channels;
75+
}
76+
5977
@TargetApi(26)
6078
private void deleteChannel(String channelId) {
6179
// only call on Android O and above
@@ -358,6 +376,17 @@ public void run() {
358376
}
359377
}
360378
});
379+
} else if (LIST_CHANNELS.equals(action)) {
380+
// un-subscribing for a topic
381+
cordova.getThreadPool().execute(new Runnable() {
382+
public void run() {
383+
try {
384+
callbackContext.success(listChannels());
385+
} catch (JSONException e) {
386+
callbackContext.error(e.getMessage());
387+
}
388+
}
389+
});
361390
} else {
362391
Log.e(LOG_TAG, "Invalid action : " + action);
363392
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));

src/js/push.js

+4
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ module.exports = {
313313
exec(successCallback, errorCallback, 'PushNotification', 'deleteChannel', [channelId]);
314314
},
315315

316+
listChannels: (successCallback, errorCallback) => {
317+
exec(successCallback, errorCallback, 'PushNotification', 'listChannels', []);
318+
},
319+
316320
/**
317321
* PushNotification Object.
318322
*

www/push.js

+4
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ module.exports = {
354354
exec(successCallback, errorCallback, 'PushNotification', 'deleteChannel', [channelId]);
355355
},
356356

357+
listChannels: function listChannels(successCallback, errorCallback) {
358+
exec(successCallback, errorCallback, 'PushNotification', 'listChannels', []);
359+
},
360+
357361
/**
358362
* PushNotification Object.
359363
*

0 commit comments

Comments
 (0)