Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android badge count not showing more than one digit like 10,11,...100 #375

Open
AdilHypeteq opened this issue Jun 2, 2022 · 12 comments
Open

Comments

@AdilHypeteq
Copy link

Hello friends,

I have tried to implement this library and but facing one or two issues like not support all devices and if app badge count showing then not showing more than one digit it's stuck at 9 number.

So after 9 badge count not showing number like 10,11,12.... it's just show one half second and then again showing 9 badge.

Please help me.

@jkasten2
Copy link

jkasten2 commented Jun 2, 2022

@AdilHypeteq Android 8 introduced Android Notification Channels that supports badges. Most Android Launchers should be using this already.

I recommend you include the Android version and device model and if it is up to date if you do really need to support an older version of Android. However this library hasn't been updated in a number of years, most likely due to Android Notification Channels noted above.

@AdilHypeteq
Copy link
Author

@jkasten2 I have already implemented Notification Channels like this, is there anything wrong?

private void sendNotification(RemoteMessage remoteMessage) {

      RemoteMessage.Notification notification = remoteMessage.getNotification();
      try {
          Intent intent = new Intent(this, MainActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

          PendingIntent pendingIntent;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
              pendingIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, PendingIntent.FLAG_IMMUTABLE);
          } else {
              pendingIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, PendingIntent.FLAG_UPDATE_CURRENT);
          }

          String channelId = "channel-01";
          String channelName = "Channel Name";
          int importance = NotificationManager.IMPORTANCE_HIGH;

          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                  .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                  .setSmallIcon(R.mipmap.ic_launcher)
                  .setContentTitle(notification.getTitle())
                  .setContentText(notification.getBody())
                  .setAutoCancel(true)
                  .setNumber(count)
                  .setDefaults(Notification.DEFAULT_ALL)
                  .setPriority(NotificationCompat.PRIORITY_HIGH)
                  .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                  .setContentIntent(pendingIntent);

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
              notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
              notificationBuilder.setColor(getResources().getColor(R.color.purple_200));
              notificationBuilder.setFullScreenIntent(pendingIntent, true);
          } else {
              notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
          }

          NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("chats","Messages"));
              NotificationChannel mChannel = new NotificationChannel(channelId, channelName, importance);
              mChannel.setDescription("description");
              mChannel.setShowBadge(true);
              mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
              notificationManager.createNotificationChannel(mChannel);
          }
          int num = (int) System.currentTimeMillis();
          notificationManager.notify(num,notificationBuilder.build());
          notificationsQueue.postRunnable(() -> setBadge(count));
      } catch (Exception e) {
          e.printStackTrace();
      }
  }

@jkasten2
Copy link

jkasten2 commented Jun 2, 2022

@AdilHypeteq Looks correct to me, I see you have mChannel.setShowBadge(true); and using that channelId with NotificationCompat.Builder(this, channelId). I also see you have setNumber(count) which also looks correct.

Possibly ShortcutBadger is interfering , I am guessing the call to setBadge(count) is where you are calling this? Can you try removing it?

Also what Android version, device model, and Android Launcher are you using?

Also this Android doc on badges might be useful:
https://developer.android.com/training/notify-user/badges

@AdilHypeteq
Copy link
Author

@jkasten2 I have also tried this but not getting success.

@jkasten2
Copy link

jkasten2 commented Jun 2, 2022

@AdilHypeteq I would guess then that this is some designed behavior for the Android Launcher you are using.

@AdilHypeteq
Copy link
Author

AdilHypeteq commented Jun 2, 2022

@jkasten2 I'm using Realme XT, Android 11 and in this device show only red dot not showing number like WhatsApp.

@AdilHypeteq
Copy link
Author

@jkasten2 if you have any working demo then please send me.

@jkasten2
Copy link

jkasten2 commented Jun 2, 2022

Just some guess:

  • Realme might whitelist WhatsApp to show a number instead of a dot.
  • Realme has another API to set badges like this. It my be public, or a private API they only share with some apps.

I haven't seen this issue with AOSP Android or the 3rd party Nova Android Launcher.
I don't have a Realme device to try.

@AdilHypeteq
Copy link
Author

@jkasten2 if you have any demo that working in other devices then please share with me.

@jkasten2
Copy link

jkasten2 commented Jun 2, 2022

@AdilHypeteq
Here is a Samsung S21 on OneUI 4.1 (Android 12) going over 9 for a badge value only using NotificationChannel (not ShortcutBadger).

device-2022-06-02-013926.mp4

@AdilHypeteq
Copy link
Author

@jkasten2 can you share me demo code or GitHub link?

@jkasten2
Copy link

jkasten2 commented Jun 2, 2022

@AdilHypeteq Was sending pushes through the OneSignal push notification service in that video, using this example project to build the app for the device:
https://github.com/OneSignal/OneSignal-Android-SDK/tree/main/Examples/OneSignalDemo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants