如何设置通知的标题和内容?
How to set Title & Content of Notification?
我正在尝试在收到新消息时向用户发送通知。
我可以发送通知,但我在自定义通知时遇到了一些问题。
这是我创建 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());
我将内容文本设置为 "You have an unread message"。但通知显示如下 [1]:https://hizliresim.com/EO6rVB
所以完全没有内容。
我猜你必须像这样将内容设置为 InboxStyle:
notificationBuilder.setStyle(new Notification.InboxStyle()
.setContentTitle("Title")
.addLine("Hello")
.addLine("World")
.setSummaryText("+3 more"))
您也可以 "play" 使用 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);
我正在尝试在收到新消息时向用户发送通知。
我可以发送通知,但我在自定义通知时遇到了一些问题。
这是我创建 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());
我将内容文本设置为 "You have an unread message"。但通知显示如下 [1]:https://hizliresim.com/EO6rVB
所以完全没有内容。
我猜你必须像这样将内容设置为 InboxStyle:
notificationBuilder.setStyle(new Notification.InboxStyle()
.setContentTitle("Title")
.addLine("Hello")
.addLine("World")
.setSummaryText("+3 more"))
您也可以 "play" 使用 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);