我使用以下代码块创建了一个Android通知:
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(NOTIFICATION_ARG_KEY, nd.getIntentArgValue());
PendingIntent pendingIntent = PendingIntent
.getActivity(context, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notifManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "notifications_channel";
int ledColor = Color.MAGENTA;
int iconColor = ContextCompat.getColor(context, R.color.colorAccent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = "messages notification channel";
String channelDescription = "This channel is for receive critical notifications";
int channelImportance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel =
new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(true);
notificationChannel.setLightColor(ledColor);
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[] { 100, 200, 300, 100, 300, 200 });
if (notifManager != null)
notifManager.createNotificationChannel(notificationChannel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.app_icon_notification).setContentTitle(nd.getTitle())
.setContentText(nd.getDescription()).setAutoCancel(true).setColor(iconColor)
.setWhen(nd.getWhen() * 1_000).setShowWhen(true).setLights(ledColor, 200, 2000)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setStyle(new NotificationCompat.BigTextStyle().bigText(nd.getDescription()))
.setTicker(nd.getDescription()).setSound(defaultSoundUri)
.setCategory(nd.getCategory())
//.setGroup(nd.getGroup())
.setContentIntent(pendingIntent);
if (notifManager != null) {
FutureTarget<Bitmap> futureTarget =
GlideApp.with(context).asBitmap().load(nd.getLargeImage()).circleCrop().submit();
LoadImageTask task = new LoadImageTask(icon -> {
notificationBuilder.setLargeIcon(icon);
GlideApp.with(context).clear(futureTarget);
notifManager.notify(nd.getTag(), nd.getId(), notificationBuilder.build());
});
task.execute(futureTarget);
}
您使用setWhen()来显示特定的通知日期。 但是Android Oreo错误地显示了将通知重新接收为“通知日期”的日期,而不是setWhen()中指定的日期