如何删除通知操作

时间:2014-12-04 22:54:58

标签: android android-notifications

我有一个创建/更新通知的方法:

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());
    }

有时我需要删除使用通知创建的操作。如果我只是创建一个新构建并通知没有该操作的新事件,则操作按钮仍然存在。

我添加了这段代码:

builder.addAction(0, null, null);

然后删除了操作按钮,但仍然在通知屏幕中留下了一个空白区域。是否有权利'从通知中删除操作的方法及其使用的空间?

7 个答案:

答案 0 :(得分:3)

从上次回答中花了很多时间。但我认为对某人来说它可能是有用的。在NotificationCompat.Builder中有一个ArrayList - mActions。我通过拨打myNotificationBuilder.mActions.clear()清除了这个Arraylist。它帮助我从通知中删除操作按钮而不重新创建它。

答案 1 :(得分:0)

我最近遇到了同样的问题。我能想到的唯一解决方案是在我更新之前先取消之前的通知。

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

答案 2 :(得分:0)

试试这个:

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());
        }
祝你好运!

答案 3 :(得分:0)

我同意Eugen,在我的Notification课程中我构建了这个方法:

/**
 * 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);
}

答案 4 :(得分:0)

我认为如果您向构建器添加.setAutoCancel(true),则可以完成工作

答案 5 :(得分:0)

只需清除通知中的操作并通知它。

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

答案 6 :(得分:0)

如果出现错误“只能从同一库组(groupId = androidx.core)中访问Builder.mActions”,我可以解决以下问题:

  1. 首先取消您以前的通知

    notificationManager.cancel(NOTIFICATION_ID);

  2. 创建新的通知生成器,而无需添加操作按钮

  

java

NotificationCompat.Builder builder2 = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
                builder2.setContentTitle("Same title as before").setSmallIcon(R.mipmap.ic_launcher)
                        .setContentIntent(pendingIntent);
                notificationManager.notify(NOTIFICATION_ID, builder2.build());