-1

I'm developing an android app where I display notifications with actions. But on action click notification not clearing, It stuck in that shade. How do I clear a notification on action click?

MY CODE

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, SettingsActivity.class);
PendingIntent openSettingsActivity = PendingIntent.getActivity(this,1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.addAction(R.drawable.ic_notification_button, "Settings", openSettingsActivity);
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
notificationBuilder.setContentTitle(title);
                notificationBuilder.setContentText(text);
                notificationBuilder.setAutoCancel(true);
                notificationBuilder.setColor(color);
                notificationBuilder.setSmallIcon(R.drawable.ic_notification);
                notificationBuilder.setContentIntent(openSettingsActivity);
                final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notificationBuilder.build());
ArghadipDasCEO
  • 177
  • 1
  • 12

1 Answers1

-1

Hiding notifications should be processed in the place where the intent is sent. In your current code:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra("hide_notification", true); //add boolean to check later in activity if it should remove notification on activity create

And in your activity smth like this, to check if it should remove notification:

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //check for notification remove
        boolean hideNotification = getIntent().getBooleanExtra("hide_notification", false);
        if (hideNotification) {
            NotificationManagerCompat nmc = NotificationManagerCompat.from(this);
            nmc.cancel(1); //1 - is your notification id
        }
    }

Depends on what you want, maybe it will be better to call that not in onCreate() but onStart()

kara4k
  • 407
  • 5
  • 11