|
| 1 | +"use strict"; |
| 2 | +// SNSAdapter |
| 3 | +// |
| 4 | +// Uses SNS for push notification |
| 5 | +import PushAdapter from './PushAdapter'; |
| 6 | + |
| 7 | +const Parse = require('parse/node').Parse; |
| 8 | +const GCM = require('../../GCM'); |
| 9 | +const cryptoUtils = require('../../cryptoUtils'); |
| 10 | + |
| 11 | +const AWS = require('aws-sdk'); |
| 12 | +const path = require('path'); |
| 13 | +const rest = require('../../rest'); |
| 14 | + |
| 15 | +var DEFAULT_REGION = "us-east-1"; |
| 16 | +import { classifyInstallations } from './PushAdapterUtils'; |
| 17 | + |
| 18 | +export class SNSPushAdapter extends PushAdapter { |
| 19 | + |
| 20 | + // Publish to an SNS endpoint |
| 21 | + // Providing AWS access and secret keys is mandatory |
| 22 | + // Region will use sane defaults if omitted |
| 23 | + constructor(pushConfig = {}) { |
| 24 | + super(pushConfig); |
| 25 | + this.validPushTypes = ['ios', 'android']; |
| 26 | + this.availablePushTypes = []; |
| 27 | + this.arnMap = {}; |
| 28 | + this.senderMap = {}; |
| 29 | + |
| 30 | + if (!pushConfig.accessKey || !pushConfig.secretKey) { |
| 31 | + throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, |
| 32 | + 'Need to provide AWS keys'); |
| 33 | + } |
| 34 | + |
| 35 | + if (pushConfig.pushTypes) { |
| 36 | + let pushTypes = Object.keys(pushConfig.pushTypes); |
| 37 | + for (let pushType of pushTypes) { |
| 38 | + if (this.validPushTypes.indexOf(pushType) < 0) { |
| 39 | + throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, |
| 40 | + 'Push to ' + pushTypes + ' is not supported'); |
| 41 | + } |
| 42 | + this.availablePushTypes.push(pushType); |
| 43 | + switch (pushType) { |
| 44 | + case 'ios': |
| 45 | + this.senderMap[pushType] = this.sendToAPNS.bind(this); |
| 46 | + this.arnMap[pushType] = pushConfig.pushTypes[pushType]; |
| 47 | + break; |
| 48 | + case 'android': |
| 49 | + this.senderMap[pushType] = this.sendToGCM.bind(this); |
| 50 | + this.arnMap[pushType] = pushConfig.pushTypes[pushType]; |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + AWS.config.update({ |
| 57 | + accessKeyId: pushConfig.accessKey, |
| 58 | + secretAccessKey: pushConfig.secretKey, |
| 59 | + region: pushConfig.region || DEFAULT_REGION |
| 60 | + }); |
| 61 | + |
| 62 | + // Instantiate after config is setup. |
| 63 | + this.sns = new AWS.SNS(); |
| 64 | + } |
| 65 | + |
| 66 | + getValidPushTypes() { |
| 67 | + return this.availablePushTypes; |
| 68 | + } |
| 69 | + |
| 70 | + static classifyInstallations(installations, validTypes) { |
| 71 | + return classifyInstallations(installations, validTypes) |
| 72 | + } |
| 73 | + |
| 74 | + //Generate proper json for APNS message |
| 75 | + generateiOSPayload(data) { |
| 76 | + return { |
| 77 | + 'APNS': JSON.stringify(data) |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + // Generate proper json for GCM message |
| 82 | + generateAndroidPayload(data) { |
| 83 | + var payload = GCM.generateGCMPayload(data); |
| 84 | + |
| 85 | + // SNS is verify sensitive to the body being JSON stringified but not GCM key. |
| 86 | + return { |
| 87 | + 'GCM': JSON.stringify(payload) |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + sendToAPNS(data, devices) { |
| 92 | + var payload = this.generateiOSPayload(data); |
| 93 | + |
| 94 | + return this.sendToSNS(payload, devices, 'ios'); |
| 95 | + } |
| 96 | + |
| 97 | + sendToGCM(data, devices) { |
| 98 | + var payload = this.generateAndroidPayload(data); |
| 99 | + return this.sendToSNS(payload, devices, 'android'); |
| 100 | + } |
| 101 | + |
| 102 | + sendToSNS(payload, devices, pushType) { |
| 103 | + var promises = []; |
| 104 | + var exchangePromises = []; |
| 105 | + var self = this; |
| 106 | + |
| 107 | + // Exchange the device token for the Amazon resource ID |
| 108 | + for (let device of devices) { |
| 109 | + exchangePromises.push(this.exchangeTokenPromise(device, pushType)); |
| 110 | + } |
| 111 | + |
| 112 | + // Publish off to SNS! |
| 113 | + Parse.Promise.when(exchangePromises).then(function(arns) { |
| 114 | + for (let arn of arns) { |
| 115 | + promises.push(self.sendSNSPayload(arn, payload)); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + return promises; |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + /** |
| 124 | + * Request a Amazon Resource Identifier if one is not set. |
| 125 | + */ |
| 126 | + getPlatformArn(device, pushType, callback) { |
| 127 | + var params = { |
| 128 | + PlatformApplicationArn: this.arnMap[pushType], |
| 129 | + Token: device.deviceToken |
| 130 | + }; |
| 131 | + |
| 132 | + this.sns.createPlatformEndpoint(params, callback); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Exchange the device token for an ARN |
| 137 | + */ |
| 138 | + exchangeTokenPromise(device, pushType) { |
| 139 | + return new Parse.Promise((resolve, reject) => { |
| 140 | + this.getPlatformArn(device, pushType, function (err, data) { |
| 141 | + if (data.EndpointArn) { |
| 142 | + console.log("Generating ARN " + data.EndpointArn); |
| 143 | + resolve(data.EndpointArn); |
| 144 | + } else { |
| 145 | + console.log(err); |
| 146 | + reject(err); |
| 147 | + } |
| 148 | + }); |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + // Bulk publishing is not yet supported on Amazon SNS. |
| 153 | + sendSNSPayload(arn, payload) { |
| 154 | + |
| 155 | + var object = { |
| 156 | + Message: JSON.stringify(payload), |
| 157 | + MessageStructure: 'json', |
| 158 | + TargetArn: arn |
| 159 | + }; |
| 160 | + |
| 161 | + return new Parse.Promise((resolve, reject) => { |
| 162 | + this.sns.publish(object, function (err, data) { |
| 163 | + if (err != null) { |
| 164 | + console.log("Error sending push " + err); |
| 165 | + return reject(err); |
| 166 | + } |
| 167 | + console.log("Sent push to " + object.TargetArn); |
| 168 | + resolve(object); |
| 169 | + }); |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + // For a given config object, endpoint and payload, publish via SNS |
| 174 | + // Returns a promise containing the SNS object publish response |
| 175 | + send(data, installations) { |
| 176 | + let sendPromises = []; |
| 177 | + let deviceMap = classifyInstallations(installations, this.availablePushTypes); |
| 178 | + |
| 179 | + for (let pushType in deviceMap) { |
| 180 | + |
| 181 | + let sender = this.senderMap[pushType]; |
| 182 | + |
| 183 | + if (!sender) { |
| 184 | + console.log('Can not find sender for push type %s, %j', pushType, data); |
| 185 | + continue; |
| 186 | + } |
| 187 | + |
| 188 | + let devices = deviceMap[pushType]; |
| 189 | + sendPromises.push(sender(data, devices)); |
| 190 | + } |
| 191 | + return Parse.Promise.when(sendPromises); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +export default SNSPushAdapter; |
| 196 | +module.exports = SNSPushAdapter; |
0 commit comments