且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-19 18:54:23

原因:对于 5.0 Lollipop,通知图标必须完全为白色".

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

If we solve the white icon problem by setting target SDK to 20, our app will not target Android Lollipop, which means that we cannot use Lollipop-specific features.

目标 Sdk 21 的解决方案

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

If you want to support Lollipop Material Icons, then make transparent icons for Lollipop and the above version. Please refer to the following: https://design.google.com/icons/

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

Please look at http://developer.android.com/design/style/iconography.html, and we'll see that the white style is how notifications are meant to be displayed in Android Lollipop.

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

In Lollipop, Google also suggests that we use a color that will be displayed behind the white notification icon. Refer to the link: 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)

Wherever we want to add Colors https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setColor(int)

对于低于和高于 Lollipop OS 版本的通知生成器的实现将是:

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 中可用,它只影响图标的背景.

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