How to remove a notification action

I have a method that creates / updates a notification:

private void createNotification(Boolean removeAction) {
        getButtonPendingIntent().cancel();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My App")
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_MAX);

        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        bigTextStyle.setBigContentTitle("My App");

        if (removeAction) {
            String msg = "Some message";
            bigTextStyle.bigText(msg);
            builder.setContentText(msg);
            // this will remove button but leaves an empty space where the button existed
            builder.addAction(0, null, null);
        } else {
            String msg = "You are logged in. Slide down to logout.";
            bigTextStyle.bigText(msg);
            builder.setContentText(msg);
            builder.addAction(R.drawable.some_icon, "My Button", getButtonPendingIntent());
        }

        builder.setStyle(bigTextStyle);

        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, ResultActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(ResultActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(100, builder.build());
    }

      

Sometimes I need to delete an action I created with a notification. If I just create a new assembly and notify of a new event that has no action, the action button remains.

I add to this code:

builder.addAction(0, null, null);

      

And that removes the action button, but leaves a blank space on the notification screen where the button was. Is there a "correct" way to remove an activity from a notification and the space it uses?

+3


source to share


6 answers


Much time has passed from the last answer. But I think it might be useful for someone. There NotificationCompat.Builder

is an ArrayList - mActions

. I cleared this Arraylist by calling myNotificationBuilder.mActions.clear()

. And it helped me remove the action button from my notification without recreating it.



+2


source


I have had the same problem lately. The only solution I could think of was to cancel the previous notification first, before I updated it.



NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(100);
notificationManager.notify(100, builder.build());

      

0


source


Try the following:

if (removeAction) {
            String msg = "Some message";
            bigTextStyle.bigText(msg);
            builder.setContentText(msg);
            // this will remove button but leaves an empty space where the button existed
            if(!builder.mActions.isEmpty()) builder.mActions.clear();// remove button (:
        } else {
            String msg = "You are logged in. Slide down to logout.";
            bigTextStyle.bigText(msg);
            builder.setContentText(msg);
            builder.addAction(R.drawable.some_icon, "My Button", getButtonPendingIntent());
        }

      

Good luck!

0


source


I agree with Eugen, in my Notification class I create this method:

/**
 * Cancel the notification by its id
 * @param contexto
 * @param id
 */
public static void cancell(Context contexto, int id){
    NotificationManager notificacaoManager = (NotificationManager) contexto.getSystemService(Activity.NOTIFICATION_SERVICE);
    notificacaoManager.cancel(id);
}

      

0


source


I think if you add .setAutoCancel(true)

to your builder maybe do the job

0


source


Just clear actions from notifications and notify them.

notificationBuilder.mActions.clear();
notificationManager.notify(id, notificationBuilder.build());

      

0


source







All Articles