我的应用会生成通知,但我没有显示为该通知设置的图标 . 相反,我得到一个白色方块 .
我已经尝试调整图标的大小(尺寸720x720,66x66,44x44,22x22) . 奇怪的是,当使用较小尺寸时,白色方块较小 .
我已经google了这个问题,以及生成通知的正确方法,从我读过的代码应该是正确的 . 可悲的是,事情并不像他们应该的那样 .
我的手机是带有Android 5.1.1的Nexus 5 . 问题还出现在模拟器,带有Android 5.0.1的三星Galaxy s4和带有Android 5.0.1的摩托罗拉Moto G上(我借用了这两款,现在还没有)
接下来是通知代码和两个屏幕截图 . 如果您需要更多信息,请随时提出要求 .
谢谢你们 .
@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);
}
16 回答
解决此问题的要求:
图像格式:32位PNG(带alpha)
图像应该是透明的
透明度颜色索引:白色(FFFFFF)
资料来源:http://gr1350.blogspot.com/2017/01/problem-with-setsmallicon.html
我找到了一个链接,我们可以生成自己的白色图标,
打开此Link并上传您的ic_launcher或通知图标
您可以为不同版本使用不同的图标 . 只需在您的图标上设置逻辑,如下所示:
Cause: For 5.0 Lollipop "Notification icons must be entirely white".
Solution for target Sdk 21
如果您想支持棒棒糖素材图标,请为Lollipop及以上版本制作透明图标 . 请参考以下内容:https://design.google.com/icons/
请查看http://developer.android.com/design/style/iconography.html,我们会看到白色样式是Android Lollipop中显示通知的方式 .
在Lollipop中,Google还建议我们使用将在白色通知图标后面显示的颜色 . 参考链接: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)
Implementation of Notification Builder for below and above Lollipop OS version would be:
Note: setColor is only available in Lollipop and it only affect to background of the icon.
It will solve your problem completely!!
如果您使用的是Google Cloud 消息传递,则只需更改图标即可解决问题 will not be solved . 例如,这不起作用:
Even if ic_notification是透明和白色的 . 它必须也在Manifest元数据中定义,如下所示:
元数据位于
application
标记下,供参考 .我真的建议遵循Google的设计指南:
其中说 "Notification icons must be entirely white."
在Android Manifest中声明此代码:
我希望这对你有用 .
我们可以这样做:
创建通知构建器的新对象,并使用通知构建器对象调用
setSmallIcon()
,如下面的代码所示 .创建一个方法,我们将检查我们正在安装应用程序的操作系统版本 . 如果它低于Lolipop即API 21,那么它将采用具有背景颜色的普通应用程序图标,否则它将采用没有任何背景的透明应用程序图标 . 因此,使用os版本> = 21的设备将使用Notification builder类的方法
setColor()
设置图标的背景颜色 .Sample Code:
最后我得到了解决这个问题的方法 .
This issue occurs only when the app is not at all running. (neither in the background nor in the foreground) . 当 app runs on either foreground or background the notification icon is displayed properly . (不是白色方块)
So what we've to set is the same configuration for notification icon in Backend APIs as that of Frontend.
在前端,我们使用了 React Native ,对于推送通知,我们使用了react-native-fcm npm package .
我们使用fcm-push npm package使用 Node.js 作为推送通知的后端,并按如下方式设置有效负载结构 .
什么它基本上搜索我们的Android系统本地存储的notification_icon图像 .
在应用程序块的manifest.xml文件中添加此行
如果您想提供棒棒糖支持通知图标,请制作两个类型的通知图标:
带有透明背景的
现在,根据操作系统版本在运行时将相应的图标设置为通知构建器:
试试这个
我面临同样的问题,我尝试了很多回答,但没有得到任何解决方案,最后我找到了解决我的问题的方法 .
- 制作带有透明背景的通知图标 . 应用程序的宽度和高度必须类似于以下尺寸,并将所有这些粘贴到您的项目中 - > app-> src-> main-> res
MDPI 24 * 24
HDPI 36 * 36
XHDPI 48 * 48
XXHDPI 72 * 72
在上面粘贴了下面的onMessageReceived方法中的这一行
不要忘记在清单文件中添加此代码
通知是 greyscale ,如下所述 . 尽管别人写过,但它们并非黑白分明 . 您可能已经看到了具有多种阴影的图标,例如网络强度条 .
在API 21(Lollipop 5.0)之前,颜色图标有效 . 您可以强制您的应用程序进行定位API 20,但限制了应用程序可用的功能,因此不建议这样做 . 您可以测试正在运行的API级别并适当地设置颜色图标或灰度图标,但这可能不值得 . 在大多数情况下,最好使用灰度图标 .
图像有四个通道,RGBA(红色/绿色/蓝色/ alpha) . 对于通知图标,Android会忽略R,G和B通道 . 唯一重要的通道是Alpha,也称为不透明度 . 使用编辑器设计图标,使您可以控制绘图颜色的Alpha值 .
How Alpha values generate a greyscale image:
Alpha = 0(透明) - 这些像素是透明的,显示背景颜色 .
Alpha = 255(不透明) - 这些像素为白色 .
Alpha = 1 ... 254 - 这些像素正是您所期望的,提供透明和白色之间的阴影 .
Changing it up with setColor:
NotificationCompat.Builder.setColor(int argb)
. 从Notification.color
的文档:我使用setColor进行的测试显示Alpha组件被 not 忽略 . 较高的Alpha值会使像素变为白色 . 较低的Alpha值会将一个像素转换为通知区域中的背景颜色(我的设备上为黑色),或者为下拉通知中的指定颜色 .
对于SDK> = 23,请添加setLargeIcon
When you want keep colorful icon - Workaround
将颜色略有不同的像素添加到图标中 .
在我的情况下,有一个带有阴影和光线的黑色图标 . 当添加深蓝色像素时,它可以工作 .
我在android 8.0上有类似的问题 . 尝试使用WHITE图标资源 . 当我尝试使用彩色图像作为图标时,我有白色方块,当我将其替换为白色图标时,它开始工作 .