Search code examples
androidandroid-intentpush-notificationnotificationsandroid-pendingintent

Clear notification on Action Button click


I am trying to clear an notification when an user clicken on the action button. I searched a lot on stackoverflow and google, tried a lot but all failed. So I was wondering, if there is any solution how to fix it?

My code for creating notification:

final int NOTIFICATION_ID = 1;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_info_black_24dp);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Geschiedenis \nFrans \nDuits \nNatuurkunde \nWiskunde"));
builder.setContentTitle("Rooster: ");
builder.setOngoing(true);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
Intent showIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);
builder.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Sluiten",  contentIntent);
builder.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

So to be clear: I would like to clear (close) the notification once "Sluiten" is clicked.


Solution

  • To close the notification when the "Sluiten" link is clicked on the notification I have included a working version of code.

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        final int NOTIFICATION_ID = 1;
        NotificationManager notificationManager;
        Button notificationBtn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            if(getIntent()!=null && getIntent().hasExtra(getPackageName())){
                notificationManager.cancel(NOTIFICATION_ID); //closes notification
            }
            notificationBtn = (Button) findViewById(R.id.notificationBtn);
            notificationBtn.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.notificationBtn:
                    showNotification();
                    break;
            }
        }
    
        void showNotification(){
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setSmallIcon(R.drawable.ic_account_box_black_24dp);
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Geschiedenis \nFrans \nDuits \nNatuurkunde \nWiskunde"));
            builder.setContentTitle("Rooster: ");
            builder.setOngoing(true);
            builder.setAutoCancel(true);
            builder.setDefaults(Notification.DEFAULT_ALL);
            builder.setAutoCancel(true);
            Intent showIntent = new Intent(this, MainActivity.class);
            showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Sluiten",  contentIntent);
            builder.setContentIntent(contentIntent);
            notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID, builder.build());
        }
    
    
    }
    

    NOTE In the previous answer I did not include the line

    showIntent.putExtra(getPackageName(), NOTIFICATION_ID); // this line sets off closing notification in onCreate()
    

    which is essential to closing the notification on click