【问题标题】:Android notification icon is a white circleAndroid 通知图标是一个白色圆圈
【发布时间】:2017-03-17 05:44:39
【问题描述】:

我今天遇到了一个奇怪的通知图标问题。

看起来像这样: (白圈……)

我是不是做错了什么?

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

【问题讨论】:

标签: android notifications icons


【解决方案1】:

如果您的 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 8 的三星 Galaxy S7 不会使通知图标变暗。
  • 暗淡是什么意思?
【解决方案2】:

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

你可以设置背景颜色

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

与您的应用标识相匹配。

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 谢谢,它对我有用,我认为它应该被接受为答案。
      【解决方案5】:

      我在应用关闭时遇到了同样的问题this helped me

      很遗憾,这是 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" />
      

      【讨论】:

        【解决方案6】:

        注意:- 如果设备的 android 版本高于 20,您必须生成具有透明背景的图标,并在生成通知时使用此 sn-p

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-22
          • 2019-06-09
          • 2015-02-22
          • 1970-01-01
          • 2018-06-12
          • 1970-01-01
          • 1970-01-01
          • 2022-06-16
          相关资源
          最近更新 更多