微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决`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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。