【问题标题】:how to keep notification badge on如何保持通知徽章开启
【发布时间】:2023-03-28 02:35:01
【问题描述】:

在 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 能够做到。

【问题讨论】:

  • 您使用的是哪家公司的手机?
  • 三星galaxy s9
  • 布尔成功 = ShortcutBadger.isBadgeCounterSupported(this);在 applyCount 方法之前写下这一行并检查响应是否正确,而不是您的设备正在使用徽章。
  • 请在问题中阅读我声明 ShortcutBadger 确实支持这款手机,但 Whatsapp 确实支持这款手机,所以有办法,我只是不知道这是什么。你呢?
  • @Pratik18 他们的徽章看起来和所有其他徽章一模一样,我认为他们不是自己制作的……如果是,我该如何制作自己的?

标签: android push-notification badge


【解决方案1】:

试试这个代码,

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);

【讨论】:

  • 我的问题不是徽章出现 - 当我从通知窗格中关闭所有通知时,徽章消失了,我正在使用频道 ID 和 setShowBadge(true)
  • 您可以设置不可取消的通知。当用户在打开应用程序并清除通知后单击您的通知时,而不是在您的应用程序徽章移除后。
猜你喜欢
  • 2013-09-29
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多