Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

Add badge to the application icon , while new notification in Below Oreo devices in Android

In my Android Application , I want to display App icon badges for all devices. From Oreo Version, it is an default feature. So that we can simply use notificationChannel.setShowBadge(true);

It is working fine for Oreo devices. but I want to show badges in below oreo devices too.

Code for reference

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    String CHANNEL_ID = "1";

                    NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);
                    notificationChannel.setShowBadge(true);
                    Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.app_icon_launcher_main);

                    Notification notification = new Notification.Builder(getApplicationContext())
                            .setContentTitle(remoteMessage.getNotification().getTitle())
                            .setContentText(notificationMsg)
                            .setSmallIcon(R.mipmap.app_icon_launcher_main)
                            .setChannelId(CHANNEL_ID)
                            .setLargeIcon(icon)
                            .setAutoCancel(true)
                            .setContentIntent(pendingIntent)
                            .setPriority(NotificationCompat.PRIORITY_HIGH)
                            .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                            .setStyle(new Notification.BigTextStyle().bigText(notificationMsg))
                            .build();

                    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.createNotificationChannel(notificationChannel);
                    notificationManager.notify(Integer.parseInt(data.get(stringConstants.NOTIFICATION_ID)), notification);
                } else {
                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                            .setContentTitle(remoteMessage.getNotification().getTitle())
                            .setContentText(notificationMsg)
                            .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.app_icon_launcher_main))
                            .setSmallIcon(R.mipmap.app_icon_launcher_main)
                            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), Notification.STREAM_DEFAULT)
                            .setAutoCancel(true)
                            .setContentIntent(pendingIntent)
                            .setPriority(NotificationCompat.PRIORITY_HIGH)
                            .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationMsg));

                    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(Integer.parseInt(data.get(stringConstants.NOTIFICATION_ID)), notificationBuilder.build());
                }
            }

How can I show badges for below Oreo Devices? Anybody faced this problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...