Skip to content

Commit 53393f0

Browse files
committed
make JS match TS
1 parent 8ec017d commit 53393f0

File tree

3 files changed

+100
-37
lines changed

3 files changed

+100
-37
lines changed

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ export class MultiProvider extends EventEmitter {
223223
shutdown(callback?: () => void): void;
224224
}
225225

226-
export type ChannelAction = 'create' | 'read' | 'readAll' | 'delete';
227-
228226
export type NotificationPushType = 'background' | 'alert' | 'voip' | 'pushtotalk' | 'liveactivity' | 'location' | 'complication' | 'fileprovider' | 'mdm';
229227

228+
export type ChannelAction = 'create' | 'read' | 'readAll' | 'delete';
229+
230230
export interface NotificationAlertOptions {
231231
title?: string;
232232
subtitle?: string;

lib/notification/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ Notification.prototype.headers = function headers() {
103103
return headers;
104104
};
105105

106+
Notification.prototype.removeNonChannelRelatedProperties = function () {
107+
this.priority = undefined;
108+
this.id = undefined;
109+
this.collapseId = undefined;
110+
this.expiry = undefined;
111+
this.topic = undefined;
112+
this.pushType = undefined;
113+
};
114+
115+
/**
116+
* Add live activity push type if it's not already provided.
117+
*
118+
* @remarks
119+
* LiveActivity is the only current type supported.
120+
*/
121+
Notification.prototype.addPushTypeToPayloadIfNeeded = function () {
122+
if (this.rawPayload == undefined && this.payload['push-type'] == undefined) {
123+
this.payload['push-type'] = 'liveactivity';
124+
}
125+
};
126+
106127
/**
107128
* Compile a notification down to its JSON format. Compilation is final, changes made to the notification after this method is called will not be reflected in further calls.
108129
* @returns {String} JSON payload for the notification.

lib/provider.js

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function (dependencies) {
1616

1717
Provider.prototype = Object.create(EventEmitter.prototype);
1818

19-
Provider.prototype.send = function send(notification, recipients) {
19+
Provider.prototype.send = async function send(notification, recipients) {
2020
const builtNotification = {
2121
headers: notification.headers(),
2222
body: notification.compile(),
@@ -26,38 +26,41 @@ module.exports = function (dependencies) {
2626
recipients = [recipients];
2727
}
2828

29-
return Promise.all(
30-
recipients.map(token => this.client.write(builtNotification, token, 'device', 'post'))
31-
).then(responses => {
32-
const sent = [];
33-
const failed = [];
34-
35-
responses.forEach(response => {
36-
if (response.status || response.error) {
37-
failed.push(response);
38-
} else {
39-
sent.push(response);
40-
}
41-
});
42-
return { sent, failed };
29+
const sentNotifications = await Promise.all(
30+
recipients.map(
31+
async token => await this.client.write(builtNotification, token, 'device', 'post')
32+
)
33+
);
34+
const sent = [];
35+
const failed = [];
36+
37+
sentNotifications.forEach(sentNotification => {
38+
if (sentNotification.status || sentNotification.error) {
39+
failed.push(sentNotification);
40+
} else {
41+
sent.push(sentNotification);
42+
}
4343
});
44+
45+
return { sent, failed };
4446
};
4547

46-
Provider.prototype.manageChannels = function manageChannels(notification, bundleId, action) {
48+
Provider.prototype.manageChannels = async function manageChannels(
49+
notifications,
50+
bundleId,
51+
action
52+
) {
4753
let type = 'channels';
4854
let method = 'post';
4955

56+
if (!Array.isArray(notifications)) {
57+
notifications = [notifications];
58+
}
59+
5060
switch (action) {
5161
case 'create':
5262
type = 'channels';
5363
method = 'post';
54-
if (notification['push-type'] == null) {
55-
// Add live activity push type if it's not already provided.
56-
// Live activity is the only current type supported.
57-
// Note, this seems like it should be lower cased, but the
58-
// docs shows it in the current format.
59-
notification['push-type'] = 'LiveActivity';
60-
}
6164
break;
6265
case 'read':
6366
type = 'channels';
@@ -76,25 +79,64 @@ module.exports = function (dependencies) {
7679
bundleId,
7780
error: new VError(`the action "${action}" is not supported`),
7881
};
79-
return Promise.resolve(error);
82+
return error;
8083
}
8184
}
8285

83-
const builtNotification = {
84-
headers: notification.headers(),
85-
body: notification.compile(),
86-
};
86+
const sentNotifications = await Promise.all(
87+
notifications.map(async notification => {
88+
if (action == 'create') {
89+
notification.addPushTypeToPayloadIfNeeded();
90+
}
91+
const builtNotification = {
92+
headers: notification.headers(),
93+
body: notification.compile(),
94+
};
8795

88-
return this.client.write(builtNotification, bundleId, type, method);
96+
return await this.client.write(builtNotification, bundleId, type, method);
97+
})
98+
);
99+
const sent = [];
100+
const failed = [];
101+
102+
sentNotifications.forEach(sentNotification => {
103+
if (sentNotification.status || sentNotification.error) {
104+
failed.push(sentNotification);
105+
} else {
106+
sent.push(sentNotification);
107+
}
108+
});
109+
110+
return { sent, failed };
89111
};
90112

91-
Provider.prototype.broadcast = function broadcast(notification, bundleId) {
92-
const builtNotification = {
93-
headers: notification.headers(),
94-
body: notification.compile(),
95-
};
113+
Provider.prototype.broadcast = async function broadcast(notifications, bundleId) {
114+
if (!Array.isArray(notifications)) {
115+
notifications = [notifications];
116+
}
117+
118+
const sentNotifications = await Promise.all(
119+
notifications.map(async notification => {
120+
const builtNotification = {
121+
headers: notification.headers(),
122+
body: notification.compile(),
123+
};
124+
125+
return await this.client.write(builtNotification, bundleId, 'broadcasts', 'post');
126+
})
127+
);
128+
const sent = [];
129+
const failed = [];
130+
131+
sentNotifications.forEach(sentNotification => {
132+
if (sentNotification.status || sentNotification.error) {
133+
failed.push(sentNotification);
134+
} else {
135+
sent.push(sentNotification);
136+
}
137+
});
96138

97-
return this.client.write(builtNotification, bundleId, 'broadcasts', 'post');
139+
return { sent, failed };
98140
};
99141

100142
Provider.prototype.shutdown = function shutdown(callback) {

0 commit comments

Comments
 (0)