Java 如何设置标题和标题;通知的内容?
我正在尝试在新消息到达时向用户发送通知 我可以发送通知,但在自定义通知时遇到一些问题 以下是我创建notificationChannel的代码:Java 如何设置标题和标题;通知的内容?,java,android,android-notifications,Java,Android,Android Notifications,我正在尝试在新消息到达时向用户发送通知 我可以发送通知,但在自定义通知时遇到一些问题 以下是我创建notificationChannel的代码: if(Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.O){ NotificationChannel NotificationChannel=新的NotificationChannel(通知通道ID,“NotificationName”, 通知经理(重要性高); notificationChannel.se
if(Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.O){
NotificationChannel NotificationChannel=新的NotificationChannel(通知通道ID,“NotificationName”,
通知经理(重要性高);
notificationChannel.setDescription(“测试通道”);
notificationChannel.setVibrationPattern(新的长[]{010005002000});
通知通道。启用振动(真);
notificationChannel.setShowBadge(真);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(此为通知通道ID);
以下是创建通知的代码:
notificationBuilder.setAutoCancel(真)
.SetVimble((新长[]{0,1000,500,2000}))
.setStyle((新的NotificationCompat.InboxStyle()))
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_通知)
.setContentTitle(“未读邮件”)
.setContentText(“您有一封未读的邮件”)
.setonlylertonce(真)
.setContentInfo(“信息”);
意向意向=新意向(此,bildirimDeneme.class);
PendingEvent contentIntent=PendingEvent.getActivity(this,0,intent,PendingEvent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(contentIntent);
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
我正在将内容文本设置为“您有未读邮件”。但通知如下所示[1]:
所以根本就没有内容 我想您必须将内容设置为Inbox样式,如下所示:
notificationBuilder.setStyle(new Notification.InboxStyle()
.setContentTitle("Title")
.addLine("Hello")
.addLine("World")
.setSummaryText("+3 more"))
您还可以“玩”显示大文本的setStyle方法,并让通知API完成这项工作
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_cloud_upload)
.setContentTitle(context.getString(R.string.notif_title))
.setContentText(context.getString(R.string.notif_subject))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(*your_message*))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(new long[0])
.setAutoCancel(true);
使用notitification样式谢谢,但是你能告诉我应该选择哪种样式吗?我读了链接上的页面,但无论我使用哪种样式,它看起来都有标题和内容。我尝试使用Notification.BigTextStyle,但没有什么不同。