Android 推送通知:图标未显示在通知中,而是显示白色方块 [英] Android Push Notifications: Icon not displaying in notification, white square shown instead

查看:23
本文介绍了Android 推送通知:图标未显示在通知中,而是显示白色方块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用生成了一个通知,但我为该通知设置的图标没有显示.相反,我得到了一个白色方块.

My app generates a notification, but the icon I set for that notification is not being displayed. Instead, I get a white square.

我尝试调整图标的 png 大小(尺寸 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.这个问题也存在于模拟器上,一台运行 Android 5.0.1 的三星 Galaxy s4 和一台运行 Android 5.0.1 的摩托罗拉 Moto G(这两个都是我借来的,现在没有)

My phone is a Nexus 5 with Android 5.1.1. The 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)

通知代码如下,还有两张截图.如果您需要更多信息,请随时索取.

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

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

这篇关于Android 推送通知:图标未显示在通知中,而是显示白色方块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆