Reputation: 3296
I have an android app successfully set up to receive notifications using Urban Airship, but am running into problems in handling the PushManager.ACTION_NOTIFICATION_OPENED) broadcast. My BroadcastReceiver is working, receiving the message, and calling the following (from the example code):
Intent launch = new Intent(Intent.ACTION_MAIN);
launch.setClass(UAirship.shared().getApplicationContext(), Main.class);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UAirship.shared().getApplicationContext().startActivity(launch);
This works fine and brings the main activity back to the foreground, except for the case in which the app is no longer running. If I send a notification to a phone, kill the app, and then open the notification, the app crashes on a NullPointerException:
Failed to load meta-data, NullPointer: null
Unable to takeOff automatically
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver com.urbanairship.CoreReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
at android.app.ActivityThread.access$1500(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I can't figure out what could be causing this. Any thoughts?
Upvotes: 4
Views: 2173
Reputation: 5146
I met the same problem. I resolved it by putting the UAirship initialization into the onCreate of Application, not activity.
Hi Amit Jayaswal, please see the follow example:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Optionally, customize your config at runtime:
//
// AirshipConfigOptions options = new AirshipConfigOptions();
// options.inProduction = false;
// options.developmentAppKey = "Your Development App Key";
// options.developmentAppSecret "Your Development App Secret";
//
// UAirship.takeOff(this, options);
UAirship.takeOff(this, new UAirship.OnReadyCallback() {
@Override
public void onAirshipReady(UAirship airship) {
// Perform any airship configurations here
// Create a customized default notification factory
DefaultNotificationFactory defaultNotificationFactory = new DefaultNotificationFactory(getApplicationContext());
defaultNotificationFactory.setSmallIconId(R.drawable.ic_notification);
//defaultNotificationFactory.setColor(NotificationCompat.COLOR_DEFAULT);
// Set it
airship.getPushManager().setNotificationFactory(defaultNotificationFactory);
// Enable Push
airship.getPushManager().setPushEnabled(true);
}
});
}
}
Upvotes: 1
Reputation: 3296
OK, figured it out.
I was instantiating the UrbanAirship and registering the IntentReceiver from an Activity context, not from an Application context.
Oops.
Upvotes: 3