Android 推送通知:通知中未显示图标,而是显示白色方块

2024-02-16

我的应用程序生成通知,但我为该通知设置的图标未显示。相反,我得到一个白色方块。

我尝试过调整图标的 png 大小(尺寸 720x720、66x66、44x44、22x22)。奇怪的是,当使用较小的尺寸时,白色方块会更小。

我已经用谷歌搜索了这个问题,以及生成通知的正确方法,从我读到的内容来看,我的代码应该是正确的。可悲的是,事情并不像他们应该的那样。

我的手机是 Nexus 5,运行 Android 5.1.1。这个问题也存在于模拟器上,三星 Galaxy s4(带 Android 5.0.1)和摩托罗拉 Moto G(带 Android 5.0.1)(这两个都是我借来的,现在没有)

下面是通知代码和两个屏幕截图。如果您需要更多信息,请随时询问。

谢谢你们。

@SuppressLint("NewApi") private void sendNotification(String msg, String title, String link, Bundle bundle) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
    resultIntent.putExtras(bundle);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    Notification notification;
    Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound);
    notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.lg_logo)
                .setContentTitle(title)
                .setStyle(new Notification.BigTextStyle().bigText(msg))
                .setAutoCancel(true)
                .setContentText(msg)
                .setContentIntent(contentIntent)
                .setSound(sound)
                .build();
    notificationManager.notify(0, notification);
}

原因:对于 5.0 Lollipop,“通知图标必须是全白色的”。

如果我们通过将目标 SDK 设置为 20 来解决白色图标问题,我们的应用程序 不会针对 Android Lollipop,这意味着我们无法使用 Lollipop 特定的功能。

目标Sdk 21的解决方案

如果你想支持Lollipop Material Icons,那么就为Lollipop及以上版本制作透明图标。请参考以下内容:https://design.google.com/icons/ https://design.google.com/icons/

请看http://developer.android.com/design/style/iconography.html http://developer.android.com/design/style/iconography.html,我们将看到白色样式是通知在 Android Lollipop 中的显示方式。

在 Lollipop 中,Google 还建议我们使用显示在白色通知图标后面的颜色。参考链接:https://developer.android.com/about/versions/android-5.0-changes.html https://developer.android.com/about/versions/android-5.0-changes.html

无论我们想在哪里添加颜色https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setColor(int) https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setColor(int)

以下和以上 Lollipop 操作系统版本的通知生成器的实现如下:

Notification notification = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    notification.setSmallIcon(R.drawable.icon_transperent);
    notification.setColor(getResources().getColor(R.color.notification_color));
} else { 
    notification.setSmallIcon(R.drawable.icon);
} 

注意:setColor 仅在 Lollipop 中可用,并且仅影响图标的背景。

它将彻底解决你的问题!!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 推送通知:通知中未显示图标,而是显示白色方块 的相关文章

随机推荐