【问题标题】:Notification Count not shown Badges in App Launcher Icon in Android Version 29 and 27Android 版本 29 和 27 中的应用启动器图标中未显示通知计数徽章
【发布时间】:2020-07-23 07:57:12
【问题描述】:

我想在我的 Android 应用的启动器图标上显示未读消息数。只有在用户阅读所有消息后才应将其清除。我在NotificationCompat.Builder 中尝试过setnumber()。但它没有显示通知计数。

我在两个安卓设备上执行了我的代码。

  1. 真正的 Android 设备 - 版本为 29(当我生成通知时,应用启动器中出现红点)

  2. Android 模拟器 - 版本为 27(未显示)

通知通道创建:

private void createNotificationChannel(String notificationChannelId, String systemTrayNotificationName){

    Log.i("Tag","createNotificationChannel "+Build.VERSION.SDK_INT);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        CharSequence name = systemTrayNotificationName;
        String description = systemTrayNotificationName;
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, name,importance);
        notificationChannel.setDescription(description);
        notificationChannel.setShowBadge(true);
        notificationChannel.canShowBadge();

        NotificationManager notificationManager = getContext().getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

形成通知的代码

public void buildNotification(){

    createNotificationChannel(NOTIFICATION_CHANNEL_ID,SYSTEM_TRAY_NOTIFICATION_NAME);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_home_black_24dp)
            .setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher))
            .setContentTitle("Title")
            .setContentText("Content Text")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setNumber(5)
            .setBadgeIconType(NotificationCompat.BADGE_ICON_NONE)
            .setAutoCancel(true);

    // Get an instance of the NotificationManager service
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(getContext());

    // Issue the notification with notification manager.
    notificationManager.notify(200, notificationBuilder.build());
}

【问题讨论】:

    标签: android notifications android-8.0-oreo


    【解决方案1】:

    您使用的启动器必须支持通知数量。 据我所知,谷歌手机典型的像素启动器默认作为模拟器的launcher3 不支持通知计数,因为它在您的第一种情况下发生。 您可以尝试使用第三方启动器来查看计数是否有效,但正如刚才所说,此功能必须由启动器原生支持。

    【讨论】:

    • 谢谢,请告诉我,我在哪里可以找到支持“在应用启动器中显示计数”的安卓设备列表?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    相关资源
    最近更新 更多