折叠时的Android多行通知

折叠时的Android多行通知,android,notifications,android-notifications,android-appcompat,androidx,Android,Notifications,Android Notifications,Android Appcompat,Androidx,我希望通过android中的通知实现以下功能: 当通知已折叠时 当通知展开时 我试着用和做这件事 但在这两种情况下,我都得到了一个多行文本,而它只是展开的。没有扩展的\n将被空格所取代。如何在未展开的情况下实现多行文字 还是自定义布局通知 提前谢谢 这可以通过使用远程视图布局来实现 val notificationLayout = RemoteViews(packageName, R.layout.notification_small) NotificationCompat.Builder(

我希望通过android中的通知实现以下功能:

当通知已折叠时

当通知展开时

我试着用和做这件事

但在这两种情况下,我都得到了一个多行文本,而它只是展开的。没有扩展的
\n
将被
空格所取代。如何在未展开的情况下实现多行文字

还是自定义布局通知


提前谢谢

这可以通过使用远程视图布局来实现

val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_notification_overlay)
            .setContentTitle("Multiline Notification")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCustomContentView(notificationLayout)
这是我的自定义布局文件(notification_small.xml):


这可以通过使用远程视图布局来实现

val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_notification_overlay)
            .setContentTitle("Multiline Notification")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCustomContentView(notificationLayout)
这是我的自定义布局文件(notification_small.xml):


您可以使用MessagingStyle。例如:

 public static void notify(final Context context, List<Message> newMessages) {
    Person person = new Person.Builder().setName("Name").build(); // You can also set Person's icon here
    NotificationCompat.MessagingStyle messageStyle = new NotificationCompat.MessagingStyle(person);
    for(Message message : newMessages){
        messageStyle.addMessage(message.text, message.time.getTime(), person);
    }

    String notificationTitle = newMessages.size() + " new messages";
    messageStyle.setConversationTitle(notificationTitle); // Then add the style to the notification builder.
publicstaticvoidnotify(最终上下文,列出新消息){
Person Person=new Person.Builder().setName(“Name”).build();//您也可以在此处设置Person的图标
NotificationCompat.MessagingStyle messageStyle=新NotificationCompat.MessagingStyle(个人);
用于(消息:newMessages){
messageStyle.addMessage(message.text,message.time.getTime(),person);
}
String notificationTitle=newMessages.size()+“新邮件”;
messageStyle.setConversationTitle(notificationTitle);//然后将样式添加到通知生成器中。

您可以使用MessagingStyle。例如:

 public static void notify(final Context context, List<Message> newMessages) {
    Person person = new Person.Builder().setName("Name").build(); // You can also set Person's icon here
    NotificationCompat.MessagingStyle messageStyle = new NotificationCompat.MessagingStyle(person);
    for(Message message : newMessages){
        messageStyle.addMessage(message.text, message.time.getTime(), person);
    }

    String notificationTitle = newMessages.size() + " new messages";
    messageStyle.setConversationTitle(notificationTitle); // Then add the style to the notification builder.
publicstaticvoidnotify(最终上下文,列出新消息){
Person Person=new Person.Builder().setName(“Name”).build();//您也可以在此处设置Person的图标
NotificationCompat.MessagingStyle messageStyle=新NotificationCompat.MessagingStyle(个人);
用于(消息:newMessages){
messageStyle.addMessage(message.text,message.time.getTime(),person);
}
String notificationTitle=newMessages.size()+“新邮件”;
messageStyle.setConversationTitle(notificationTitle);//然后将样式添加到通知生成器中。

是实现此效果的答案。您是否尝试过System.getProperty(“line.separator”)而不是\n?是的,但当通知折叠时它不起作用。请检查此-是实现此效果的答案。您尝试过System.getProperty(“line.separator”)吗而不是\n?是,但当通知折叠时它不工作。请检查此项-