baas.io Javascript SDK v0.9.0 src/push/push.js

Push

method
Baas.Push()

    Description

    A class to model a Baas Notification.
    Set the path in the options object.

    @constructor
    @param {object} options {client:client, data: {'key': 'value'}, path:'path'}

    Source

    Baas.Push = function(options) {
    
        if(options){
          this._client = options.client;
          this._data = options.data || {};
          this._data.type = "pushes";
        }
      }
    
      Baas.Push.prototype = new Baas.Entity();

    send

    method
    Baas.Push.prototype.send()

      Description

      Push Message Send

      @method fetch
      @public
      @param {object} options
      @param {function} callback(err, data, entity)

      Source

      Baas.Push.prototype.send = function(options, callback){
      
          var type = this.get('type');
          var method = 'POST';
      
          //update the entity
          var self = this;
      
          var data = {};
          var entityData = this.get();
      
          //options check
          if(typeof options !== 'object' || !options){
            callback(true, {'error_code':100 , 'error_description' : 'not type of options'})
            return;
          }
      
          var platform = options.platform;
          var reserve = options.reserve;
      
          var target = options.target;
          var to = options.to;
          var payload = options.payload;
      
          //options target check
          if(typeof target === 'string'){
            if(!(target === 'all' || target === 'user' || target === 'device' || target ==='tag')){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid target parameter'});
              return;
            }
          } else {
            callback(true, {'error_code':100 , 'error_description' : 'Bad request'});
            return;
          }
      
          //options.to check
          if(typeof to === 'string'){
            if(target === 'user' && !Baas.Utils.isUUID(to)){
              callback(true, {'error_code':100, 'error_description' : 'Invalid UUID string:' + to});
              return
            }
            if(target === 'device' && to === ''){
              callback(true, {'error_code':620, 'error_description' : 'Not existed devices'});
              return
            }
            if(target === 'tag' && to === ''){
              callback(true, {'error_code':620, 'error_description' : 'Push to is null'});
              return
            }
          } else {
            if(target !== 'all'){
              callback(true, {'error_code':100, 'error_description' : 'Bad request'});
              return
            }
          }
      
          //options.to check
          if(!payload){
            callback(true, {'error_code':100, 'error_description' : 'Bad request'});
            return;
          } else if(typeof payload === 'string' || typeof payload === 'number' || (typeof payload === 'object' && payload instanceof Array)){
            console.log("121212")
            callback(true, {'error_code':100, 'error_description' : 'Invalid payload.badge type'});
            return;
          }
      
          //options.payload.alert check
          if(!payload.alert || typeof payload.alert !== 'string'){
            callback(true, {'error_code':100, 'error_description' : 'Bad request'});
            return;
          }
      
          // badge check
          if(!payload.badge){
            if(payload.badge !== undefined){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid payload.badge parameter'})
              return;
            }
          } else {
            // badge is not number
            if(typeof payload.badge !== 'number'){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid payload.badge type'})
              return;
            }
          }
      
          // sound check
          if(!payload.sound){
            if(payload.sound !== undefined){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid payload.sound parameter'})
              return;
            }
          } else {
            if(typeof payload.sound !== 'string'){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid payload.sound type'})
              return;
            }
          }
      
          // platform check
          if(!platform){
            if(platform !== undefined){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid platform parameter'})
              return;
            }
          } else {
            if(typeof platform !== 'string'){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid platform type'})
              //return;
            }
            if(!(platform === 'I' || platform === 'G')){
              callback(true, {'error_code':620 , 'error_description' : 'Device platform invalid regxp'})
              return;
            }
          }
      
          // reserve check
          if(!reserve){
            if(reserve !== undefined){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid reserve parameter'})
              return;
            }
          } else {
            if(typeof reserve !== 'string'){
              callback(true, {'error_code':100 , 'error_description' : 'Invalid reserve type'})
              return;
            }
            if(reserve.length !== 12 || isNaN(reserve)){
              callback(true, {'error_code':620 , 'error_description' : 'Push reserve invalid regxp'})
              //return;
            }
          }
      
          for (var item in entityData) {
            if (item === 'metadata' || item === 'created' || item === 'modified' ||
                item === 'type' || item === 'activated' || item ==='uuid') { continue; }
            data[item] = entityData[item];
          }
      
          //remove system specific properties
          for (var item in options) {
            if(item === 'to' && target === 'all') {continue;}
            data[item] = options[item];
          }
      
          var options =  {
            method:method,
            endpoint:type,
            body:data
          };
      
          this._client.request(options, function(err, retdata){
            if(err) {
              if(self._client.logging) {
                console.log('error getting push');
              }
            } else {
              if (retdata.entities) {
                if (retdata.entities.length) {
                  var entity = retdata.entities[0];
                  self.set(entity);
                }
              }
            }
      
            if(typeof(callback) === 'function') {
              callback(err, retdata, self);
            }
          }) 
        }