Search code examples
androidandroid-notifications

Auto Cancel Custom Notification using RemoteView


I have created a custom layout notification using remoteview. Problem I'm facing is, How to autoCancel the notification once the user touches it. I did try few things, but none is giving desired result.

Find Code below:

RemoteViews remoteView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);      
    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
    builder.setSmallIcon(R.drawable.ic_launcher);

    Intent intent = new Intent(MainActivity.this, SecondActivity.class);

    PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, RQST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    remoteView.setOnClickPendingIntent(R.id.btnNotification, pIntent);

    builder.setAutoCancel(true);
    builder.setContent(remoteView); 
    Notification notify = builder.build();
    notify.flags |= Notification.FLAG_AUTO_CANCEL;
    NotificationManager notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notiManager.notify(NOTY_ID, notify);

Solution

  • The way I achieved this is by creating BroadcastReceiver which controls button clicks from Notification. Create something like this :

    public class NotificationButtonListener extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            int action = intent.getIntExtra("mode", -1);
            switch (action) {
                // do some things depending on action if there are more stuff to do
            }
        }
    }
    

    Don't forget to add your BroadcastListener to your manifest file:

    <receiver android:name=".NotificationButtonListener">
    

    And you can create a helper class for creating and cancelling notification:

    public class NotificationHelper {
    
        private static final int NOTIFICATION_ID = 5;
    
        private static NotificationManager mNotificationManager = null;
    
        public static void showNotification(Context context) {
    
            mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
            RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
            contentView.setOnClickPendingIntent(R.id.btn_close, closePendingIntent);
    
            Intent mMainIntent = new Intent(context, MainActivity.class);
            mMainIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent mMainPendingIntent = PendingIntent.getActivity(context, 555, mMainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setTicker("Playing")
                    .setContent(contentView)
                    .setAutoCancel(false)
                    .setOngoing(true)
                    .setSmallIcon(R.drawable.ic_av_play)
                    .setContentIntent(mMainPendingIntent);
    
            Notification notification = mBuilder.build();
    
            mNotificationManager.notify(NOTIFICATION_ID, notification);
    
        }
    
        public static void cancelNotification() {
            if (mNotificationManager != null) {
                mNotificationManager.cancelAll();
            }
        }
    }
    

    And using that class in your NotificationButtonListener you can call NotificationHelper.cancelNotification(); .

    Hope this helps you!