`NotificationCompat.MessagingStyle(String userDisplayName)` 的构造函数已弃用

问题描述

我想将 MessagingStyle 用于我的聊天通知构建器,但我只有来自发件人的用户名,我阅读了文档并发现我可以使用 MessagingStyle(Person person) 构造函数

如何使用 Person 实例化 MessagingStyle?

val notificationBuilder: NotificationCompat.Builder =
    NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID).apply {
        setShowWhen(false) 
        
        // Set the Notification style
        // setStyle(NotificationCompat.MessagingStyle(Person)) ??
        
        color = Color.WHITE
        setSmallIcon(R.drawable.chat_notif_small_icon)
        setonlyAlertOnce(true)
        setContentIntent(contentPendingIntent)
        setongoing(false)
    }



解决方法

有关详细信息,请查看:https://medium.com/google-developer-experts/exploring-android-p-enhanced-notifications-a9adb8d78387

val sender = Person.Builder()
        .setName(R.string.user_name)
        .build()

然后将其用作

NotificationCompat.MessagingStyle(sender)
        .addMessage("Check this out!",Date().time,sender)
        .setBuilder(notificationBuilder)