在onMessage()上收到推送通知时执行一些代码


Executing some code when a push notification is recived on onMessage()

这是我的代码,当收到推送通知时,我必须在AsyncTask中执行一些http请求。正如我所理解的,如果我需要在推送通知到达时做一些事情,我必须在OnMessage()方法中完成。然而,应用程序崩溃时,它收到通知,不做我需要它做的事情:代码:

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("price");
    //sends info to the server
    new PostAsyncTask().execute();
    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

非常感谢!(如果你需要全班同学,就说出来。然而,我可以告诉你,PostAsyncTask执行一些httpRequest代码。)

Logcat:

http://pastebin.com/LggC0uFq

AsyncTask:(它只是调用一个函数来执行一些http请求)

class PostAsyncTask extends AsyncTask<String, Integer, Boolean> {
     protected Boolean doInBackground(String... params) {
        ReNewCoordinates();
         postData(lat, lon);
            return true;
     }
     protected void onPreExecute() {
         // show progress dialog
     }

         protected void onPostExecute(Long result) {
             // hide  progress dialog
         }
     }

我猜,你不应该使用推送通知的onMessage方法的AsyncTask。你应该调用一个服务来完成创建http连接的任务。还记得使用单独的线程,如果你使用服务类,因为它只运行在UI线程。我希望这对你有所帮助。