android - 将联系人图像添加到 MessagingStyle 通知“人员”
问题描述
我希望我的通知包含联系人的图像。我正在使用 NotificationCompat 和 MessagingStyle 确实显示联系人的聊天消息。
这在对人使用 .setIcon 时有效,但图像是方形的,所以我想使用 setUri 但它不显示图像,而是在圆圈中看到联系人的第一个首字母。
这是我用于通知的代码。
Person person = new Person.Builder()
// .setIcon(contactIcon)
.setName(displayName)
.setUri(contentLookupURI)
.setKey(contactDetail)
.build();
NotificationCompat.MessagingStyle chatMessageStyle = new NotificationCompat.MessagingStyle(person);
NotificationCompat.MessagingStyle.Message notificationMessage = new
NotificationCompat.MessagingStyle.Message(
contentText,
System.currentTimeMillis(),
person
);
chatMessageStyle.addMessage(notificationMessage);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_MESSAGES_CHANNEL_ID)
.setSmallIcon(icon) // notification icon
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(chatMessageStyle)
.setOnlyAlertOnce(true)
.setContentTitle(displayName);
.setUri
对于我使用库中使用的联系人 URI来获取联系人 ID 和查找键,然后使用以下方法:
contactLookupURI =
ContactsContract.Contacts.getLookupUri(contactsList.get(0).getId(),
contactsList.get(0).getLookupKey());
这产生了这个:content://com.android.contacts/contacts/lookup/3118r4-2F294339313F5B16.3789r5-2F294339313F5B16/6
这对我来说看起来是正确的。
我不确定 MessagingStyle 方法的这种形式有多新,因为我找不到很多与 Person 对象一起使用的示例。
解决方案
这是使用带有 Glide 的远程 URL 的解决方案
第 1 步:从远程 URL 获取位图
private fun getAvatarFromUrl(photoUrl: String): Bitmap? {
return try {
val futureTarget = Glide.with(this)
.asBitmap()
.load(photoUrl)
.submit()
futureTarget.get()
} catch (e: Exception) {
Timber.e(e)
null
}
}
第 2 步:将位图设置为 Person.Icon
val avatar = getAvatarFromUrl(user.avatar)
val sender = Person.Builder()
.setName(msg.from?.name)
.setIcon(IconCompat.createWithBitmap(avatar))
.setImportant(true)
.build()
推荐阅读
- python - factory-boy:将“size”即时传递给 RelatedFactoryList
- php - 如何使用 jQuery 将 HTML/PHP 数据插入 MySQL 数据库
- linux - 我想从我的 Linux 机器上删除任何本地安装的包并直接使用环境中的包
- java - “无法解析 com.google.protobuf:protoc:4.0.0-rc-2”尝试使用 Protobuf Gradle 插件
- apache-spark - 是否可以在不停止 SparkContext 的情况下刷新和上传 Spark 事件日志?
- google-apps-script - GoogleJsonResponseException:对 drive.files.insert 的 API 调用失败并出现错误:错误请求
- java - 异常 NoClassDefFoundError: org/jsoup/Jsoup
- python - 从python列表项中提取文本
- android - 我无法在 android studio 上的 android 应用程序上更改文本
- python - reticulate 在 Linux 上找不到 conda env