Push notifications deviceToken
Hi there,
I would like to know how to get the deviceToken of my IPhone using Titanium.
I use this code below in my app :
But i can't send push notifications without the deviceToken.
var win = Titanium.UI.currentWindow;
var label = Ti.UI.createLabel({
text:'Attempting to register with Apple for Push Notifications...',
textAlign:'center',
width:'auto'
});
win.add(label);
// register for push notifications
Titanium.Network.registerForPushNotifications({
types: [
Titanium.Network.NOTIFICATION_TYPE_BADGE,
Titanium.Network.NOTIFICATION_TYPE_ALERT,
Titanium.Network.NOTIFICATION_TYPE_SOUND
],
success:function(e)
{
alert('LOL :'+ e.deviceToken);
var deviceToken = e.deviceToken;
label.text = "Device registered. Device token: nn"+deviceToken;
alert("Push notification device token is:
"+deviceToken+"Push notification types:
"+Titanium.Network.remoteNotificationTypes+"Push notification
enabled: "+Titanium.Network.remoteNotificationsEnabled);
},
error:function(e)
{
label.text = "Error during registration: "+e.error;
},
callback:function(e)
{
// called when a push notification is received.
alert("Received a push notificationnnPayload:nn"+JSON.stringify(e.data));
}
});
Thanks.
13 Answers
-
Someone should also Mention, that Device Token Cannot be fetched if on Device in Debug mode, must be in Run Mode on the device!
-
What happens when you run the above code? As far as I can see you're doing it right:
Ti.Network.registerForPushNotifications({ [ ... ], success:function(e){ e.deviceToken // this is your device token } })
Keep in mind too that push only works on a physical device, not the simulator. You'll need to test it on your iPhone/iPod touch to even get a device token (let alone successfully push).
-
I got the alert "push notifications, allow or deny",
then I allow them.Yes I test it on my iPhone, but there's no device token anywhere
thanks
-
To retrieve your device token after it's registered use this:
var myID = Titanium.Network.remoteDeviceUUID;
-
I have the alert which ask me if I want to allow push notifications on my app.
then I select "allow".But I can't display or get my deviceToken,can I ?
And yes I try my code on me iphone.
Thanks
-
Can someone give me a quick rundown of what to do to get the push message to the phone once you have a deviceToken? Ideally I would like to run from a webserver a php function like:
function sendAPN($badge, $alert, $sound, $deviceToken) {
…What goes in here?
}I have tried to look online, but there doesn't seem to be a simple guide…!
Thanks
-
@Paul Burrowes:
Have a look at http://urbanairship.com
It's free up to 250,000 messages per month, and is super easy to use.
-
Having to register with an APNs to retrieve the device UUID is just.. duh.
-
Warning!
Since Ti 1.4.1 e.deviceToken returns a TiBlob and not a String anymore! Every try that I made (.text, .toString()) failed and so the registration at Urban Airship failed, too!
Solution: use this instead:
var deviceToken=Ti.Network.remoteDeviceUUID; -
Titanium.Network.remoteDeviceUUID
returning null
-
Hi
I have the same problem and I did not find a solution
everything seems ok but I am not able to receive the message on the phone -
Just check that the success is calling or not keep an alert theree with devicetoken and test it on the real device with titaniium 2.0. hiher versions and xcode 4.2 and higher
-
Anybody managed to get the deviceToken working? It's been 2 days I'm trying to solve this…
app.js
if(!Ti.Platform.Android) { // For iOS Ti.API.info('Register for PushNotification (on iOS)'); Titanium.Network.registerForPushNotifications({ types: [ Titanium.Network.NOTIFICATION_TYPE_BADGE, Titanium.Network.NOTIFICATION_TYPE_ALERT, Titanium.Network.NOTIFICATION_TYPE_SOUND ], success:function(e) { var deviceToken = e.deviceToken; Ti.App.Properties.setString('token',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); }, 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)); } }); } else { // For Android }
I'm running it on the device… I see the Push notifications in My Settings.
The Console shows:
Register for PushNotification (on iOS)
But Never the Push Notification device token and two other API.info trace outs… Neither do any of theerror
or callback function get fired…Anybody has any information on pointing me in the right direction…?