Push notifications with Urban Airship on Android

So I’m testing out push notifications using Urban Airship’s system, and I’ve been ripping my hair out for the last day or so trying to get my app to respond to them. It turns out that the example code they have on their site doesn’t actually work. If you do everything exactly as they say, you’ll either get a ActivityNotFoundException when the BroadcastReceiver tries to start your activity, or you’ll get an ANR, and a log trace that looks like this:

E/Bundle  ( 1101): readBundle: bad magic number
E/Bundle  ( 1101): readBundle: trace = java.lang.RuntimeException
E/Bundle  ( 1101): 	at android.os.Bundle.readFromParcelInner(Bundle.java:1580)
E/Bundle  ( 1101): 	at android.os.Bundle.(Bundle.java:82)
E/Bundle  ( 1101): 	at android.os.Parcel.readBundle(Parcel.java:1381)
E/Bundle  ( 1101): 	at android.os.Parcel.readBundle(Parcel.java:1366)
E/Bundle  ( 1101): 	at android.content.Intent.readFromParcel(Intent.java:5479)
E/Bundle  ( 1101): 	at android.content.Intent.(Intent.java:5453)
E/Bundle  ( 1101): 	at android.content.Intent$1.createFromParcel(Intent.java:5444)
E/Bundle  ( 1101): 	at android.content.Intent$1.createFromParcel(Intent.java:5446)
E/Bundle  ( 1101): 	at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:132)
E/Bundle  ( 1101): 	at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:1480)
E/Bundle  ( 1101): 	at android.os.Binder.execTransact(Binder.java:288)
E/Bundle  ( 1101): 	at dalvik.system.NativeStart.run(Native Method)

What’s happening is that you register a PushReceiver with the AirMail control panel app, which then creates and sends an intent to start your activity. The problem being, (I think) that the intent gets sent from the scope of the AirMail app, meaning it wont necessarily have access to your classes.

The way I finally got this to work is by using the setClassName(String, String) method of the intent to explicitly set the packagename and class name of the activity I wanted. Stick this in the onClick method of your PushReceiver:

Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setClassName("com.example", "com.example.YourActivity");
YourApplication.this.startActivity(i);

Comments are closed.