我正在使用代码:
/** 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中根本没有显示。然后我 添加了下面的代码。正在显示图标,但尺寸很小
notificationBuilder.setLargeIcon(largeIcon);
答案 0 :(得分:0)
它对我有用,而不是使用可绘制图标使用mipmap图标。
Bitmap bigIcon = BitmapFactory.decodeResource(mContext.getResources(),
R.mipmap.ic_launcher);
答案 1 :(得分:0)
尝试这样:
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
} else {
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notificationBuilder.build());
希望这可能对您有帮助