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

Kno*_*ing 7 service android android-notifications notification-channel

startForeground() 需要创建 NotificationChannel 以便它会在 Oreo 设备的启动器图标上显示徽章编号 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);
Run Code Online (Sandbox Code Playgroud)

isa*_*ent 7

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


Eff*_*rix 5

您所需要做的就是调用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)
Run Code Online (Sandbox Code Playgroud)

查看修改通知徽章