Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将联系人图像添加到MessagingStyle通知';人';_Android_Notifications - Fatal编程技术网

Android 将联系人图像添加到MessagingStyle通知';人';

Android 将联系人图像添加到MessagingStyle通知';人';,android,notifications,Android,Notifications,我希望我的通知包括联系人的图像。 我正在使用NotificationCompat和MessagingStyle显示联系人的聊天信息 当在人身上使用.setIcon时,这是有效的,但是图像是方形的,所以我想使用setUri,但它不显示图像,而是看到联系人在圆圈中的第一个首字母 这是我用于通知的代码 Person person = new Person.Builder() // .setIcon(contactIcon) .setName

我希望我的通知包括联系人的图像。 我正在使用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()

如果所有联系人都没有imagelol@ManojPerumarath,我检查过了,他们确实有图像,当我将setIcon与联系人照片URI一起使用时,它会起作用,我希望这就是你的意思。你有没有对其进行排序?@Bink没有,我最终使用RoundedBitmapperDrawable手动生成和设置位图