我有一个Android应用程序已成功设置为使用Urban Airship接收通知,但在处理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);
这样可以正常运行并将主要活动带回前台,但应用程序不再运行的情况除外。如果我向手机发送通知,终止应用程序,然后打开通知,应用程序崩溃就会出现NullPointer异常:
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)
我无法弄清楚是什么原因引起的。有什么想法吗?
答案 0 :(得分:3)
好的,想通了。
我正在实例化UrbanAirship并从Activity上下文注册IntentReceiver,而不是从Application上下文注册。
糟糕。
答案 1 :(得分:1)
我遇到了同样的问题。我通过将UAirship初始化放入应用程序的onCreate而非活动来解决它。
嗨Amit Jayaswal,请看以下示例:
公共类MyApplication扩展了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);
}
});
}
}