【问题标题】:setSmallIcon from external url in Android notification来自 Android 通知中外部 url 的 setSmallIcon
【发布时间】:2017-12-23 03:58:48
【问题描述】:

我需要通过 PNG 从外部 URL 设置小的 Android 通知图标,而不是使用 mipmap。

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        notificationBuilder.setSmallIcon(R.mipmap.ic_notification);
        notificationBuilder.setColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));

    }

需要传递外部PNG图片而不是使用:R.mipmap.ic_notification

有谁知道这是否可行?

【问题讨论】:

  • 我认为直接使用 Builder 是不可能的。它只需要int 参数,即资源的id

标签: android firebase push-notification notification-icons


【解决方案1】:

如果您阅读特定于 Notification.Builder 的开发人员文档,您会看到 setSmallIcon(int icon) 需要在应用程序的可绘制对象包中使用 A 资源 ID。

下载图像,转换为位图,然后将其设置为setSmallIcon(int resId) 仍然会给您一个错误。

即使您要像这样将 Bitmap 转换为 Drawable:

Drawable d = new BitmapDrawable(getResources(), bmpFinal);

它仍然会给你一个错误,因为你的应用程序包中不存在 Drawable。

唯一可能的解决方案是使用包中存在的可绘制资源并将其设置为setSmallIcon() 方法。典型用法:

builder.setSmallIcon(R.drawable.ic_launcher);

或者,setLargeIcon (Bitmap icon) 需要一个位图实例。无需对您当前的代码进行任何额外的更改(因为您已经有一个位图),如果它符合您的要求,您可以按原样使用它。

如果没有,您几乎必须使用已存在于可绘制文件夹之一中的 Drawable 资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多