繁体   English   中英

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

[英]Android notification icon is a white circle

我今天的通知图标有一个奇怪的问题。

它看起来像这样: 在此处输入图片说明 (白色圆圈...)

我做错了什么吗?

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 消息服务...

如果您的 compileSDKversion 高于 20,则通知图标应该是透明背景上的白色图像。 否则图像将呈现为白色图像。

请也通过以下链接获取创建图标的指南

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

以及通知图标生成器。

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

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

您可以设置背景颜色

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

以匹配您的应用程序身份。

里面的图标将保持白色,圆圈将获得您定义的颜色。

在 Android Studio 上 在系统栏 通知时

这似乎是编译过程中的缓存问题......我使用的第一个图像很糟糕(全彩色),所以我认为我的编译器在文件名上创建了某种缓存。

我在 Windows 上工作并这样做:从我的手机上卸载应用程序,使 Android sudio 中的所有缓存无效 => 在重新编译时,图标正常。

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

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

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

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

不幸的是,这是 SDK 9.0.0-9.6.1 中 Firebase 通知的限制。 当应用程序在后台时,启动器图标从清单(带有必要的 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" />

暂无
暂无

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

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