Android 通知多行
如何使长通知多行。我正在使用以下代码段,但它不起作用:Android 通知多行,android,notifications,Android,Notifications,如何使长通知多行。我正在使用以下代码段,但它不起作用: NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setContentTitle(title) .setSmallIcon(R.drawable.icon) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) .setContentText
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setSmallIcon(R.drawable.icon)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setContentIntent(pIntent);
return mBuilder.build();
添加以下内容:
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
Notification notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message).build();
notificationManager.notify(0, notification);
另请检查:尝试使用android Oreo通知频道id执行此代码
public static int id = 0;
private void sendNotification(String messageBody) {
Log.d("FCMID","id : " + id);
Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,PendingIntent.FLAG_ONE_SHOT);
String channelId = "1";
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentTitle("Piml Sid")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Spike Bot",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
id ++;
notificationManager.notify(id /* ID of notification */, notificationBuilder.build());
}
在
通知compat.Builder
中添加
.setStyle(NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body)
alnog with
.setContentText(body)
您针对的是哪个sdk版本?4.1+操作系统支持多行通知。查看我提供的答案部分中的链接。工作,这里的详细信息:似乎链接已关闭。事情是这样的:你能解释一下你添加了哪些行吗?首先,行的顺序与问题中的顺序不同,其次,您可以使用英语解释您的解决方案,而不仅仅是编写未格式化的无序代码。setStyle(new Notification.BigTextStyle()
的代码必须是setStyle(new NotificationCompat.BigTextStyle())
。否则它会抛出一个错误,因为BigTextStyle无法转换为Style
。setStyle被调用了两次。这是错误的吗?如果不是,这是如何工作的?不能使用BigTextStyle
两次,第二次将覆盖第一次。标题、上下文、BigText这些可以是不同的值。如果不确定,则显示大文本n被展开或用户自己展开
public static int id = 0;
private void sendNotification(String messageBody) {
Log.d("FCMID","id : " + id);
Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,PendingIntent.FLAG_ONE_SHOT);
String channelId = "1";
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentTitle("Piml Sid")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Spike Bot",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
id ++;
notificationManager.notify(id /* ID of notification */, notificationBuilder.build());
}
.setStyle(NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body)