Android推送通知:图标未显示在通知中,而是显示白色方块
我的应用会生成通知,但我没有显示为该通知设置的图标。相反,我得到一个白色方块。
My app generates a notification, but the icon I set for that notification is not being displayed. Instead I get a white square.
我已经尝试调整图标的大小(尺寸720x720,66x66,44x44,22x22)。奇怪的是,当使用较小的尺寸时,白色方块较小。
I have tried resizing the png of the icon (dimensions 720x720, 66x66, 44x44, 22x22). Curiously, when using smaller dimensions the white square is smaller.
我已经搜索了这个问题,以及生成通知的正确方法,以及我从中读过的内容我的代码应该是正确的。可悲的是事情并非如此。
I have googled this problem, as well as the correct way of generating notifications, and from what I've read my code should be correct. Sadly things are not as they should be.
我的手机是带有Android 5.1.1的Nexus 5。模拟器,三星Galaxy s4和Android 5.0.1以及摩托罗拉Moto G与Android 5.0.1(我借用的,现在都没有)的问题也出现了问题。
My phone is a Nexus 5 with Android 5.1.1. Problem is also present on emulators, a Samsung Galaxy s4 with Android 5.0.1 and a Motorola Moto G with Android 5.0.1 (both of which I borrowed, and don't have right now)
通知代码如下,以及两个屏幕截图。如果您需要更多信息,请随时索取。
Code for notifications follows, and two screenshots. If you require more information, please feel free to ask for it.
谢谢大家。
@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特定功能。
If we solve 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材质图标,请为Lollipop及以上版本制作透明图标。请参阅以下内容:
https://design.google.com/icons/
If you want to support Lollipop Material Icons then make transparent icons for Lollipop and above version. Please refer 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 suggest that we use a color that will be displayed behind the white notification icon. Refer Link: https://developer.android.com/about/versions/android-5.0-changes.html
Wherever we want to add Colors https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setColor(int)
以下Lollipop操作系统版本的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中可用且仅影响到图标的背景。
它将彻底解决您的问题!!