android - Android通知中来自外部网址的setSmallIcon

标签 android firebase push-notification notification-icons

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

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

有人知道这是否可行吗?

最佳答案

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

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

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

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

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

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

builder.setSmallIcon(R.drawable.ic_launcher);

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

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

关于android - Android通知中来自外部网址的setSmallIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949620/

相关文章:

java - 如何将 Android 中的音频导入 libPD?

android - Unity Ads 4.0 - 奖励广告的多个 OnUnityAdsShowComplete 回调

angular - 在 angularfire 应用程序中创建时间戳对象

java - 使用 Firebase 数据库跻身最佳玩家前十名

reactjs - FCM 网络推送通知 Google Analytics

push-notification - AWS SNS 终端节点自行禁用

android - Ionic 3 Firebase Google Auth 在 Android 设备上停止工作

android - 我的 Android 应用程序中的电池耗尽问题

android - java.lang.NoSuchMethodError : No static method registerDefaultInstance with Firebase Performance and Espresso Instrumented Tests

ios - 使用 Cordova Push phonegap-plugin-push 1.8.0 的 PhoneGap Windows 构建失败