Created
March 19, 2013 16:37
-
-
Save opolyo01/5197701 to your computer and use it in GitHub Desktop.
Push Notifications with Titanium Cloud
This file contains 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
getDeviceToken(); | |
function getDeviceToken() { | |
trace("REGISTERING LOCAL PUSH"); | |
Titanium.Network.registerForPushNotifications({ | |
types : [Titanium.Network.NOTIFICATION_TYPE_BADGE, Titanium.Network.NOTIFICATION_TYPE_ALERT, Titanium.Network.NOTIFICATION_TYPE_SOUND], | |
success : function(e) { | |
user_device_token = e.deviceToken; | |
Ti.App.Properties.setString("device_token", user_device_token); | |
alert("Device token received " + user_device_token); | |
login(subscribeToServerPush); | |
}, | |
error : function(e) { | |
alert("Error during registration: " + e.error); | |
}, | |
callback : function(e) { | |
// called when a push notification is received. | |
alert("Received a push notification\n\nPayload:\n\n" + JSON.stringify(e.data)); | |
} | |
}); | |
} | |
function login(cb) { | |
var params = { | |
"login": 'test@yahoo.com', | |
"password": 'test' | |
}; | |
Cloud.Users.login(params, function(e) { | |
if (e.success) { | |
var user = e.users[0]; | |
alert('Success:\\n' + 'id: ' + user.id + '\\n' + 'first name: ' + user.first_name + '\\n' + 'last name: ' + user.last_name); | |
cb.call(this); | |
} else { | |
alert('Error:\\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
} | |
function subscribeToServerPush() { | |
Cloud.PushNotifications.subscribe({ | |
channel : 'friend_request', | |
type : 'ios', | |
device_token : user_device_token | |
}, function(e) { | |
if (e.success) { | |
alert('Success' + ((e.error && e.message) || JSON.stringify(e))); | |
} else { | |
alert('Error:\\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry I'm also struggling with this, you think it fails platform?
Do not give me access to the cloud.
Your code is to send Push Notifications from a real web server?