Titanium Community Questions & Answer Archive

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

Retrieve the device token for APN

Hello Guys

I'm trying to implement the push notification , first time a dialog box shows up and i allowed , but didn't store the device token .

Now i cant send the notifications without the device token , how can i retrieve that ?

I've tried Titanium.Network.remoteDeviceUUID and it returns null

and Titanium.Platfrom.id returns the UUID .

Please help.

— asked October 24th 2010 by Raquibul Islam
  • apn
  • apns
  • notification
  • push
2 Comments
  • I understand your trauma as I'm facing the same here.
    But you can do a tricky workaround method follow these steps:
    1> unregister push notification on application start app.js by pasting the following line:
    Titanium.Network.unregisterForPushNotifications; (no need to add any parameter here).
    2> register again after an interval function and retrieve the e.deviceToken again and this time save it in your database using xhr method

    — commented March 18th 2012 by Talal Manaa
  • But Still the best practice as you'll never get the tokenDevice Again no matter what you try in Titanium once the device was registered.
    You can do the following in your database engine:
    1> make two tables, the first table has two fields to store your device unique ID, and the deviceToken.
    2> the second table also stores two things: app name, and device id (not the token as you will not be able to retrieve it again).
    3> in your app try to insert your device id along with the app name using xhr.
    4> in your server side script disallow saving the device id and app name if they already exists.
    Then related both tables by the device id field in your query, so that you can target users using certain app.
    This is the only way that I use because the Ti.Network.remoteDeviceUUID will never work and it will always gives you null !

    — commented March 19th 2012 by Talal Manaa

1 Answer

  • I am using easyapns and set up my script using the following block of code, but this will work with almost any apn server since all the info is required.

    loginWindow.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)
        {
            var deviceToken = e.deviceToken;
            label.text = "Device registered. Device token: \n\n"+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 request = Titanium.Network.createHTTPClient();
            request.onload = function()
            {
                Ti.API.info('in utf-8 onload for POST');
            };
            request.onerror = function()
            {
                Ti.API.info('in utf-8 error for POST');
            };
            request.open("GET","http://somesite.com/apns/apns.php?task=register&appname=appname&appversion="+escape(Titanium.App.version)+"&deviceuid="+escape(Titanium.Platform.id)+"&devicetoken="+escape(e.deviceToken)+"&devicemodel="+escape(Titanium.Platform.model)+"&devicename=tester&deviceversion="+escape(Titanium.Platform.version)+"&pushbadge=enabled&pushalert=enabled&pushsound=enabled");
            request.send();
        },
        error:function(e)
        {
            label.text = "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));
        }
    
    });
    
    — answered November 3rd 2010 by Christian Sullivan
    permalink
    0 Comments
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.