在通知栏中显示小图标但在通知面板中隐藏

4

我在这个论坛里尝试找到相关内容,但是找到的几个方法都没有用。我只需要将其隐藏在通知面板中,在大图标旁边出现,但如果我将其隐藏,则它也会从通知栏中消失。

enter image description here

有没有办法只显示大图标,就像第一和第四个通知那样?
这是我正在使用的代码:

mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent;

    Notification.Builder mBuilder =
            new Notification.Builder(this)
                    .setSmallIcon(R.mipmap.small_icon).setTicker(getApplicationContext().getResources().getString(R.string.app_name))
                    .setLargeIcon(largeIcon)
                    .setAutoCancel(true)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                    .setLights(Color.GREEN, 1000, 1000)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setContentTitle(extras.getString("title"))
                    .setStyle(new Notification.BigTextStyle().bigText(extras.getString("message")))
                    .setContentText(extras.getString("message"));
        edit.putString(Config.FRAGMENT, extras.getString("fragment"));
        edit.commit();
        intent = new Intent(this, NavigationActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                intent, 0);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
2个回答

7
你可以在构建通知后将其设置为不可见:
int smallIconId = context.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
            if (smallIconId != 0) {
                notification.contentView.setViewVisibility(smallIconId, View.INVISIBLE);
                notification.bigContentView.setViewVisibility(smallIconId, View.INVISIBLE);
            }

1
嗨,这还安全吗?contentView和bigContentView已经过时了。 - Neon Warge
1
显然,它应该被替换为setCustomBigContentView,我会更新api 24+的答案。 - awsleiman
builder.setPriority(NotificationCompat.PRIORITY_MAX) 不起作用 - Ahamadullah Saikat

0

对我来说有效。

试试看。

Notification.Builder builder=new Notification.Builder(this);
            builder.setContentIntent(intent)
                    .setSmallIcon(R.drawable.ic_notification).setTicker(context.getResources().getString(R.string.app_name))
                    .setContentTitle(context.getResources().getString(R.string.app_name))
                    .setContentText(message);
            Notification notification = builder.build();

但是大图标呢?我需要在标题和通知列表中有不同的图标。 - Iban Arriola
1
如果您想设置大图标,则无法隐藏小图标。在上面的屏幕截图中,如果您没有设置largeIcon,则只会显示单个图标。您可以使用 builder.setColor(<colorCode>) 更改颜色。 - Ankita Shah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接