【问题标题】:Android multiline notification when collapsed折叠时的Android多行通知
【发布时间】:2020-04-08 14:12:35
【问题描述】:

我想在android中通过通知实现以下功能:

当通知折叠

当通知展开

我试着用Notification.InboxStyleNotification.BigTextStyle 来做。

但是在它们中,我只在扩展时得到一个多行文本。没有扩展的\n 将被space 取代。不展开时如何实现多行文字?

还是自定义布局通知?

提前致谢!

【问题讨论】:

标签: android notifications android-notifications android-appcompat androidx


【解决方案1】:

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

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):

<?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>

【讨论】:

    【解决方案2】:

    您使用 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      相关资源
      最近更新 更多