ParsePushAdapter is a package
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
// PushAdapter, it uses GCM for android push and APNS
|
||||
// for ios push.
|
||||
|
||||
import { classifyInstallations } from './PushAdapterUtils';
|
||||
import { utils } from 'parse-server-push-adapter';
|
||||
import ParsePushAdapter from 'parse-server-push-adapter';
|
||||
|
||||
const Parse = require('parse/node').Parse;
|
||||
var deepcopy = require('deepcopy');
|
||||
@@ -30,7 +31,7 @@ export class OneSignalPushAdapter extends PushAdapter {
|
||||
}
|
||||
|
||||
send(data, installations) {
|
||||
let deviceMap = classifyInstallations(installations, this.validPushTypes);
|
||||
let deviceMap = utils.classifyInstallations(installations, this.validPushTypes);
|
||||
|
||||
let sendPromises = [];
|
||||
for (let pushType in deviceMap) {
|
||||
@@ -49,7 +50,7 @@ export class OneSignalPushAdapter extends PushAdapter {
|
||||
}
|
||||
|
||||
static classifyInstallations(installations, validTypes) {
|
||||
return classifyInstallations(installations, validTypes)
|
||||
return utils.classifyInstallations(installations, validTypes)
|
||||
}
|
||||
|
||||
getValidPushTypes() {
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
"use strict";
|
||||
// ParsePushAdapter is the default implementation of
|
||||
// PushAdapter, it uses GCM for android push and APNS
|
||||
// for ios push.
|
||||
|
||||
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 {
|
||||
|
||||
supportsPushTracking = true;
|
||||
|
||||
constructor(pushConfig = {}) {
|
||||
super(pushConfig);
|
||||
this.validPushTypes = ['ios', 'android'];
|
||||
this.senderMap = {};
|
||||
// used in PushController for Dashboard Features
|
||||
this.feature = {
|
||||
immediatePush: true
|
||||
};
|
||||
let pushTypes = Object.keys(pushConfig);
|
||||
|
||||
for (let pushType of pushTypes) {
|
||||
if (this.validPushTypes.indexOf(pushType) < 0) {
|
||||
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
||||
'Push to ' + pushTypes + ' is not supported');
|
||||
}
|
||||
switch (pushType) {
|
||||
case 'ios':
|
||||
this.senderMap[pushType] = new APNS(pushConfig[pushType]);
|
||||
break;
|
||||
case 'android':
|
||||
this.senderMap[pushType] = new GCM(pushConfig[pushType]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getValidPushTypes() {
|
||||
return this.validPushTypes;
|
||||
}
|
||||
|
||||
static classifyInstallations(installations, validTypes) {
|
||||
return classifyInstallations(installations, validTypes)
|
||||
}
|
||||
|
||||
send(data, installations) {
|
||||
let deviceMap = classifyInstallations(installations, this.validPushTypes);
|
||||
let sendPromises = [];
|
||||
for (let pushType in deviceMap) {
|
||||
let sender = this.senderMap[pushType];
|
||||
if (!sender) {
|
||||
sendPromises.push(Promise.resolve({
|
||||
transmitted: false,
|
||||
response: {'error': `Can not find sender for push type ${pushType}, ${data}`}
|
||||
}))
|
||||
} else {
|
||||
let devices = deviceMap[pushType];
|
||||
sendPromises.push(sender.send(data, devices));
|
||||
}
|
||||
}
|
||||
return Parse.Promise.when(sendPromises);
|
||||
}
|
||||
}
|
||||
|
||||
export default ParsePushAdapter;
|
||||
module.exports = ParsePushAdapter;
|
||||
@@ -1,27 +0,0 @@
|
||||
/**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
|
||||
});
|
||||
}
|
||||
}
|
||||
return deviceMap;
|
||||
}
|
||||
Reference in New Issue
Block a user