我正在尝试在收到新消息时向用户发送通知。
我可以发送通知,但是自定义通知存在一些问题。
这是我用于创建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
所以根本没有内容。
答案 0 :(得分:2)
我想您必须像这样将内容设置为InboxStyle:
notificationBuilder.setStyle(new Notification.InboxStyle()
.setContentTitle("Title")
.addLine("Hello")
.addLine("World")
.setSummaryText("+3 more"))
答案 1 :(得分:1)
您还可以使用 setStyle 方法显示大文本并让Notification 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);