Skip to content

Commit 5567de1

Browse files
authored
feat: Add apns-id header to client return value (#195)
1 parent 0d8bc6b commit 5567de1

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

lib/client.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ module.exports = function (dependencies) {
503503
Client.prototype.createHeaderObject = function createHeaderObject(
504504
uniqueId,
505505
requestId,
506-
channelId
506+
channelId,
507+
notificationId
507508
) {
508509
const header = {};
509510
if (uniqueId) {
@@ -515,6 +516,9 @@ module.exports = function (dependencies) {
515516
if (channelId) {
516517
header['apns-channel-id'] = channelId;
517518
}
519+
if (notificationId) {
520+
header['apns-id'] = notificationId;
521+
}
518522
return header;
519523
};
520524

@@ -531,6 +535,7 @@ module.exports = function (dependencies) {
531535
let uniqueId = null;
532536
let requestId = null;
533537
let channelId = null;
538+
let notificationId = null;
534539
let responseData = '';
535540

536541
const headers = extend(
@@ -561,6 +566,7 @@ module.exports = function (dependencies) {
561566
uniqueId = headers['apns-unique-id'];
562567
requestId = headers['apns-request-id'];
563568
channelId = headers['apns-channel-id'];
569+
notificationId = headers['apns-id'];
564570
});
565571

566572
request.on('data', data => {
@@ -577,7 +583,12 @@ module.exports = function (dependencies) {
577583
if (this.logger.enabled) {
578584
this.logger(`Request ended with status ${status} and responseData: ${responseData}`);
579585
}
580-
const headerObject = this.createHeaderObject(uniqueId, requestId, channelId);
586+
const headerObject = this.createHeaderObject(
587+
uniqueId,
588+
requestId,
589+
channelId,
590+
notificationId
591+
);
581592

582593
if (status === 200 || status === 201 || status === 204) {
583594
const body = responseData !== '' ? JSON.parse(responseData) : {};

test/client.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,27 @@ describe('Client', () => {
356356
expect(errorMessages).to.be.empty;
357357
});
358358

359+
it('Returns APNs notification ID in responses', async () => {
360+
const notificationId = '7dc35f9f-58d4-40dd-8c08-38ab811f57df';
361+
362+
server = createAndStartMockServer(TEST_PORT, (req, res) => {
363+
res.writeHead(200, { 'apns-id': notificationId });
364+
res.end('');
365+
});
366+
await new Promise(resolve => server.on('listening', resolve));
367+
368+
client = createClient(CLIENT_TEST_PORT);
369+
370+
const mockNotification = {
371+
headers: { 'apns-someheader': 'somevalue' },
372+
body: MOCK_BODY,
373+
};
374+
const device = MOCK_DEVICE_TOKEN;
375+
const result = await client.write(mockNotification, device, 'device', 'post');
376+
377+
expect(result).to.deep.equal({ 'apns-id': notificationId, device });
378+
});
379+
359380
// https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/handling_notification_responses_from_apns
360381
it('JSON decodes HTTP 400 responses', async () => {
361382
let didRequest = false;

0 commit comments

Comments
 (0)