2013-02-13 64 views
4

我有一個android應用程序成功設置爲使用Urban Airship接收通知,但遇到處理PushManager.ACTION_NOTIFICATION_OPENED)廣播時出現問題。我的廣播接收器工作時,接收消息,並調用以下(從示例代碼):當用戶點擊Urban Airship通知時重新啓動android應用程序

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) 

我想不出什麼可能會導致此。有什麼想法嗎?

回答

3

好的,算出來了。

我正在實例化UrbanAirship並從Activity上下文註冊IntentReceiver,而不是從Application上下文註冊。

糟糕。

+1

可以共享代碼,我也越來越相同的問題?請幫助我@TomBomb。 – 2014-02-10 15:11:21

1

我遇到了同樣的問題。我通過將UAirship初始化放入應用程序的onCreate而非Activity來解決它。

嗨阿米特Jayaswal,請參閱如下例子:

公共類MyApplication的擴展應用{

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

}

相關問題