From bc03e17610647cba1cb613b76391ab91ae5eccd1 Mon Sep 17 00:00:00 2001 From: Douglas Hennrich Date: Tue, 5 Jan 2016 11:10:40 -0200 Subject: [PATCH] Add cancelRegister & getObject --- index.js | 78 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 1178794..de68b09 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ if (ANDROID) { function TiPush(e) { this.backendUrl = e.backendUrl; - this.token = ''; + this.token = Ti.App.Properties.getString('TiPushNotification_Token') || ''; this.os = (ANDROID) ? 'Android' : 'iOS'; } @@ -40,24 +40,27 @@ TiPush.prototype.registerDevice = function(_prams) { } that.token = token; - var uploadParams = {}; + if(!Ti.App.Properties.getString('TiPushNotification_Token')){ + var uploadParams = {}; - for (var key in that) { - if (['backendUrl', 'getToken', 'registerDevice'].indexOf(key) === -1) { - uploadParams[key] = that[key]; + for (var key in that) { + if (['backendUrl', 'getToken', 'registerDevice', 'cancelRegister', 'getObject'].indexOf(key) === -1) { + uploadParams[key] = that[key]; + } } - } - var xhr = Ti.Network.createHTTPClient({ - onload: function() { - Ti.API.debug("[TiPush] Token sent to our backend"); - }, - onerror: function() { - Ti.API.error("[TiPush] Can't send tokend to our backend"); - } - }); - xhr.open("POST", that.backendUrl); - xhr.send(uploadParams); + var xhr = Ti.Network.createHTTPClient({ + onload: function() { + Ti.API.debug("[TiPush] Token sent to our backend"); + Ti.App.Properties.setString('TiPushNotification_Token', that.token); + }, + onerror: function() { + Ti.API.error("[TiPush] Can't send tokend to our backend"); + } + }); + xhr.open("POST", that.backendUrl); + xhr.send(uploadParams); + } } function deviceTokenError(e) { @@ -148,6 +151,49 @@ TiPush.prototype.getToken = function() { return this.token; }; +TiPush.prototype.cancelRegister = function () { + var that = this, + params = {}; + + for (var key in that) { + if (['backendUrl', 'getToken', 'registerDevice', 'cancelRegister'].indexOf(key) === -1) { + params[key] = that[key]; + } + } + + var xhr = Ti.Network.createHTTPClient({ + onload: function() { + Ti.API.debug("[TiPush] Token deleted from our backend"); + Ti.App.Properties.removeProperty('TiPushNotification_Token'); + + for (var key in that) { + if (['backendUrl', 'getToken', 'registerDevice', 'cancelRegister', 'getObject', 'os'].indexOf(key) === -1) { + that[key] = null; + } + } + that.token = ''; + }, + onerror: function() { + Ti.API.error("[TiPush] Can't delete token from our backend"); + } + }); + xhr.open("DELETE", that.backendUrl); + xhr.send(params); +}; + +TiPush.prototype.getObject = function () { + var obj = {} + , that = this; + + for (var key in that) { + if (['backendUrl', 'getToken', 'registerDevice', 'cancelRegister', 'getObject'].indexOf(key) === -1) { + obj[key] = that[key]; + } + } + + return obj; +}; + exports.init = function(param) { return new TiPush(param); };