我正在尝试在收到新消息时向用户发送通知。
我可以发送通知,但我在自定义通知时遇到了一些问题。
这是我创建 notificationChannel 的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NotificationName",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("Test channel");
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 2000});
notificationChannel.enableVibration(true);
notificationChannel.setShowBadge(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
这是创建通知的代码:
notificationBuilder.setAutoCancel(true)
.setVibrate((new long[]{0, 1000, 500, 2000}))
.setStyle((new NotificationCompat.InboxStyle() ) )
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("Unread message")
.setContentText("You have an unread message")
.setOnlyAlertOnce(true)
.setContentInfo("Info");
Intent intent = new Intent(this, bildirimDeneme.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(contentIntent);
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
我将内容文本设置为“您有一条未读消息”。但通知显示如下 [1]:https://hizliresim.com/EO6rVB
所以根本就没有内容。
最佳答案
我想您必须像这样将内容设置为 InboxStyle:
notificationBuilder.setStyle(new Notification.InboxStyle()
.setContentTitle("Title")
.addLine("Hello")
.addLine("World")
.setSummaryText("+3 more"))
关于java - 如何设置通知的标题和内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56883210/