java - 通过标准 SDK 在 Android 8 上显示应用程序图标徽章

标签 java android android-studio

根据https://developer.android.com/training/notify-user/badges 从 Android 8 开始,操作系统允许设置应用程序图标通知徽章,但我无法让它工作。

            NotificationManagerCompat nm = NotificationManagerCompat.from(activityContext);

            if (Build.VERSION.SDK_INT >= 26) {
                NotificationChannel nc = new NotificationChannel("ab", "chanelName", NotificationManager.IMPORTANCE_DEFAULT);
                nc.setDescription("A chanel description here");
                nc.setShowBadge(true);

                NotificationManager nmm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                nmm.createNotificationChannel(nc);
            }

            NotificationCompat.Builder nb = new NotificationCompat.Builder(App.instance, "ab");
            nb.setSmallIcon(R.drawable.accept_icon);
            nb.setTicker("here is the ticker");
            nb.setContentText("Here content text");
            nb.setContentTitle("Here content title");
            nb.setNumber(5);
            nb.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
            nm.notify(1, nb.build());

我使用的是 Android 8.1 的 Nexus 6P。 它在系统栏中显示通知,但不显示应用程序图标上的徽章。 我错过了什么?

最佳答案

我认为您的代码很好,但 Nexus 6P 不支持默认启动器上的通知徽章。您需要在其他设备(Pixel 或类似设备 - 当然您可以使用带有 Oreo 或更高版本的模拟器)上进行测试,或者下载支持 Oreo 通知徽章的启动器。

有人也遇到过类似的问题。 在这里阅读更多信息:https://productforums.google.com/forum/#!topic/nexus/NykQ-lp1n2c

关于java - 通过标准 SDK 在 Android 8 上显示应用程序图标徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52526171/

相关文章:

java - JDBC 连接池测试查询 "SELECT 1"未捕获 AWS RDS 写入器/读取器故障转移

android - Kitkat 设备的外部存储写访问

Android:如何将图钉添加到谷歌地图

android-studio - 安卓工作室 : Howto to open SQLite File in SQLScout Plugin

java - Android Studio : add a custom jar as dependency

java - 无法在 gradle.properties 中设置基于 Windows 的路径

java - 如何使用 ProgressMonitor 窗口而不失去对主 JFrame 的焦点?

java - 不使用 JavaMail 通过 Gmail 发送电子邮件

java - 如何删除 jTextArea 的第一行

javascript - 如何在后台运行cocos Creator的android构建