如何保持通知徽章

时间:2018-12-16 16:51:07

标签: android push-notification badge

在android Oreo中,Android 8.0在快捷方式图标上支持徽章。

问题是,规范说,即使用户从通知面板删除了通知,我也需要保持徽章。

我该怎么做?

ShortcutBadger在此手机上不起作用。

理论上,当我显示此数字时,我会这样做:

   api.getUnreadNotificationCount(unreadCount -> {
            ShortcutBadger.applyCount(getApplicationContext(), unreadCount);

            NotificationCompat.Builder groupBuilder = new NotificationCompat.Builder(CloudMessaging.this, pushNotificationModel.collapseKey == null ? getApplicationContext().getString(R.string.default_notification_channel_id) : pushNotificationModel.collapseKey)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setAutoCancel(true)
                    .setSound(null)
                    .setPriority(NotificationCompat.PRIORITY_LOW)
                    .setChannelId(getString(R.string.default_notification_channel_id))
                    .setGroup(remoteMessage.getCollapseKey())
                    .setGroupSummary(true)
                    .setNumber(unreadCount)
                    .setSubText(unreadCount + " new updates");

            pushNotificationModel.summaryMessageId = 100;
         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                notificationManager.notify(pushNotificationModel.collapseKey, pushNotificationModel.summaryMessageId, groupBuilder.build());
            }
   });

WhatsApp可以做到这一点。

1 个答案:

答案 0 :(得分:0)

尝试此代码,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  builder.setChannelId(NOTIFICATION_CHANNEL);
  NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, "Badger Sample",
  NotificationManager.IMPORTANCE_DEFAULT);
  channel.setShowBadge(true);
  mNotificationManager.createNotificationChannel(channel);
 }

移至线下

ShortcutBadger.applyCount(getApplicationContext(), unreadCount);