如何在Android通知内容中显示图标和文本

3

我需要在我的 Android 应用中显示通知。

我正在使用以下代码:

NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(baseContext).setLargeIcon(large_icon)
                .setSmallIcon(R.drawable.message_received_small_icon)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setSound(soundUri);
                .setContentTitle("New Message");
                .setContentText("video");

这个通知显示在左侧的“消息接收”图标。在接收信息图标的右侧,它以“DHan Nexus”为标题,并在其下方显示“照片”。

但是我想显示相机图标+“照片”字符串,而不仅仅是显示“照片”字符串。我没有找到在setContentText() API中显示相机图标的方法,请问如何实现?我需要制作一个自定义布局吗,还是默认方法就可以?

以下是图片,使问题更加明确:

enter image description here

我尝试使用SpannableString来显示图标和文本,但似乎不起作用。

CharSequence cs = getContentIcon(text);
.setContentText(cs.toString());
    private CharSequence getContentIcon(String text)
    {
        Drawable image = ContextCompat.getDrawable(baseContext, R.drawable.camera_icon);

        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
        // Replace blank spaces with image icon
        SpannableString sb = new SpannableString("                      "+text);
        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BASELINE);
        sb.setSpan(imageSpan, 0, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return sb;
    }

1
创建自定义布局以用于通知。 - Vivek Mishra
我建议你试试PugNotification(http://www.halysongoncalves.com/Pugnotification/) - Shai Levy
2个回答

1
你可以使用自定义布局与RemoteViews
请注意,Android 7 Nougat在通知方面有一些更改(我还没有深入研究)。以下代码适用于Nougat以下版本。
// for standard notification
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_simple);

// for expanded notification
// RemoteViews bigViews = new RemoteViews(getPackageName(), R.layout.notification_expanded);

views.setImageViewBitmap(R.id.picture, YOUR_IMAGE_BITMAP_HERE);
views.setTextViewText(R.id.text, YOUR_TEXT_HERE);

NotificationCompat.Builder notificationBuilder = 
        new NotificationCompat.Builder(this)
                .setSmallIcon(YOUR_NOTIFICATION_ICON)
                .setCustomContentView(views);
                // .setCustomBigContentView(bigViews); // if you've set it

notification = notificationBuilder.build();

0

这里我写了答案:https://stackoverflow.com/a/69086821/4498813 在这里重复一遍:

要显示更大的图片/图标,您需要设置自定义远程视图并自定义您的layout.xml。

要添加小的Emoji图标,您只需将图标的Unicode添加到字符串中。 要查找Emoji图标,请访问此处:https://emojipedia.org/google/

示例:

NotificationCompat.Builder(context, channelId)
        .setContentTitle("Your Title")
        .setContentText("\uD83D\uDCC4  -> This is Page Facing Up emoji icon")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接