Skip to content
46 changes: 40 additions & 6 deletions spec/APNS.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ describe('APNS', () => {
};
let expirationTime = 1454571491354;
let collapseId = "collapseIdentifier";
let priority = 5;

let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId, priority: priority });
let pushType = "alert";
let priority = 5;
let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId, pushType: pushType, priority: priority });

expect(notification.aps.alert).toEqual({ body: 'alert', title: 'title' });
expect(notification.aps.badge).toEqual(data.badge);
Expand All @@ -189,11 +190,35 @@ describe('APNS', () => {
});
expect(notification.expiry).toEqual(Math.round(expirationTime / 1000));
expect(notification.collapseId).toEqual(collapseId);
expect(notification.pushType).toEqual(pushType);
expect(notification.priority).toEqual(priority);
done();
});

it('sets push type to alert if not defined explicitly', (done) => {
//Mock request data
let data = {
'alert': 'alert',
'title': 'title',
'badge': 100,
'sound': 'test',
'content-available': 1,
'mutable-content': 1,
'category': 'INVITE_CATEGORY',
'threadId': 'a-thread-id',
'key': 'value',
'keyAgain': 'valueAgain'
};
let expirationTime = 1454571491354;
let collapseId = "collapseIdentifier";

let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId });

expect(notification.pushType).toEqual('alert');
done();
});

it('can generate APNS notification from raw data', (done) => {
it('can generate APNS notification from raw data', (done) => {
//Mock request data
let data = {
'aps': {
Expand All @@ -210,12 +235,14 @@ describe('APNS', () => {
};
let expirationTime = 1454571491354;
let collapseId = "collapseIdentifier";
let priority = 5
let pushType = "background";
let priority = 5;

let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId, priority: priority });
let notification = APNS._generateNotification(data, { expirationTime: expirationTime, collapseId: collapseId, pushType: pushType, priority: priority });

expect(notification.expiry).toEqual(Math.round(expirationTime / 1000));
expect(notification.collapseId).toEqual(collapseId);
expect(notification.pushType).toEqual(pushType);
expect(notification.priority).toEqual(priority);

let stringifiedJSON = notification.compile();
Expand Down Expand Up @@ -284,8 +311,10 @@ describe('APNS', () => {
// Mock data
let expirationTime = 1454571491354;
let collapseId = "collapseIdentifier";
let pushType = "alert"; // or background
let data = {
'collapse_id': collapseId,
'push_type': pushType,
'expiration_time': expirationTime,
'priority': 6,
'data': {
Expand Down Expand Up @@ -317,7 +346,8 @@ describe('APNS', () => {
let notification = calledArgs[0];
expect(notification.aps.alert).toEqual(data.data.alert);
expect(notification.expiry).toEqual(Math.round(data['expiration_time'] / 1000));
expect(notification.collapseId).toEqual(data['collapse_id']);
expect(notification.collapseId).toEqual(collapseId);
expect(notification.pushType).toEqual(pushType);
expect(notification.priority).toEqual(data['priority']);
let apnDevices = calledArgs[1];
expect(apnDevices.length).toEqual(4);
Expand Down Expand Up @@ -355,9 +385,11 @@ describe('APNS', () => {
apns.providers = [provider, providerDev];
// Mock data
let expirationTime = 1454571491354;
let pushType = "alert"; // or background
let collapseId = "collapseIdentifier";
let data = {
'collapse_id': collapseId,
'push_type': pushType,
'expiration_time': expirationTime,
'data': {
'alert': 'alert'
Expand Down Expand Up @@ -395,6 +427,7 @@ describe('APNS', () => {
expect(notification.aps.alert).toEqual(data.data.alert);
expect(notification.expiry).toEqual(Math.round(data['expiration_time'] / 1000));
expect(notification.collapseId).toEqual(data['collapse_id']);
expect(notification.pushType).toEqual(pushType);
let apnDevices = calledArgs[1];
expect(apnDevices.length).toBe(3);

Expand All @@ -404,6 +437,7 @@ describe('APNS', () => {
expect(notification.aps.alert).toEqual(data.data.alert);
expect(notification.expiry).toEqual(Math.round(data['expiration_time'] / 1000));
expect(notification.collapseId).toEqual(data['collapse_id']);
expect(notification.pushType).toEqual(pushType);
apnDevices = calledArgs[1];
expect(apnDevices.length).toBe(2);
done();
Expand Down
10 changes: 8 additions & 2 deletions src/APNS.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class APNS {
let coreData = data.data;
let expirationTime = data['expiration_time'];
let collapseId = data['collapse_id'];
let pushType = data['push_type'];
let priority = data['priority'];
let allPromises = [];

Expand All @@ -97,7 +98,7 @@ export class APNS {
continue;
}

let headers = { expirationTime: expirationTime, topic: appIdentifier, collapseId: collapseId, priority: priority }
let headers = { expirationTime: expirationTime, topic: appIdentifier, collapseId: collapseId, pushType: pushType, priority: priority }
let notification = APNS._generateNotification(coreData, headers);
const deviceIds = devices.map(device => device.deviceToken);
let promise = this.sendThroughProvider(notification, deviceIds, providers);
Expand Down Expand Up @@ -167,7 +168,7 @@ export class APNS {
/**
* Generate the apns Notification from the data we get from api request.
* @param {Object} coreData The data field under api request body
* @param {Object} headers The header properties for the notification (topic, expirationTime, collapseId, priority)
* @param {Object} headers The header properties for the notification (topic, expirationTime, collapseId, pushType, priority)
* @returns {Object} A apns Notification
*/
static _generateNotification(coreData, headers) {
Expand Down Expand Up @@ -215,6 +216,11 @@ export class APNS {
notification.topic = headers.topic;
notification.expiry = Math.round(headers.expirationTime / 1000);
notification.collapseId = headers.collapseId;
// set alert as default push type. If push type is not set notifications are not delivered to devices running iOS 13, watchOS 6 and later.
notification.pushType = 'alert';
if (headers.pushType) {
notification.pushType = headers.pushType;
}
if (headers.priority) {
// if headers priority is not set 'node-apn' defaults it to 5 which is min. required value for background pushes to launch the app in background.
notification.priority = headers.priority
Expand Down