在onMessage()上收到推送通知时执行一些代码
This is my code, and I have to execute some http request in an AsyncTask when a push notification is received. As I understood if I need to do something when a push notification arrives, I have to do it within the OnMessage() method. However the app crashes when it receives the notification and does not do what I need it to do: Code:
@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);
}
Thank you very much! (In case you need the whole class just say it. However I can tell you that the PostAsyncTask executes some httpRequest code.)
Logcat:
AsyncTask: (It just calls a function that executes some http request)
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
}
}
I guess, you should not use AsyncTask from the onMessage method of Push Notification. You should call a service which does the task of making http connection. Also remember to use separate thread if you are using Service class as it runs on UI thread only. I hope this helps you out.