Skip to content

Instantly share code, notes, and snippets.

@mattberg
Created September 21, 2012 14:24
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mattberg/3761738 to your computer and use it in GitHub Desktop.
Save mattberg/3761738 to your computer and use it in GitHub Desktop.
Simple Parse REST API module for Titanium
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();
}
}
});
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;
@dppooja
Copy link

dppooja commented Nov 28, 2014

How can use this with andorid?
I'm successfully able to register my device over parse.com but when I send messages from parse how can I receive that messages. I mean I need callback function to receive my push messages.

@mohnada
Copy link

mohnada commented Dec 10, 2014

Thank you so much
i was looking for it for long time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment