Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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.

— asked March 30th 2010 by alexandre duquenoy
  • devicetoken
  • notification
  • notifications
  • problem
  • push
0 Comments

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!

    — answered March 20th 2013 by Joseph Sachs
    permalink
    1 Comment
    • crazy stuff… one would assume that debug mode is for debugging…
      well… at least it works!

      just making things clear.
      to get the token, install the app. do not run debug mode from titanium.

      — commented April 30th 2013 by Danny Dechtiar
  • 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).

    — answered March 30th 2010 by Nick Haffie-Emslie
    permalink
    0 Comments
  • 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

    — answered March 30th 2010 by alexandre duquenoy
    permalink
    2 Comments
    • I got the same issue. Any chance to get some solution here or should i repost the question?
      I got asked "Allow push?" answered "yes", the app got registered in the settings menu under "notifications" and everything seems to be working. But the e.deviceToken and the Titanium.Network.remoteDeviceUUID stay null.

      — commented July 29th 2011 by Krum Lozev
    • U will get device token for this code it is working here

      — commented September 27th 2012 by sriram pavani
  • To retrieve your device token after it's registered use this:

    var myID = Titanium.Network.remoteDeviceUUID;

    — answered April 8th 2010 by David Brown
    permalink
    1 Comment
    • Hello David

      it returns null but i've already allowed my app for APN and i need to retrieve the token as i didn't saved that .

      — commented October 23rd 2010 by Raquibul Islam
  • 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

    — answered March 31st 2010 by alexandre duquenoy
    permalink
    0 Comments
  • 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

    — answered April 1st 2010 by Paul Burrowes
    permalink
    1 Comment
    • Hi,
      got working?

      <?php
      $deviceToken = 'XXXXXXXXX'; // masked for security reason
      // Passphrase for the private key
      $pass = 'XXXXXX';
      
      // Get the parameters from http get or from command line
      $message = $_GET['message'] or $message = $argv[1] or $message = 'Test Message';
      $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
      $sound = $_GET['sound'] or $sound = $argv[3];
      
      // Construct the notification payload
      $body = array();
      $body['aps'] = array('alert' => $message);
      if ($badge)
          $body['aps']['badge'] = $badge;
      if ($sound)
          $body['aps']['sound'] = $sound;
      
      
      /* End of Configurable Items */
      $ctx = stream_context_create();
      stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev.pem');
      // assume the private key passphase was removed.
      stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
      
      // for production change the server to ssl://gateway.push.apple.com:219
      $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
      
      if (!$fp) {
          print "Failed to connect $err $errstr\n";
          return;
      }
      else {
          print "Connection OK\n";
      }
      
      $payload = json_encode($body);
      $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
      print "sending message :" . $payload . "\n";
      fwrite($fp, $msg);
      fclose($fp);
      ?>
      

      — commented July 13th 2010 by Christian Brauwers
  • @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.

    — answered April 16th 2010 by Nick Haffie-Emslie
    permalink
    0 Comments
  • Having to register with an APNs to retrieve the device UUID is just.. duh.

    — answered May 23rd 2010 by Pedro B.
    permalink
    0 Comments
  • 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;

    — answered October 13th 2010 by Sebastian
    permalink
    0 Comments
  • Titanium.Network.remoteDeviceUUID

    returning null

    — answered October 23rd 2010 by Raquibul Islam
    permalink
    0 Comments
  • 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

    — answered November 8th 2010 by Michele Nasoni
    permalink
    0 Comments
  • 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

    — answered September 27th 2012 by sriram pavani
    permalink
    0 Comments
  • 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 the error or callback function get fired…

    Anybody has any information on pointing me in the right direction…?

    — answered March 20th 2013 by Joseph Sachs
    permalink
    2 Comments
    • Note, I've recreated the Push Certificate, and uploaded it to ACS & created a New Provisioning file with the App Id with Push Notification… and success or any other callback from registerForPushNotifications…. I have no idea left what to do?

      — commented March 20th 2013 by Joseph Sachs
    • Make sure you don't Debug the App on thje device, and that you Run the App on the device… shold work.

      — commented March 20th 2013 by Robert Massa
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.