Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ module.exports = function (dependencies) {
Client.prototype.createHeaderObject = function createHeaderObject(
uniqueId,
requestId,
channelId
channelId,
notificationId
) {
const header = {};
if (uniqueId) {
Expand All @@ -515,6 +516,9 @@ module.exports = function (dependencies) {
if (channelId) {
header['apns-channel-id'] = channelId;
}
if (notificationId) {
header['apns-id'] = notificationId;
}
return header;
};

Expand All @@ -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(
Expand Down Expand Up @@ -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 => {
Expand All @@ -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) : {};
Expand Down
21 changes: 21 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down