Skip to content

Instantly share code, notes, and snippets.

@iRonin
Forked from yagitoshiro/push_notifications.js
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iRonin/6b1f5ffd251d4dc7e92d to your computer and use it in GitHub Desktop.
Save iRonin/6b1f5ffd251d4dc7e92d to your computer and use it in GitHub Desktop.
//////////////////////push_notifications.js///////////////////////
var apns = function(){
var pref = require('preferences').preferences;
Titanium.Network.registerForPushNotifications({
types: [
Titanium.Network.NOTIFICATION_TYPE_BADGE,
Titanium.Network.NOTIFICATION_TYPE_ALERT
],
success:function(e)
{
var deviceToken = e.deviceToken;
Ti.API.info("Push notification device token is: "+deviceToken);
Ti.API.info("Push notification types: "+Titanium.Network.remoteNotificationTypes);
Ti.API.info("Push notification enabled: "+Titanium.Network.remoteNotificationsEnabled);
var http = Ti.Network.createHTTPClient();
http.onload = function(){
// do nothing.
};
http.open('POST', 'https://your-server/');
http.send({deviceToken: e.deviceToken});
},
error:function(e)
{
Ti.API.info("Error during registration: "+e.error);
},
callback:function(e)
{
// called when a push notification is received.
Titanium.Media.vibrate();
var data = JSON.parse(e.data);
var badge = data.badge;
if(badge > 0){
Titanium.UI.iPhone.appBadge = badge;
}
var message = data.message;
if(message != ''){
var my_alert = Ti.UI.createAlertDialog({title:'', message:message});
my_alert.show();
}
}
});
};
exports = {apns:apns};
//////////////////////app.js///////////////////////
var platform = Ti.Platform.name;
if(platform != 'android'){
var apns = require('push_notifications');
apns.apns();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment