Skip to content

Instantly share code, notes, and snippets.

@rborn
Last active August 29, 2015 14:18
Show Gist options
  • Save rborn/e158be8d5c944c8744b1 to your computer and use it in GitHub Desktop.
Save rborn/e158be8d5c944c8744b1 to your computer and use it in GitHub Desktop.
var Alloy = require('alloy');
var isIOS8Plus = (OS_IOS && parseInt(Ti.Platform.version.split(".")[0]) >= 8);
var Parse;
if (OS_ANDROID) {
Parse = require('eu.rebelcorp.parse');
}
if (OS_IOS) {
Parse = require('parse');
}
function ParsePush() {
}
/**
* _args.successCallback, _args.errorCallback, _args.pushCallback
*/
ParsePush.prototype.Parse = Parse;
ParsePush.prototype.registerForPush = function(_args) {
if (OS_ANDROID) {
var Parse_AppId = Ti.App.Properties.getString('Parse_AppId');
var Parse_ClientKey = Ti.App.Properties.getString('Parse_ClientKey');
Parse.start(Parse_AppId, Parse_ClientKey);
Parse.enablePush();
} else if (OS_IOS) {
Ti.App.Properties.removeProperty('parseToken');
Ti.App.Properties.removeProperty('parseInstallation');
Ti.App.Properties.removeProperty('parseChannels');
if (isIOS8Plus) {
Ti.App.iOS.addEventListener('usernotificationsettings', e = function() {
Ti.App.iOS.removeEventListener('usernotificationsettings', e);
Ti.Network.registerForPushNotifications({
success: function(e) {
Ti.App.Properties.setString('parseToken', e.deviceToken);
var params = {
deviceType: 'ios',
deviceToken: e.deviceToken,
channels: ['ios', 'league'],
appVersion: Ti.App.version,
appIdentifier: Ti.App.id,
appName: Ti.App.name,
};
Parse.registerPush({
data: params,
callback: function(success, responseJSON, status) {
if (success) {
var response = JSON.parse(responseJSON);
console.log(response);
Ti.App.Properties.setString('parseInstallation', response.objectId);
Ti.App.Properties.setList('parseChannels', response.channels);
if (_args.successCallback && typeof(_args.successCallback) == "function") {
_args.successCallback(responseJSON);
}
} else {
if (_args.errorCallback && typeof(_args.errorCallback) == "function") {
_args.errorCallback(success);
}
}
}
});
},
error: function(e) {
//alert(e.error);
console.log(e.error);
if (_args.errorCallback && typeof(_args.errorCallback) == "function") {
_args.errorCallback(e);
}
},
callback: function(e) {
// alert(e);
console.log(e);
if (_args.pushCallback && typeof(_args.pushCallback) == "function") {
_args.pushCallback(e);
}
}
});
});
Ti.App.iOS.registerUserNotificationSettings({
types: [
Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE,
Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND
]
});
} else {
Ti.Network.registerForPushNotifications({
// Specifies which notifications to receive
types: [
Ti.Network.NOTIFICATION_TYPE_BADGE,
Ti.Network.NOTIFICATION_TYPE_ALERT,
Ti.Network.NOTIFICATION_TYPE_SOUND
],
success: function(e) {
var params = {
deviceType: 'ios',
deviceToken: e.deviceToken,
channels: ['ios', 'league']
};
parsePush.registerPush(params, function(e) {
console.log(e);
_args.successCallback(e);
});
},
error: function(e) {
//alert(e.error);
console.log(e.error);
_args.errorCallback(e);
},
callback: function(e) {
alert(e);
console.log(e);
_args.pushCallback(e);
}
});
}
}
};
ParsePush.prototype.subscribeToChannel = function(channel, callback) {
if (OS_ANDROID) {
Parse.subscribeChannel(channel);
} else if (OS_IOS) {
}
};
ParsePush.prototype.unsubscribeToChannel = function(channel, callback) {
if (OS_ANDROID) {
Parse.unsubscribeChannel(channel);
} else if (OS_IOS) {
}
};
var parsePush = new ParsePush();
module.exports = parsePush;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment