Skip to content

Commit

Permalink
Merge pull request #149 from jkblume/extract_push_method_by_reg_id_me…
Browse files Browse the repository at this point in the history
…thod

Extracted method that 'detects push method by reg id'
  • Loading branch information
alex-friedl committed Apr 24, 2021
2 parents 99fc0bb + 012dae7 commit 45a84b2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -115,6 +115,8 @@ registrationIds.push('INSERT_YOUR_DEVICE_ID');
registrationIds.push('INSERT_OTHER_DEVICE_ID');
```

The `PN.send()` method later detects device type and therefore used push method, based on the id stucture. Check out the method `PN.getPushMethodByRegId` how this detection works.

**Android:**

- If you provide more than 1.000 registration tokens, they will automatically be splitted in 1.000 chunks (see [this issue in gcm repo](https://github.com/ToothlessGear/node-gcm/issues/42))
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Expand Up @@ -5,6 +5,7 @@ module.exports = {
ADM_METHOD: 'adm',
WNS_METHOD: 'wns',
WEB_METHOD: 'webPush',
UNKNOWN_METHOD: 'unknown',
DEFAULT_SETTINGS: {
gcm: {
id: null, // PUT YOUR GCM SERVER API KEY,
Expand Down
46 changes: 38 additions & 8 deletions src/push-notifications.js
Expand Up @@ -3,7 +3,15 @@ import APN from './sendAPN';
import sendADM from './sendADM';
import sendWNS from './sendWNS';
import sendWebPush from './sendWeb';
import { DEFAULT_SETTINGS } from './constants';
import {
DEFAULT_SETTINGS,
UNKNOWN_METHOD,
WEB_METHOD,
WNS_METHOD,
ADM_METHOD,
GCM_METHOD,
APN_METHOD,
} from './constants';

class PN {
constructor(options) {
Expand All @@ -30,6 +38,28 @@ class PN {
});
}

getPushMethodByRegId(regId) {
if (typeof regId === 'object') {
return WEB_METHOD;
}
if (this.settings.isAlwaysUseFCM) {
return GCM_METHOD;
}
if (regId.substring(0, 4) === 'http') {
return WNS_METHOD;
}
if (/^(amzn[0-9]*.adm)/i.test(regId)) {
return ADM_METHOD;
}
if (regId.length > 64) {
return GCM_METHOD;
}
if (regId.length === 64) {
return APN_METHOD;
}
return UNKNOWN_METHOD;
}

send(_regIds, data, callback) {
const promises = [];
const regIdsGCM = [];
Expand All @@ -42,17 +72,17 @@ class PN {

// Classify each pushId for corresponding device
regIds.forEach((regId) => {
if (typeof regId === 'object') {
const pushMethod = this.getPushMethodByRegId(regId);

if (pushMethod === WEB_METHOD) {
regIdsWebPush.push(regId);
} else if (this.settings.isAlwaysUseFCM) {
} else if (pushMethod === GCM_METHOD) {
regIdsGCM.push(regId);
} else if (regId.substring(0, 4) === 'http') {
} else if (pushMethod === WNS_METHOD) {
regIdsWNS.push(regId);
} else if (/^(amzn[0-9]*.adm)/i.test(regId)) {
} else if (pushMethod === ADM_METHOD) {
regIdsADM.push(regId);
} else if (regId.length > 64) {
regIdsGCM.push(regId);
} else if (regId.length === 64) {
} else if (pushMethod === APN_METHOD) {
regIdsAPN.push(regId);
} else {
regIdsUnk.push(regId);
Expand Down
81 changes: 81 additions & 0 deletions test/push-notifications/basic.js
Expand Up @@ -5,6 +5,14 @@ import dirtyChai from 'dirty-chai';
import sinonChai from 'sinon-chai';
import { spy } from 'sinon';
import PN from '../../src';
import {
UNKNOWN_METHOD,
WEB_METHOD,
WNS_METHOD,
ADM_METHOD,
GCM_METHOD,
APN_METHOD,
} from '../../src/constants';

const { expect } = chai;
chai.use(dirtyChai);
Expand Down Expand Up @@ -135,4 +143,77 @@ describe('push-notifications: instantiation and class properties', () => {
);
});
});

describe('check getPushMethodByRegId returns expected push method types', () => {
const regIds = {
androidRegId:
'APA91bFQCD9Ndd8uVggMhj1usfeWsKIfGyBUWMprpZLGciWrMjS-77bIY24IMQNeEHzjidCcddnDxqYo-UEV03xw6ySmtIgQyzTqhSxhPGAi1maf6KDMAQGuUWc6L5Khze8YK9YrL9I_WD1gl49P3f_9hr08ZAS5Tw',
androidWithAdmSubstringRegId:
'APA9admQCD9Ndd8uVggMhj1usfeWsKIfGyBUWMprpZLGciWrMjS-77bIY24IMQNeEHzjidCcddnDxqYo-UEV03xw6ySmtIgQyzTqhSxhPGAi1maf6KDMAQGuUWc6L5Khze8YK9YrL9I_WD1gl49P3f_9hr08ZAS5Tw',
androidWithAmznSubscringRegId:
'amzn1mQCD9Ndd8uVggMhj1usfeWsKIfGyBUWMprpZLGciWrMjS-77bIY24IMQNeEHzjidCcddnDxqYo-UEV03xw6ySmtIgQyzTqhSxhPGAi1maf6KDMAQGuUWc6L5Khze8YK9YrL9I_WD1gl49P3f_9hr08ZAS5Tw',
iOSRegId:
'43e798c31a282d129a34d84472bbdd7632562ff0732b58a85a27c5d9fdf59b69',
windowsPhoneRegId:
'https://db5.notify.windows.com/?token=AwYAAAD8sfbDrL9h7mN%2bmwlkSkQZCIfv4QKeu1hYRipj2zNvXaMi9ZAax%2f6CDfysyHp61STCO1pCFPt%2b9L4Jod72JhIcjDr8b2GxuUOBMTP%2b6%2bqxEfSB9iZfSATdZbdF7cJHSRA%3d',
amazonRegId:
'amzn1.adm-registration.v2.Y29tLmFtYXpvbi5EZXZpY2VNZXNzYWdpbmcuUmVnaXN0cmF0aW9uSWRFbmNyeXB0aW9uS2V5ITEhOE9rZ2h5TXlhVEFFczg2ejNWL3JMcmhTa255Uk5BclhBbE1XMFZzcnU1aFF6cTlvdU5FbVEwclZmdk5oTFBVRXVDN1luQlRSNnRVRUViREdQSlBvSzRNaXVRRUlyUy9NYWZCYS9VWTJUaGZwb3ZVTHhlRTM0MGhvampBK01hVktsMEhxakdmQStOSXRjUXBTQUhNU1NlVVVUVkFreVRhRTBCYktaQ2ZkUFdqSmIwcHgzRDhMQnllVXdxQ2EwdHNXRmFVNklYL0U4UXovcHg0K3Jjb25VbVFLRUVVOFVabnh4RDhjYmtIcHd1ZThiekorbGtzR2taMG95cC92Y3NtZytrcTRPNjhXUUpiZEk3QzFvQThBRTFWWXM2NHkyMjdYVGV5RlhhMWNHS0k9IW5GNEJMSXNleC9xbWpHSU52NnczY0E9PQ',
webRegId: {
endpoint: 'https://push.subscription.url',
keys: {
p256dh: 'userPublicEncryptionKey',
auth: 'userAuthSecret',
},
},
unknownRegId: 'abcdef',
};

it('Android / GCM', () => {
let pn = new PN();
expect(pn.getPushMethodByRegId(regIds.androidRegId)).to.equal(GCM_METHOD);

expect(
pn.getPushMethodByRegId(regIds.androidWithAdmSubstringRegId)
).to.equal(GCM_METHOD);

expect(
pn.getPushMethodByRegId(regIds.androidWithAmznSubscringRegId)
).to.equal(GCM_METHOD);

const settings = {
isAlwaysUseFCM: true,
};
pn = new PN(settings);
expect(pn.getPushMethodByRegId(regIds.unknownRegId)).to.equal(GCM_METHOD);
});

it('iOS / APN', () => {
const pn = new PN();
expect(pn.getPushMethodByRegId(regIds.iOSRegId)).to.equal(APN_METHOD);
});

it('Windows Phone / WNS', () => {
const pn = new PN();
expect(pn.getPushMethodByRegId(regIds.windowsPhoneRegId)).to.equal(
WNS_METHOD
);
});

it('Amazon / ADM', () => {
const pn = new PN();
expect(pn.getPushMethodByRegId(regIds.amazonRegId)).to.equal(ADM_METHOD);
});

it('Web / WEB', () => {
const pn = new PN();
expect(pn.getPushMethodByRegId(regIds.webRegId)).to.equal(WEB_METHOD);
});

it('Unknown / UNKNOWN', () => {
const pn = new PN();
expect(pn.getPushMethodByRegId(regIds.unknownRegId)).to.equal(
UNKNOWN_METHOD
);
});
});
});

0 comments on commit 45a84b2

Please sign in to comment.