且构网

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

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

更新时间:2023-01-24 16:26:10

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

如果我们通过将目标 SDK 设置为 20 来解决白色图标问题,我们的应用will not target Android 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 Builder 的实现将是:

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

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