安卓通知图标是一个白色圆形。

12

今天我的通知图标出现了奇怪的问题。

它看起来像这样: enter image description here (白色圆圈...)

我做错了什么吗?

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);

这是我的图标图片(从这里刚刚下载 https://material.io/icons/#ic_photo): http://image.noelshack.com/fichiers/2016/44/1478185219-icon-notification.png

我漏掉了什么吗?

补充一下,我正在使用SDK 24,目前只为hdpi资源文件夹创建了一个。

编辑#1:我添加了ldpimdpixhdpi图标,但没有改变……

编辑#2:更准确地说,我正在尝试从服务中创建此通知...... FCM消息服务...


1
这个回答解决了您的问题吗?Android通知具有彩色图标,而不是变成白色 - mage1k99
6个回答

9

小细节:有些厂商不会将图标变暗。例如,搭载Android 8的三星Galaxy S7并不会将通知图标变暗。 - Lingviston
“dim” 是什么意思? - Simon

6

您必须使用没有背景的通知图标。Android将添加圆形背景。

您可以使用

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

设置背景颜色以匹配您的应用程序身份。

内部的图标仍然是白色的,圆圈会获得您定义的颜色。

On Android Studio On system bar On notification


3

看起来是编译过程中的缓存问题...我一开始使用的图片很糟糕(全部着色),所以我认为我的编译器在文件名上创建了某种缓存。

我在Windows上工作,做了以下操作:从手机上卸载应用程序,从Android Studio中使所有缓存失效 => 重新编译后,图标就正常了。


1

当应用程序关闭时,我遇到了同样的问题,这个帮助了我

不幸的是,Firebase通知在SDK 9.0.0-9.6.1中存在限制。当应用程序在后台运行时,从控制台发送的消息将使用清单中的启动器图标(带有必要的Android色调)。

然而,在SDK 9.8.0中,您可以覆盖默认设置!在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" />

1

您需要生成一个单独的图标,它将是您启动器图标的白色版本。您可以使用下面的链接来生成这样的图标。

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

注意:您需要上传具有透明背景的启动器图标的PNG图像。
设置图标,您可以使用以下方法之一。
private int getSmallIconForNotification(){
    return (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP)? R.mipmap.ic_stat_launcher : R.mipmap.ic_launcher;
}

代码使用:

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

1
谢谢,这对我有用,我认为它应该被接受为答案。 - Web.11

0

注意:- 如果设备的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)......

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接