Ripitchip
Ripitchip

Reputation: 41

Application icon badge not displayed Android

For an assignment, I have to write an application which can display all contacts and their birthday date (& create / modify them). So far so good. Then, I have to fire a notification with all people who are born today.

The notification is launched without problem but I don't get the app icon badge.

Notification is launched

After searching, I found out I have to use the channels -> done.

I've verified in the settings if the notification dot is enabled:

Notification options

I've verified the android documentation saying "it's automatic you shoundn't have anything to do"

My simulator is using Android 9.0 which is greater than android oreo.

So what's wrong ?

public class BirthdayListener implements View.OnClickListener {
    private static final String CHANNEL_ID = "ch.hefr.tic.birthday_channel";
    private static final int SUMMARY_ID = -1;
    private static final String GROUP_KEY = "ch.hefr.tic.group_channel";

    private Context context;
    private NotificationManager notificationManager;

    public BirthdayListener(Context context) {
        this.context = context;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = context.getResources().getString(R.string.channel_name);
            String description = context.getResources().getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            channel.setShowBadge(true);
            notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    @Override
    public void onClick(View v) {
        List<Contact> hasBirthday = searchBirthdays();

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);

        NotificationCompat.InboxStyle summaryStyle = new NotificationCompat.InboxStyle();
        summaryStyle.setBigContentTitle(hasBirthday.size() + " new messages");

        for(int i = 0; i < hasBirthday.size(); ++i) {
            Contact contact = hasBirthday.get(i);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_notification)
                    .setContentTitle("Birthday")
                    .setContentText(contact.getName() + " has his birthday today !")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setGroup(GROUP_KEY)
                    .setNumber(5);

            String phoneNumber = contact.getPhoneNumber();
            if(phoneNumber != null && !phoneNumber.equals("")) {
                Intent smsIntent = new Intent(Intent.ACTION_VIEW);
                smsIntent.setData(Uri.parse("sms:" + phoneNumber));

                PendingIntent pending = PendingIntent.getActivity(context, 0, smsIntent, 0);
                builder.addAction(R.drawable.ic_arrow_back, "SMS", pending);
            }

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
                notificationManagerCompat.notify(i, builder.build());
            else
                notificationManager.notify(i, builder.build());
        }

        NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_notification)
                .setContentTitle("Today Birthdays")
                .setContentText(hasBirthday.size() + " new messages")
                .setStyle(summaryStyle)
                .setGroup(GROUP_KEY)
                .setGroupSummary(true)
                .setNumber(5);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            notificationManagerCompat.notify(SUMMARY_ID, summaryBuilder.build());
        else
            notificationManager.notify(SUMMARY_ID, summaryBuilder.build());
    }
}

Edit : tried to use the setColor on NotificationCompat.Builder builder but it changes the color of the icon. Still don't have any badge.

Upvotes: 1

Views: 2350

Answers (1)

Ripitchip
Ripitchip

Reputation: 41

Thanks to Bob and his link : https://support.google.com/pixelphone/thread/1575301?hl=en, I found out I had to go to settings < apps and notifications < special app access < notifications < notification access < turn on for "pixel launcher". Then the notification dots would appear as expected !

Well time to write my report I guess.

Upvotes: 1

Related Questions