Anju
Anju

Reputation: 9479

Call Asynctask when a push notification appears

My requirement is when a push notification comes in my device, I need to call an Asynctask. The app can be running in background. I shouldn't click the notification, instead when it comes I need to call Asynctask. Is that possible?

Upvotes: 1

Views: 1920

Answers (2)

Dixit Patel
Dixit Patel

Reputation: 3192

In your GCMIntentService just override onMessage(..) method this method called when push notification is comming in device.

   @Override
    protected void onMessage(Context context, Intent intent) {

        mContext = context;

        final String message = intent.getStringExtra("message");
        Log.e(TAG, "GCM Received message : "+message);


    AsyncTask<Void, Void, Void> DatabaseOperationTask = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {

                // do your Database Operation here

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {

                super.onPostExecute(result);

            }
        };
        DatabaseOperationTask.execute();

    }

Upvotes: 2

farrukh
farrukh

Reputation: 627

Yes it is possible

you have GCMIntentService which have the method

    @Override
    protected void onMessage(Context context, Intent intent)
        {

        }

this method receives message and generate notification you can execute your async task in this method if you need any context the service has its own context

Upvotes: 1

Related Questions