Search code examples
titaniumpushappceleratortitanium-mobileappcelerator-mobile

Appcelerator: Read custom variable when returning to app from push notification


So I got my first push notifications from my PHP server to the iPhone working (so excited!)

Now when the user clicks on the notification, my app reloads, but it reloads to whatever point it left off at obviously.

Is there a way I can have the app know that it was reloaded from a iOS notification click?

And the next step is to read a custom variable that I sent along with the notification, how would I read my variable? (I added the custom variable to my payload as follows:)

$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default', 'customVar' ==> '1');

Solution

  • Mark, You can get the custom property using the callback property of Register For Push Notifications method. Since any code written in this callback method will be invoked on receiving a remote push.

    Titanium.Network.registerForPushNotifications({
        types: [
            Titanium.Network.NOTIFICATION_TYPE_BADGE,
            Titanium.Network.NOTIFICATION_TYPE_ALERT,
            Titanium.Network.NOTIFICATION_TYPE_SOUND
        ],
      success:function(e)
      {
          deviceToken = e.deviceToken;
          alert("deviceToken = "+deviceToken);
          registerForPush();
      },
      error:function(e)
      {
          alert("Error: "+e.message);
      },
      callback:function(e)
      {
          //It'll be invoked on receiving a remote push, this e.data will contain your custom variables also
          alert("push notification received"+JSON.stringify(e.data));
      }
    });
    

    You can use this link for more information.