Skip to content

Commit 1a583b3

Browse files
add support for apns-push-type
Required when delivering notifications to devices running iOS 13 and later, or watchOS 6 and later.
1 parent 30c3e89 commit 1a583b3

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

spec/APNS.spec.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ describe('APNS', () => {
172172
};
173173
let expirationTime = 1454571491354;
174174
let collapseId = "collapseIdentifier";
175+
let pushType = "alert"
175176

176-
let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId });
177+
let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId, pushType: pushType });
177178

178179
expect(notification.aps.alert).toEqual({ body: 'alert', title: 'title' });
179180
expect(notification.aps.badge).toEqual(data.badge);
@@ -188,6 +189,7 @@ describe('APNS', () => {
188189
});
189190
expect(notification.expiry).toEqual(Math.round(expirationTime / 1000));
190191
expect(notification.collapseId).toEqual(collapseId);
192+
expect(notification.pushType).toEqual(pushType);
191193
done();
192194
});
193195

@@ -208,11 +210,13 @@ describe('APNS', () => {
208210
};
209211
let expirationTime = 1454571491354;
210212
let collapseId = "collapseIdentifier";
213+
let pushType = "alert"
211214

212-
let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId });
215+
let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId, pushType: pushType });
213216

214217
expect(notification.expiry).toEqual(Math.round(expirationTime / 1000));
215218
expect(notification.collapseId).toEqual(collapseId);
219+
expect(notification.pushType).toEqual(pushType);
216220

217221
let stringifiedJSON = notification.compile();
218222
let jsonObject = JSON.parse(stringifiedJSON);
@@ -280,8 +284,10 @@ describe('APNS', () => {
280284
// Mock data
281285
let expirationTime = 1454571491354;
282286
let collapseId = "collapseIdentifier";
287+
let pushType = "alert" // or background
283288
let data = {
284289
'collapse_id': collapseId,
290+
'push_type': pushType,
285291
'expiration_time': expirationTime,
286292
'data': {
287293
'alert': 'alert'
@@ -312,7 +318,8 @@ describe('APNS', () => {
312318
let notification = calledArgs[0];
313319
expect(notification.aps.alert).toEqual(data.data.alert);
314320
expect(notification.expiry).toEqual(Math.round(data['expiration_time'] / 1000));
315-
expect(notification.collapseId).toEqual(data['collapse_id']);
321+
expect(notification.collapseId).toEqual(collapseId);
322+
expect(notification.pushType).toEqual(pushType);
316323
let apnDevices = calledArgs[1];
317324
expect(apnDevices.length).toEqual(4);
318325
done();
@@ -349,9 +356,11 @@ describe('APNS', () => {
349356
apns.providers = [provider, providerDev];
350357
// Mock data
351358
let expirationTime = 1454571491354;
359+
let pushType = "alert" // or background
352360
let collapseId = "collapseIdentifier";
353361
let data = {
354362
'collapse_id': collapseId,
363+
'push_type': pushType,
355364
'expiration_time': expirationTime,
356365
'data': {
357366
'alert': 'alert'
@@ -389,6 +398,7 @@ describe('APNS', () => {
389398
expect(notification.aps.alert).toEqual(data.data.alert);
390399
expect(notification.expiry).toEqual(Math.round(data['expiration_time'] / 1000));
391400
expect(notification.collapseId).toEqual(data['collapse_id']);
401+
expect(notification.pushType).toEqual(pushType);
392402
let apnDevices = calledArgs[1];
393403
expect(apnDevices.length).toBe(3);
394404

@@ -398,6 +408,7 @@ describe('APNS', () => {
398408
expect(notification.aps.alert).toEqual(data.data.alert);
399409
expect(notification.expiry).toEqual(Math.round(data['expiration_time'] / 1000));
400410
expect(notification.collapseId).toEqual(data['collapse_id']);
411+
expect(notification.pushType).toEqual(pushType);
401412
apnDevices = calledArgs[1];
402413
expect(apnDevices.length).toBe(2);
403414
done();

src/APNS.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class APNS {
7373
let coreData = data.data;
7474
let expirationTime = data['expiration_time'];
7575
let collapseId = data['collapse_id'];
76+
let pushType = data['push_type'];
7677
let allPromises = [];
7778

7879
let devicesPerAppIdentifier = {};
@@ -96,7 +97,7 @@ export class APNS {
9697
continue;
9798
}
9899

99-
let headers = { expirationTime: expirationTime, topic: appIdentifier, collapseId: collapseId }
100+
let headers = { expirationTime: expirationTime, topic: appIdentifier, collapseId: collapseId, pushType: pushType }
100101
let notification = APNS._generateNotification(coreData, headers);
101102
const deviceIds = devices.map(device => device.deviceToken);
102103
let promise = this.sendThroughProvider(notification, deviceIds, providers);
@@ -166,7 +167,7 @@ export class APNS {
166167
/**
167168
* Generate the apns Notification from the data we get from api request.
168169
* @param {Object} coreData The data field under api request body
169-
* @param {Object} headers The header properties for the notification (topic, expirationTime, collapseId)
170+
* @param {Object} headers The header properties for the notification (topic, expirationTime, collapseId, pushType)
170171
* @returns {Object} A apns Notification
171172
*/
172173
static _generateNotification(coreData, headers) {
@@ -214,6 +215,7 @@ export class APNS {
214215
notification.topic = headers.topic;
215216
notification.expiry = Math.round(headers.expirationTime / 1000);
216217
notification.collapseId = headers.collapseId;
218+
notification.pushType = headers.pushType
217219
return notification;
218220
}
219221

0 commit comments

Comments
 (0)