简体   繁体   English

Android 通知图标是一个白色圆圈

[英]Android notification icon is a white circle

I've got a strange problem with notification icon today.我今天的通知图标有一个奇怪的问题。

It looks like this :它看起来像这样: 在此处输入图片说明 (the white circle ...) (白色圆圈...)

Did I do something bad ?我做错了什么吗?

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon_notification)
                .setContentTitle(this.getString(R.string.notification_title))
                .setContentText(this.getString(R.string.notification_text))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

Here is my icon image (freshly downloaded from here https://material.io/icons/#ic_photo ) : http://image.noelshack.com/fichiers/2016/44/1478185219-icon-notification.png这是我的图标图像(从这里新下载的https://material.io/icons/#ic_photo ): http : //image.noelshack.com/fichiers/2016/44/1478185219-icon-notification.png

Did I miss something ?我错过了什么吗?

For the record, I'm using SDK 24 and only created the hdpi resource folder for now.作为记录,我使用的是 SDK 24 并且现在只创建了hdpi资源文件夹。

Edit #1 : I've added the ldpi , mdpi and xhdpi icons, nothing change ...编辑 #1:我添加了ldpimdpixhdpi图标,没有任何变化......

Edit #2 : For more precision, I'm trying to create this notification from a service ... FCM messaging service ...编辑 #2:为了更精确,我正在尝试从服务创建此通知... FCM 消息服务...

If your compileSDKversion is above 20 then notification icon should be a white-on-transparent background image.如果您的 compileSDKversion 高于 20,则通知图标应该是透明背景上的白色图像。 Otherwise the image will be rendered as a white colored image.否则图像将呈现为白色图像。

Please go through the below link too for guidelines to create the icon请也通过以下链接获取创建图标的指南

https://www.google.com/design/spec/patterns/notifications.html https://www.google.com/design/spec/patterns/notifications.html

and also the notification icon generator.以及通知图标生成器。

https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.space.trim=1&source.space.pad=0&name=ic_stat_example https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.space.trim=1&source.space.pad=0&name=ic_stat_example

You must use a notification icon with no background.您必须使用没有背景的通知图标。 Android will add the circle background. Android 将添加圆形背景。

You can set background color with您可以设置背景颜色

.setColor(context.getResources().getColor(R.color.colorPrimary))

to match your app indentity.以匹配您的应用程序身份。

Icon inside will remain white and circle will get the color you defined.里面的图标将保持白色,圆圈将获得您定义的颜色。

在 Android Studio 上 在系统栏 通知时

It seems to be a problem of cache during compilation ... The first image I was using was bad (fully colored), so I think my compilator created somekind of cache on the filename.这似乎是编译过程中的缓存问题......我使用的第一个图像很糟糕(全彩色),所以我认为我的编译器在文件名上创建了某种缓存。

I work on Windows and did this : uninstall the app from my phone, invalidate all cache from Android sudio => at re-compilation, the icon was OK.我在 Windows 上工作并这样做:从我的手机上卸载应用程序,使 Android sudio 中的所有缓存无效 => 在重新编译时,图标正常。

U need to have separate icon generated which will be white version of your launcher icon.您需要生成单独的图标,这将是您的启动器图标的白色版本。 U can use below link to generate such icon.你可以使用下面的链接来生成这样的图标。

https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit

Note : You need to upload PNG image of your launcher icon with transparent background.注意:您需要上传具有透明背景的启动器图标的 PNG 图像。

For setting icon u can have a method like this对于设置图标,你可以有这样的方法

private int getSmallIconForNotification(){
    return (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP)? R.mipmap.ic_stat_launcher : R.mipmap.ic_launcher;
}

Code usage:代码用法:

private NotificationCompat.Builder createNotificationBuilder(){
    return new NotificationCompat.Builder(this)
            .setSmallIcon(getSmallIconForNotification())
            .setContentTitle("New Message")
            .setContentText("Hi there.....")
            .setAutoCancel(true);
}

Note:- If device has android version above 20 you have to generate icon with transparent background and while generating notification use this snippet注意:- 如果设备的 android 版本高于 20,则必须生成具有透明背景的图标,并在生成通知时使用此代码段

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
        currentapiVersion=R.mipmap.ic_notification_lolipop;
} else{
        currentapiVersion=R.mipmap.ic_launcher;
}

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(currentapiVersion)......

I had the same problem while the app is closed and this helped me应用程序关闭时我遇到了同样的问题, 这对我有帮助

Unfortunately this was a limitation of Firebase Notifications in SDK 9.0.0-9.6.1.不幸的是,这是 SDK 9.0.0-9.6.1 中 Firebase 通知的限制。 When the app is in the background the launcher icon is use from the manifest (with the requisite Android tinting) for messages sent from the console.当应用程序在后台时,启动器图标从清单(带有必要的 Android 着色)用于从控制台发送的消息。

With SDK 9.8.0 however, you can override the default!但是,使用 SDK 9.8.0,您可以覆盖默认值! In your AndroidManifest.xml you can set the following fields to customise the icon and color:在您的 AndroidManifest.xml 中,您可以设置以下字段来自定义图标和颜色:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/notification_icon" />
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/google_blue" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM