Skip to content

Regenerate the iOS sender at every call #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@parse/push-adapter",
"version": "2.0.2",
"version": "2.0.3-alpha-1",
"description": "Base parse-server-push-adapter",
"main": "lib/index.js",
"files": [
Expand Down
5 changes: 5 additions & 0 deletions spec/ParsePushAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ describe('ParsePushAdapter', () => {
// Check ios
var iosSender = parsePushAdapter.senderMap['ios'];
expect(iosSender instanceof APNS).toBe(true);
var senderAgain = parsePushAdapter.senderMap['ios'];
// A new instance should be generated at every calls
expect(senderAgain).toEqual(iosSender);
// Check android
var androidSender = parsePushAdapter.senderMap['android'];
expect(androidSender instanceof GCM).toBe(true);
var androidSenderAgain = parsePushAdapter.senderMap['android'];
expect(androidSenderAgain).toEqual(androidSender);
done();
});

Expand Down
56 changes: 35 additions & 21 deletions src/APNS.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,18 @@ export class APNS {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'APNS Configuration is invalid');
}

// Create Provider from each arg-object
for (let apnsArgs of apnsArgsList) {

// rewrite bundleId to topic for backward-compatibility
if (apnsArgs.bundleId) {
log.warn(LOG_PREFIX, 'bundleId is deprecated, use topic instead');
apnsArgs.topic = apnsArgs.bundleId
}

let provider = APNS._createProvider(apnsArgs);
this.providers.push(provider);
}

// Sort the providers based on priority ascending, high pri first
this.providers.sort((s1, s2) => {
return s1.priority - s2.priority;
});

// Set index-property of providers
for (let index = 0; index < this.providers.length; index++) {
this.providers[index].index = index;
let restartInterval = 30*60*1000; // refresh the providers every 30 minutes
if (process.env.PARSE_SERVER_PUSH_ADAPTER_APNS_REFRESH) {
restartInterval = parseInt(process.env.PARSE_SERVER_PUSH_ADAPTER_APNS_REFRESH);
}
this._createProviders(apnsArgsList);
setInterval(() => {
this.providers.forEach((provider) => {
provider.shutdown();
});
log.info(LOG_PREFIX, 're-creating providers');
this._createProviders(apnsArgsList);
}, restartInterval);
}

/**
Expand Down Expand Up @@ -139,6 +129,30 @@ export class APNS {
return !(apnsArgs.cert || apnsArgs.key || apnsArgs.pfx);
}

_createProviders(apnsArgsList) {
// Create Provider from each arg-object
for (let apnsArgs of apnsArgsList) {
// rewrite bundleId to topic for backward-compatibility
if (apnsArgs.bundleId) {
log.warn(LOG_PREFIX, 'bundleId is deprecated, use topic instead');
apnsArgs.topic = apnsArgs.bundleId
}

let provider = APNS._createProvider(apnsArgs);
this.providers.push(provider);
}

// Sort the providers based on priority ascending, high pri first
this.providers.sort((s1, s2) => {
return s1.priority - s2.priority;
});

// Set index-property of providers
for (let index = 0; index < this.providers.length; index++) {
this.providers[index].index = index;
}
}

/**
* Creates an Provider base on apnsArgs.
*/
Expand Down