-
-
Save nicolasembleton/5175325 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Parse = require('/Parse') | |
var isInForeground = true; | |
Ti.App.addEventListener('pause', function(){ | |
isInForeground = false; | |
}); | |
Ti.App.addEventListener('resumed', function(){ | |
isInForeground = true; | |
}); | |
Ti.Network.registerForPushNotifications({ | |
types: [ | |
Ti.Network.NOTIFICATION_TYPE_BADGE, | |
Ti.Network.NOTIFICATION_TYPE_ALERT, | |
Ti.Network.NOTIFICATION_TYPE_SOUND | |
], success:function(e) { | |
Parse.register({ | |
deviceType: 'ios', | |
deviceToken: e.deviceToken, | |
channels: [''] // array of channels, make sure to add the default broadcast channel | |
}); | |
}, error:function(e) { | |
Ti.API.log(e.error); | |
}, callback:function(e) { | |
Ti.API.log(JSON.stringify(e.data)); | |
if (isInForeground && e.data && e.data.alert) { | |
var alertDialog = Ti.UI.createAlertDialog({ | |
title: L('Alert', 'Alert'), | |
message: e.data.alert | |
}); | |
alertDialog.show(); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var baseUrl = 'https://api.parse.com/1', | |
appId = 'XXXXXXXXXXXXXXX', | |
apiKey = 'XXXXXXXXXXXXXX'; // make sure to use the REST API Key | |
var _register = function(params, lambda, lambdaerror) { | |
var method = 'POST', | |
url = baseUrl + '/installations', | |
payload = (params) ? JSON.stringify(params) : ''; | |
_helper(url, method, payload, function(data, status) { | |
Ti.API.log('completed registration: ' + JSON.stringify(status)); | |
lambda(data, status); | |
}, function(xhr, error) { | |
Ti.API.log('error registration: ' + JSON.stringify(error)); | |
lambdaerror(error); | |
}); | |
}; | |
var _helper = function(url, method, params, lambda, lambdaerror) { | |
var xhr = Ti.Network.createHTTPClient(); | |
xhr.setTimeout(15000); | |
xhr.onerror = function(e) { | |
lambdaerror(this, e); | |
}; | |
xhr.onload = function() { | |
lambda(this.responseText, this.status); | |
}; | |
params = params.replace(/\./g, '_'); | |
xhr.open(method, url); | |
xhr.setRequestHeader('X-Parse-Application-Id', appId); | |
xhr.setRequestHeader('X-Parse-REST-API-Key', apiKey); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.send(params); | |
}; | |
exports.register = _register; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment