Search code examples
androidandroid-intentandroid-serviceandroid-notificationsandroid-intentservice

PendingIntent from Notification not received


I got an IntentService that does some long running work synchronously within onHandleIntent(Intent). So I'm showing a Notification for as long as the service runs. Now I would like to stop the service once the user taps onto the Notification.

So here's what I got. A broadcast-receiver class that is registered once the service is created. A notification with a pending-intent that is shown when the service does some work.

But somehow I never get the broadcast intent when the user taps onto the notification. Any idea?

Here's part of the code that can easily be re-used for testing.

public class SyncService extends IntentService
        {

    private static final String TAG = SyncService.class.getSimpleName();
    private static final int NOTIFICATION_REF_ID = 1;

    private static final String ACTION_CANCEL = TAG + ".CANCEL";
    private CancelReceiver receiver;

    public SyncService() {
        super(SyncService.class.getSimpleName());
    }

    private class CancelReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            stopSelf();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        receiver = new CancelReceiver();
        IntentFilter filter = new IntentFilter(ACTION_CANCEL);
        registerReceiver(receiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (receiver != null)
            unregisterReceiver(receiver);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Intent cancelIntent = new Intent(this, CancelReceiver.class);
        cancelIntent.setAction(ACTION_CANCEL);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                cancelIntent, 0);

        // create notification and set pending-intent
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("some caption")
                .setContentText("some hint")
                .setTicker("some caption").setSmallIcon(R.drawable.ic_action_refresh)
                .setOngoing(true).setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent).setProgress(0, 0, true);

        Notification notification;
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
            notification = builder.getNotification();
        else
            notification = builder.build();

        // show notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(SyncService.class.getCanonicalName(), NOTIFICATION_REF_ID,
                notification);


        // now do some long running work synchronously


        // cancel/remove notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(SyncService.class.getCanonicalName(), NOTIFICATION_REF_ID);
    }
}

The service is started from within my activity:

        Intent syncIntent = new Intent(this, SyncService.class);
        startService(syncIntent);

The idea to stop the service like that comes from this post.


Solution

  • Change

    Intent cancelIntent = new Intent(this, CancelReceiver.class);
    

    to

    Intent cancelIntent = new Intent();
    

    although onReceive() will be called which calls stopSelf() which invokes onDestroy() the planned work will go on until finished. To handle this add to the sync class

    private boolean cancelled = false;
    

    set to true in onReceive and in your 'some long running work synchronously' check regularly cancelled and break out.