【问题标题】:small icon on push notification not showing on android?推送通知上的小图标未在android上显示?
【发布时间】:2021-04-06 15:22:17
【问题描述】:

我在推送通知中添加了小图标,但仍未显示。我正在使用安卓 10 模拟器。我的图标满足使用无背景的白色并存储在 mipmap 文件夹中的要求。我在stackoverflow上尝试了一些解决方案,但仍然无法正常工作。 这是我的代码

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_notification)
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true);

推送通知图标仍然这样显示

我已经尝试在 android manifest 上添加此代码,但仍然无法正常工作

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@mipmap/ic_notification" />

我的 targetSDK 是 29

你有没有遇到过同样的问题并解决了?

【问题讨论】:

  • 您是否尝试过使用drawable文件夹中的其他图片?
  • @SamChen 我试过了,但也没有用
  • @FairuzYassar 我还建议您尝试从drawable 文件夹加载图像。我总是做的是,首先获取一个 .svg 图标,将其作为矢量资源导入到 drawable 文件夹中,这一步的结果将是生成的 .xml 布局文件。最后像这样在NotificationCompat.Builder 中调用这个布局文件.setSmallIcon(R.drawable.ic_yourIcon) 就可以了。
  • @BWappsandmore 我已经尝试过您的解决方案,但通知图标仍然显示如上图的灰色方块
  • @Fairuz Yassar 在互联网上搜索免费的.svg 图标。下载它。在 Android Studio 中,右键单击您的 drawable 文件夹,然后选择 New -&gt; Vector Asset。选择本地文件(SVG、PSD)作为资产类型。选择您下载的图标。从它生成.xml 布局文件后,单击它进行编辑。编辑 android:height="24dp"android:width="24dp" 或调整大小、36dp 或 48dp。您将从编辑器左侧的图标中看到预览。将此文件附加到您的通知中。

标签: android react-native push-notification firebase-cloud-messaging clevertap


【解决方案1】:

操作栏图标要求文件大小为 24×24 像素(基线大小 (MDPI))。但是,图像本身在图标文件中不得超过 22×22 像素居中

图标设计必须是单色白色,#ffffff。

MDPI - 24 x 24  (drawable-mdpi)
HDPI - 36 x 36  (drawable-hdpi)
XHDPI - 48 x 48  (drawable-xhdpi)
XXHDPI - 72 x 72  (drawable-xxhdpi)
XXXHDPI - 96 x 96  (drawable-xxxhdpi)

更多详情请查看Android CheatsheetOfficial Android documentation

【讨论】:

  • 我有所有这个尺寸和图标设计都遵循了规则,但图标仍然没有显示。我使用这个工具创建了图标romannurik.github.io/AndroidAssetStudio/…
  • 对数据消息使用 Drawable 文件夹 => .setSmallIcon(R.drawable.ic_notification) 对于通知消息 => android:resource="@drawable/ic_notification"
猜你喜欢
  • 2021-02-23
  • 1970-01-01
  • 2019-03-31
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多