如何在没有显示通知的情况下在奥利奥设置徽章计数?

问题描述 投票:4回答:2

我曾经使用this library在app图标中显示一个数字,如下所示:

ShortcutBadger.applyCount(context, numberToShow);

OneSignal在其Android SDK中也具有相同的功能。

现在在奥利奥,随着通知渠道的引入,事情变得复杂了。我可以创建一个频道。然后,我还可以创建如下通知:

public static void createNotification(Context context, int numberToShow) {
    Notification notification = new NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id))
            .setContentTitle("Dummy Title")
            .setContentText("Dummy content")
            .setSmallIcon(R.drawable.app_icon)
            .setNumber(numberToShow)
            .build();
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(0, notification);
}

但是,我必须通过此解决方案显示通知,我不需要这样做,因此不需要。在奥利奥有什么方法可以实现我以前做过的同样的事情,即只显示“通知点”或附加到应用程序图标的数字?

android android-notifications android-8.0-oreo badge
2个回答
3
投票

抱歉,除了您描述的Notification场景之外,没有SDK级别支持在启动器图标上显示数字或其他徽章。


4
投票

设置通知渠道的重要性

IMPORTANCE_MIN

int importance = NotificationManager.IMPORTANCE_MIN;然后创建频道 -

NotificationChannel nChannel = new NotificationChannel
                    (channelId, title, importance);

这将设置徽章计数(在长按图标时显示),而不通知用户系统托盘中的任何通知。虽然通知将在托盘中,但不会弹出并静静地驻留在那里。

© www.soinside.com 2019 - 2024. All rights reserved.