startForeground()时如何隐藏徽章计数数字

时间:2019-02-01 03:30:25

标签: android service android-notifications notification-channel

startForeground()需要创建NotificationChannel,以便它将在Oreo设备中的Launcher图标上显示徽章编号1

如何以编程方式隐藏/禁用它?

因为Galaxy S8(Oreo)显示的证卡号为1。 Android 8.0模拟器也显示点。

这就是我现在正在做的事情。但是setShowBadge(false)不起作用

EDIT1:

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        NotificationChannel tmpC = new NotificationChannel(id, "basic", NotificationManager.IMPORTANCE_MIN);
        tmpC.setShowBadge(false);

        manager.createNotificationChannel(tmpC);

        Notification notification = new NotificationCompat.Builder(this, id)
                .setChannelId(id)
                .setAutoCancel(true)
                .build();

        startForeground(getPackageName().hashCode(), notification);

2 个答案:

答案 0 :(得分:0)

您需要做的就是在setShowBadge(false)对象上调用NotificationChannel

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Create notification channel.
NotificationChannel channel = new NotificationChannel({channel_id}, {name}, {importance});
mChannel.setShowBadge(false); // Disable badges for this notification channel.
mNotificationManager.createNotificationChannel(mChannel);

// Create notification and use channel
Notification notification = new NotificationCompat.Builder(context, {channel_id})
         ...
         .build();

// notify
mNotificationManager.notify({notification_id}, notification)

签出Modify a Notification Badge

答案 1 :(得分:0)

answer是正确的,但要点是您必须手动从Android“应用程序”菜单中删除旧版本的应用程序,并使用mChannel.setShowBadge(false)安装新版本在干净的设备上使其正常工作。在旧版本(缺少mChannel.setShowBadge(false)的情况下进行安装)不会导致更改与此徽章相关的行为。