diff --git a/lib/client.js b/lib/client.js index 7e8b5e9e..8703e9b1 100644 --- a/lib/client.js +++ b/lib/client.js @@ -503,7 +503,8 @@ module.exports = function (dependencies) { Client.prototype.createHeaderObject = function createHeaderObject( uniqueId, requestId, - channelId + channelId, + notificationId ) { const header = {}; if (uniqueId) { @@ -515,6 +516,9 @@ module.exports = function (dependencies) { if (channelId) { header['apns-channel-id'] = channelId; } + if (notificationId) { + header['apns-id'] = notificationId; + } return header; }; @@ -531,6 +535,7 @@ module.exports = function (dependencies) { let uniqueId = null; let requestId = null; let channelId = null; + let notificationId = null; let responseData = ''; const headers = extend( @@ -561,6 +566,7 @@ module.exports = function (dependencies) { uniqueId = headers['apns-unique-id']; requestId = headers['apns-request-id']; channelId = headers['apns-channel-id']; + notificationId = headers['apns-id']; }); request.on('data', data => { @@ -577,7 +583,12 @@ module.exports = function (dependencies) { if (this.logger.enabled) { this.logger(`Request ended with status ${status} and responseData: ${responseData}`); } - const headerObject = this.createHeaderObject(uniqueId, requestId, channelId); + const headerObject = this.createHeaderObject( + uniqueId, + requestId, + channelId, + notificationId + ); if (status === 200 || status === 201 || status === 204) { const body = responseData !== '' ? JSON.parse(responseData) : {}; diff --git a/test/client.js b/test/client.js index 3b9f5d5e..06902ca1 100644 --- a/test/client.js +++ b/test/client.js @@ -356,6 +356,27 @@ describe('Client', () => { expect(errorMessages).to.be.empty; }); + it('Returns APNs notification ID in responses', async () => { + const notificationId = '7dc35f9f-58d4-40dd-8c08-38ab811f57df'; + + server = createAndStartMockServer(TEST_PORT, (req, res) => { + res.writeHead(200, { 'apns-id': notificationId }); + res.end(''); + }); + await new Promise(resolve => server.on('listening', resolve)); + + client = createClient(CLIENT_TEST_PORT); + + const mockNotification = { + headers: { 'apns-someheader': 'somevalue' }, + body: MOCK_BODY, + }; + const device = MOCK_DEVICE_TOKEN; + const result = await client.write(mockNotification, device, 'device', 'post'); + + expect(result).to.deep.equal({ 'apns-id': notificationId, device }); + }); + // https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/handling_notification_responses_from_apns it('JSON decodes HTTP 400 responses', async () => { let didRequest = false;