Removes shared code in PushAdapter, replaces AdapterLoader.load by loadAdapter

This commit is contained in:
Florent Vilmart
2016-02-22 18:31:10 -05:00
parent 8ce8e2bbe0
commit 48dcfe37e7
9 changed files with 97 additions and 84 deletions

View File

@@ -3,6 +3,8 @@
// PushAdapter, it uses GCM for android push and APNS
// for ios push.
import { classifyInstallations } from './PushAdapterUtils';
const Parse = require('parse/node').Parse;
var deepcopy = require('deepcopy');
import PushAdapter from './PushAdapter';
@@ -25,7 +27,7 @@ export class OneSignalPushAdapter extends PushAdapter {
send(data, installations) {
console.log("Sending notification to "+installations.length+" devices.")
let deviceMap = PushAdapter.classifyInstallation(installations, this.validPushTypes);
let deviceMap = classifyInstallations(installations, this.validPushTypes);
let sendPromises = [];
for (let pushType in deviceMap) {
@@ -43,6 +45,14 @@ export class OneSignalPushAdapter extends PushAdapter {
return Parse.Promise.when(sendPromises);
}
static classifyInstallations(installations, validTypes) {
return classifyInstallations(installations, validTypes)
}
getValidPushTypes() {
return this.validPushTypes;
}
sendToAPNS(data,tokens) {
data= deepcopy(data['data']);

View File

@@ -7,6 +7,7 @@ const Parse = require('parse/node').Parse;
const GCM = require('../../GCM');
const APNS = require('../../APNS');
import PushAdapter from './PushAdapter';
import { classifyInstallations } from './PushAdapterUtils';
export class ParsePushAdapter extends PushAdapter {
constructor(pushConfig = {}) {
@@ -31,8 +32,16 @@ export class ParsePushAdapter extends PushAdapter {
}
}
getValidPushTypes() {
return this.validPushTypes;
}
static classifyInstallations(installations, validTypes) {
return classifyInstallations(installations, validTypes)
}
send(data, installations) {
let deviceMap = PushAdapter.classifyInstallation(installations, this.validPushTypes);
let deviceMap = classifyInstallations(installations, this.validPushTypes);
let sendPromises = [];
for (let pushType in deviceMap) {
let sender = this.senderMap[pushType];

View File

@@ -16,39 +16,7 @@ export class PushAdapter {
* Get an array of valid push types.
* @returns {Array} An array of valid push types
*/
getValidPushTypes() {
return this.validPushTypes;
}
/**g
* Classify the device token of installations based on its device type.
* @param {Object} installations An array of installations
* @param {Array} validPushTypes An array of valid push types(string)
* @returns {Object} A map whose key is device type and value is an array of device
*/
static classifyInstallation(installations, validPushTypes) {
// Init deviceTokenMap, create a empty array for each valid pushType
let deviceMap = {};
for (let validPushType of validPushTypes) {
deviceMap[validPushType] = [];
}
for (let installation of installations) {
// No deviceToken, ignore
if (!installation.deviceToken) {
continue;
}
let pushType = installation.deviceType;
if (deviceMap[pushType]) {
deviceMap[pushType].push({
deviceToken: installation.deviceToken,
appIdentifier: installation.appIdentifier
});
} else {
console.log('Unknown push type from installation %j', installation);
}
}
return deviceMap;
}
getValidPushTypes() {}
}
export default PushAdapter;

View File

@@ -0,0 +1,30 @@
/**g
* Classify the device token of installations based on its device type.
* @param {Object} installations An array of installations
* @param {Array} validPushTypes An array of valid push types(string)
* @returns {Object} A map whose key is device type and value is an array of device
*/
export function classifyInstallations(installations, validPushTypes) {
// Init deviceTokenMap, create a empty array for each valid pushType
let deviceMap = {};
for (let validPushType of validPushTypes) {
deviceMap[validPushType] = [];
}
for (let installation of installations) {
// No deviceToken, ignore
if (!installation.deviceToken) {
continue;
}
let pushType = installation.deviceType;
if (deviceMap[pushType]) {
deviceMap[pushType].push({
deviceToken: installation.deviceToken,
appIdentifier: installation.appIdentifier
});
} else {
console.log('Unknown push type from installation %j', installation);
}
}
return deviceMap;
}