UNPKG

flaglib

Version:

Ignition event 15 added.

121 lines (93 loc) 2.81 kB
//var SNS = require('sns-mobile'); var SNS = require('sns-mobile-ellii'); //var logger = require('../utility/logger'); module.exports = Push; function Push(params) { console.log("ActiveGroupList const called: "); if (typeof params === "undefined") { params = {}; } this.SNS_KEY_ID = null; this.SNS_ACCESS_KEY = null; this.ANDROID_ARN = null; this.IOS_ARN = null; this.PLATFORM = null; this.sandbox = false; if (typeof params.aws_access_key_id !== 'undefined') { this.SNS_KEY_ID = params.aws_access_key_id; } if (typeof params.aws_secret_access_key !== 'undefined') { this.SNS_ACCESS_KEY = params.aws_secret_access_key; } if (typeof params.android_arn !== 'undefined') { this.ANDROID_ARN = params.android_arn; } if (typeof params.ios_arn !== 'undefined') { this.IOS_ARN = params.ios_arn; } if (typeof params.platform !== 'undefined') { this.PLATFORM = params.platform; } if (typeof params.sandbox !== 'undefined') { this.sandbox = params.sandbox; } var platform = null; var arn = null; if (this.PLATFORM == "GCM"){ platform = SNS.SUPPORTED_PLATFORMS.ANDROID; arn = this.ANDROID_ARN; }else if (this.PLATFORM == "APNS"){ platform = SNS.SUPPORTED_PLATFORMS.IOS; arn = this.IOS_ARN; } //to avoid crash in sns-mobile-ellii if (platform == null) { platform = SNS.SUPPORTED_PLATFORMS.ANDROID; arn = this.ANDROID_ARN; } this.pushObj = new SNS({ platform : platform, // If using iOS change uncomment the line below // and comment out the 'android' one above // platform: 'ios', region : 'ap-southeast-1', apiVersion : '2010-03-31', accessKeyId : this.SNS_KEY_ID, secretAccessKey : this.SNS_ACCESS_KEY, platformApplicationArn : arn, sandbox:this.sandbox }); // Handle user added events this.pushObj.on('userAdded', function(endpointArn, deviceId) { // add user details to the DB with endpoint ARN }); } Push.prototype.addUser = function(toke, dataObject, callback) { this.pushObj.addUser(token, dataObject, function(err, endpointArn){ callback(err, endpointArn); }); } Push.prototype.sendMessage = function(userArn, userMessage, callback) { this.pushObj.sendMessage(userArn, userMessage, function(err, messageId) { callback(err, messageId); } ); } /* * if user un install tha app and again install, there are high chances that, the endpoint arn in amazon Db * disabled. Which need to enable again. */ Push.prototype.enableEndPoint = function(userArn, callback) { console.log('enableEndPoint - 1: '+userArn); var attributes = { Enabled: 'true' }; this.pushObj.setAttributes(userArn,attributes, function(err, res) { console.log('enableEndPoint - 2: '+res); console.log('enableEndPoint - 3: '+err); callback(err, res); } ); }