8

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

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

因为 Galaxy S8(Oreo) 显示徽章编号 1。而 Android 8.0 模拟器也显示点。

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

编辑1:

        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);
4

2 回答 2

3

您需要做的就是调用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)

查看修改通知徽章

于 2019-02-01T03:53:08.340 回答
3

这个答案是正确的,但关键是您必须从 Android“应用程序”菜单中手动删除旧版本的应用程序,并在干净的设备上安装新版本mChannel.setShowBadge(false)才能使其正常工作。旧版本(mChannel.setShowBadge(false)不存在)的安装不会导致与此徽章相关的行为发生变化。

于 2020-04-09T12:21:45.720 回答