当用户点击Urban Airship通知时重新启动Android应用程序

4

我已经成功地设置了一个使用Urban Airship接收通知的Android应用程序,但在处理PushManager.ACTION_NOTIFICATION_OPENED广播时遇到了问题。 我的BroadcastReceiver正在工作,接收消息,并调用以下代码(来自示例代码):

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

这个方法在应用程序仍在运行时可以成功将主活动带回前台,但是如果应用程序已经停止,则会出现问题。例如,如果我向手机发送一个通知,然后关闭应用程序,再打开通知,则应用程序会因为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)

我无法弄清楚是什么原因导致这种情况。有什么想法吗?

2个回答

3

好的,我明白了。

我是用Activity上下文而不是Application上下文实例化UrbanAirship并注册IntentReceiver。

糟糕。


1
你能分享一下代码吗?我也遇到了同样的问题。请帮帮我,@TomBomb。 - Amit Jayaswal

1
我遇到了同样的问题。我通过将UAirship初始化放在Application的onCreate中而不是Activity中来解决它。
嗨,Amit Jayaswal,请参考以下示例: 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);
        }
    });
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接