Re-launching android application when user clicks Urban Airship notification
Asked Answered
D

2

4

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?

Disgraceful answered 13/2, 2013 at 8:33 Comment(0)
D
3

OK, figured it out.

I was instantiating the UrbanAirship and registering the IntentReceiver from an Activity context, not from an Application context.

Oops.

Disgraceful answered 1/3, 2013 at 20:55 Comment(1)
Can you share the code I am also getting the same problem.? Please help me @TomBomb.Congruent
C
1

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);
        }
    });
}

}

Condottiere answered 12/11, 2014 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.