Android многострочное уведомление при свертывании - PullRequest
1 голос
/ 08 апреля 2020

Я хочу получить следующие функции с уведомлениями в android:

Когда уведомление свернуто

enter image description here

Когда уведомление расширено

enter image description here

Я попытался сделать это с Notification.InboxStyle и Notification.BigTextStyle .

Но в обоих из них я получаю многострочный текст, когда он только развернут. Без расширения \n заменяется на space. Как добиться многострочного текста, когда он не развернут?

Или это уведомление о пользовательском макете?

Заранее спасибо!

Ответы [ 2 ]

0 голосов
/ 20 апреля 2020

Вы используете 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.
0 голосов
/ 19 апреля 2020

Этого можно добиться с помощью макетов удаленного просмотра.

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)

Вот мой файл пользовательского макета (messages_small. xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 1 \n Test2  \n Test 3" />

</LinearLayout>
...