Skip to content

Commit a1263c7

Browse files
committed
add channels method
1 parent 3d348fc commit a1263c7

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

index.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ export class Provider extends EventEmitter {
139139
*/
140140
send(notification: Notification, recipients: string|string[]): Promise<Responses>;
141141

142+
/**
143+
* Manage channels using a specific action.
144+
*
145+
* An "action" specifies what to do with the channel.
146+
*/
147+
manageChannels(notification: Notification, bundleId: string, action: ChannelAction): Promise<Responses>;
148+
149+
/**
150+
* Broadcast to a channel.
151+
*
152+
* An "action" specifies what to do with the channel.
153+
*/
154+
broadcast(notification: Notification, bundleId: string): Promise<Responses>;
155+
142156
/**
143157
* Set an info logger, and optionally an errorLogger to separately log errors.
144158
*
@@ -178,6 +192,8 @@ export class MultiProvider extends EventEmitter {
178192
shutdown(callback?: () => void): void;
179193
}
180194

195+
export type ChannelAction = 'create' | 'read' | 'readAll' | 'delete';
196+
181197
export type NotificationPushType = 'background' | 'alert' | 'voip' | 'pushtotalk' | 'liveactivity' | 'location' | 'complication' | 'fileprovider' | 'mdm';
182198

183199
export interface NotificationAlertOptions {

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ module.exports = function (dependencies) {
192192
const subDirectoryInformation = this.makeSubDirectoryTypeObject(type, subDirectory);
193193
const error = {
194194
...subDirectoryInformation,
195-
error: new VError(`the type "${type}" is invalid`),
195+
error: new VError(`the type "${type}" is not supported`),
196196
};
197197
return Promise.resolve(error);
198198
}

lib/provider.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const EventEmitter = require('events');
2+
const VError = require('verror');
23

34
module.exports = function (dependencies) {
45
const Client = dependencies.Client;
@@ -42,6 +43,51 @@ module.exports = function (dependencies) {
4243
});
4344
};
4445

46+
Provider.prototype.manageChannels = function manageChannels(notification, bundleId, action) {
47+
let type = 'channels';
48+
let method = 'post';
49+
50+
switch (action) {
51+
case 'create':
52+
if (notification['push-type'] == null) {
53+
// Add live activity push type if it's not already provided.
54+
// Live activity is the only current type supported.
55+
// Note, this seems like it should be lower cased, but the
56+
// docs shows it in the current format.
57+
notification['push-type'] = 'LiveActivity';
58+
}
59+
type = 'channels';
60+
method = 'post';
61+
break;
62+
case 'read':
63+
type = 'channels';
64+
method = 'get';
65+
break;
66+
case 'readAll':
67+
type = 'allChannels';
68+
method = 'get';
69+
break;
70+
case 'delete':
71+
type = 'channels';
72+
method = 'delete';
73+
break;
74+
default: {
75+
const error = {
76+
bundleId,
77+
error: new VError(`the action "${action}" is not supported`),
78+
};
79+
return Promise.resolve(error);
80+
}
81+
}
82+
83+
const builtNotification = {
84+
headers: notification.headers(),
85+
body: notification.compile(),
86+
};
87+
88+
return this.client.write(builtNotification, bundleId, type, method);
89+
};
90+
4591
Provider.prototype.broadcast = function broadcast(notification, bundleId) {
4692
const builtNotification = {
4793
headers: notification.headers(),

0 commit comments

Comments
 (0)