Skip to content

Instantly share code, notes, and snippets.

@yagitoshiro
Last active August 13, 2016 17:29
Show Gist options
  • Save yagitoshiro/1208144 to your computer and use it in GitHub Desktop.
Save yagitoshiro/1208144 to your computer and use it in GitHub Desktop.
apple push notification sample (Titanium Mobile)
//////////////////////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