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

查看:90
本文介绍了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 来解决白色图标问题,我们的应用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 中可用,它只影响图标的背景.

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

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

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