|
| 1 | +// ParsePushAdapter is the default implementation of |
| 2 | +// PushAdapter, it uses GCM for android push and APNS |
| 3 | +// for ios push. |
| 4 | + |
| 5 | +var Parse = require('parse/node').Parse, |
| 6 | + GCM = require('./GCM'), |
| 7 | + APNS = require('./APNS'); |
| 8 | + |
| 9 | +function ParsePushAdapter() { |
| 10 | + this.validPushTypes = ['ios', 'android']; |
| 11 | + this.senders = {}; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Register push senders |
| 16 | + * @param {Object} pushConfig The push configuration which is given when parse server is initialized |
| 17 | + */ |
| 18 | +ParsePushAdapter.prototype.registerPushSenders = function(pushConfig) { |
| 19 | + // Initialize senders |
| 20 | + for (var i = 0; i < this.validPushTypes.length; i++) { |
| 21 | + this.senders[this.validPushTypes[i]] = []; |
| 22 | + } |
| 23 | + |
| 24 | + pushConfig = pushConfig || {}; |
| 25 | + var pushTypes = Object.keys(pushConfig); |
| 26 | + for (var i = 0; i < pushTypes.length; i++) { |
| 27 | + var pushType = pushTypes[i]; |
| 28 | + if (this.validPushTypes.indexOf(pushType) < 0) { |
| 29 | + throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, |
| 30 | + 'Push to ' + pushTypes + ' is not supported'); |
| 31 | + } |
| 32 | + |
| 33 | + var typePushConfig = pushConfig[pushType]; |
| 34 | + var senderArgs = []; |
| 35 | + // Since for ios, there maybe multiple cert/key pairs, |
| 36 | + // typePushConfig can be an array. |
| 37 | + if (Array.isArray(typePushConfig)) { |
| 38 | + senderArgs = senderArgs.concat(typePushConfig); |
| 39 | + } else if (typeof typePushConfig === 'object') { |
| 40 | + senderArgs.push(typePushConfig); |
| 41 | + } else { |
| 42 | + throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, |
| 43 | + 'Push Configuration is invalid'); |
| 44 | + } |
| 45 | + for (var j = 0; j < senderArgs.length; j++) { |
| 46 | + var senderArg = senderArgs[j]; |
| 47 | + var sender; |
| 48 | + switch (pushType) { |
| 49 | + case 'ios': |
| 50 | + sender = new APNS(senderArg); |
| 51 | + break; |
| 52 | + case 'android': |
| 53 | + sender = new GCM(senderArg); |
| 54 | + break; |
| 55 | + } |
| 56 | + this.senders[pushType].push(sender); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Get an array of push senders based on the push type. |
| 63 | + * @param {String} The push type |
| 64 | + * @returns {Array|Undefined} An array of push senders |
| 65 | + */ |
| 66 | +ParsePushAdapter.prototype.getPushSenders = function(pushType) { |
| 67 | + if (!this.senders[pushType]) { |
| 68 | + console.log('No push sender for push type %s', pushType); |
| 69 | + return []; |
| 70 | + } |
| 71 | + return this.senders[pushType]; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Get an array of valid push types. |
| 76 | + * @returns {Array} An array of valid push types |
| 77 | + */ |
| 78 | +ParsePushAdapter.prototype.getValidPushTypes = function() { |
| 79 | + return this.validPushTypes; |
| 80 | +} |
| 81 | + |
| 82 | +ParsePushAdapter.prototype.send = function(data, installations) { |
| 83 | + var deviceMap = classifyInstallation(installations, this.validPushTypes); |
| 84 | + var sendPromises = []; |
| 85 | + for (var pushType in deviceMap) { |
| 86 | + var senders = this.getPushSenders(pushType); |
| 87 | + // Since ios have dev/prod cert, a push type may have multiple senders |
| 88 | + for (var i = 0; i < senders.length; i++) { |
| 89 | + var sender = senders[i]; |
| 90 | + var devices = deviceMap[pushType]; |
| 91 | + if (!sender || devices.length == 0) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + // For android, we can only have 1000 recepients per send |
| 95 | + var chunkDevices = sliceDevices(pushType, devices, GCM.GCMRegistrationTokensMax); |
| 96 | + for (var j = 0; j < chunkDevices.length; j++) { |
| 97 | + sendPromises.push(sender.send(data, chunkDevices[j])); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return Parse.Promise.when(sendPromises); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Classify the device token of installations based on its device type. |
| 106 | + * @param {Object} installations An array of installations |
| 107 | + * @param {Array} validPushTypes An array of valid push types(string) |
| 108 | + * @returns {Object} A map whose key is device type and value is an array of device |
| 109 | + */ |
| 110 | +function classifyInstallation(installations, validPushTypes) { |
| 111 | + // Init deviceTokenMap, create a empty array for each valid pushType |
| 112 | + var deviceMap = {}; |
| 113 | + for (var i = 0; i < validPushTypes.length; i++) { |
| 114 | + deviceMap[validPushTypes[i]] = []; |
| 115 | + } |
| 116 | + for (var i = 0; i < installations.length; i++) { |
| 117 | + var installation = installations[i]; |
| 118 | + // No deviceToken, ignore |
| 119 | + if (!installation.deviceToken) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + var pushType = installation.deviceType; |
| 123 | + if (deviceMap[pushType]) { |
| 124 | + deviceMap[pushType].push({ |
| 125 | + deviceToken: installation.deviceToken |
| 126 | + }); |
| 127 | + } else { |
| 128 | + console.log('Unknown push type from installation %j', installation); |
| 129 | + } |
| 130 | + } |
| 131 | + return deviceMap; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Slice a list of devices to several list of devices with fixed chunk size. |
| 136 | + * @param {String} pushType The push type of the given device tokens |
| 137 | + * @param {Array} devices An array of devices |
| 138 | + * @param {Number} chunkSize The size of the a chunk |
| 139 | + * @returns {Array} An array which contaisn several arries of devices with fixed chunk size |
| 140 | + */ |
| 141 | +function sliceDevices(pushType, devices, chunkSize) { |
| 142 | + if (pushType !== 'android') { |
| 143 | + return [devices]; |
| 144 | + } |
| 145 | + var chunkDevices = []; |
| 146 | + while (devices.length > 0) { |
| 147 | + chunkDevices.push(devices.splice(0, chunkSize)); |
| 148 | + } |
| 149 | + return chunkDevices; |
| 150 | +} |
| 151 | + |
| 152 | +if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') { |
| 153 | + ParsePushAdapter.classifyInstallation = classifyInstallation; |
| 154 | + ParsePushAdapter.sliceDevices = sliceDevices; |
| 155 | +} |
| 156 | +module.exports = ParsePushAdapter; |
0 commit comments