Я использую код:
/** Prepare the notification UI **/
private void prepareNotificationUI(String mTitle, String mDescription, int notificationId, Bundle bundle) {
Intent intent = new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.putExtras(bundle);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationHelper notificationHelper = new NotificationHelper(getApplicationContext());
nb = notificationHelper.getNotification(pendingIntent, mTitle, mDescription);
if (nb != null) {
notificationHelper.notify(notificationId, nb);
}
}else {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String channelId = mContext.getString(R.string.app_name);
Bitmap largeIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.notification_icon);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, channelId);
//Notification.Builder notificationBuilder=new Notification.Builder(mContext);
notificationBuilder.setContentTitle(mTitle);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mDescription));
notificationBuilder.setContentText(mDescription);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.notification_icon);
notificationBuilder.setColor(ContextCompat.getColor(mContext, R.color.white));
notificationBuilder.setLargeIcon(largeIcon);
} else {
notificationBuilder.setSmallIcon(R.drawable.notification_icon);
notificationBuilder.setLargeIcon(largeIcon);
}
/*notificationBuilder.setSmallIcon(R.drawable.notification_icon);
notificationBuilder.setColor(ContextCompat.getColor(mContext, R.color.white));*/
notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
notificationBuilder.setLights(Color.WHITE, 3000, 3000);
notificationBuilder.setContentIntent(pendingIntent);
notificationManager.notify(notificationId, notificationBuilder.build());
}
}
- Я получаю очень маленькую иконку в устройствах Marshmellow. Работает нормально
в других устройствах
Раньше иконка вообще не отображалась в Marshmellow. Затем я
добавил код ниже. Значки отображаются, но размер крошечный
notificationBuilder.setLargeIcon (largeIcon);