Blog

Phonegap 2.5 & Urban Airship Troubleshooting

April 21st, 2013

We have been using Phonegap / Cordova to develop native iPhone and Android apps for over 2 years now. We have integrated Phonegap App to get its content from WordPress and send push notifications from WordPress whenever users chooses to notify users by publishing a post. We love Cordova as it is easy to develop native applications for our clients who don’t have budget to develop seperate native apps for these two very popular mobile platforms. They can save money and maintenance costs as most of the code is all in HTML and it can be shared between Android and IOS very efficiently with minimal changes.

Recently we upgraded one of the existing apps in the store to Cordova 2.5 and also upgraded to latest Urban Airship libraries.

We ran into couple of issues after the upgrade and would love to share how we have solved those issues.

Problem : unable to send javascript in raisePush in PushNotificationPlugin.java Phonegap plugin

The above error is happening on UA PushNotificationPlugin.java when a notification came in and serialized and sending that payload to clientside javascript via the Plugin.

At first glimpse it might look like it’s payload serialization issue. But it is not.

This one is hard to troubleshoot as the same code is working fine on IOS side. On page load  of client side we are registering to UA libraries by doing push.enabled() and then registering for push event to do our client side logic to  display the message.

Reason : Issue here is when we are registering to the push event.

On Android side, when the page loads which we are detecting by jquery DOM ready event, we are registering to the push event by following the  UA js example as follows

push = window.pushNotification;
push.enablePush();
push.registerEvent('push', on_push);

If you trace it carefully, push is null. So event registration for push never happened. Now when the plugin on the native side is trying to send the message payload to this register event it is failing with error “unable to send javascript in raisePush”

Solution:

Very easy once you figure out the reason behind the issue. After the device ready event is fired, wait for some time before you are registering the cordova plugins on the client side. This also helped in solving some of the issues around childbrowser plugin.

 

function onBodyLoad()
{
  document.addEventListener("deviceready", initiate, false);
}

function initiate(){
  setTimeout(app.init(), 5);// this did the trick for proper client side plugin event registrations
}

 

This happened only on the android side. But I think it doesn’t harm to wait for couple of ms  on client side before registering for the corodova plugin callback events on both platforms.

Hopefully this will save your time if you are having similar issues