将徽章添加到应用程序图标,而 Android 中 Oreo 设备下方的新通知

时间:2021-01-07 08:28:12

标签: android android-notifications

在我的 Android 应用程序中,我想为所有设备显示应用程序图标徽章。从奥利奥版本开始,它是默认功能。这样我们就可以简单地使用 notificationChannel.setShowBadge(true);

它适用于奥利奥设备。但我也想在奥利奥设备下方显示徽章。

参考代码

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

如何为以下 Oreo 设备显示徽章?有人遇到过这个问题吗?

0 个答案:

没有答案