Android推送通知:图标未显示在通知中,而是显示白色方块

Blu*_*ver 155 notifications icons android

我的应用会生成通知,但我没有显示为该通知设置的图标.相反,我得到一个白色方块.

我已经尝试调整图标的大小(尺寸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);
}
Run Code Online (Sandbox Code Playgroud)

没有打开通知 通知已打开

Gar*_*hur 167

原因:对于5.0 Lollipop"通知图标必须完全是白色".

如果我们通过将目标SDK设置为20来解决白色图标问题,我们的应用程序将不会针对Android Lollipop,这意味着我们无法使用Lollipop特有的功能.

目标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)

针对以下Lollipop OS版本的Notification Builder的实现将是:

Notification notification = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    notification.setSmallIcon(R.drawable.icon_transperent);
    notification.setColor(getResources().getColor(R.color.notification_color));
} else { 
    notification.setSmallIcon(R.drawable.icon);
} 
Run Code Online (Sandbox Code Playgroud)

注意:setColor仅在Lollipop中可用,它只影响图标的背景.

它将完全解决您的问题!

  • 它只是为了图标而将targetSDK版本设置为<21的错误形式.最好以正确的方式修复它,如本答案中所述:http://stackoverflow.com/questions/27343202/changing-notification-icon-background-on-lollipop (8认同)
  • 通过说__targetSdkVersion 20__,你节省了我的一天!非常感谢. (2认同)
  • 但是在后台当应用程序不在堆栈中时它会显示白色图标,无论如何如何获得相同的结果 (2认同)
  • 我尝试了一切,但它不起作用。它仍然在清单文件中显示带有颜色提及的点 (2认同)

Ruc*_*nia 50

如果您使用的是Google云消息传递,则只需更改图标即可解决此问题.例如,这不起作用:

 Notification notification  = new Notification.Builder(this)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pIntent)
                .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
                .setAutoCancel(true)
                .build();
Run Code Online (Sandbox Code Playgroud)

即使 ic_notification是透明的和白色的.它必须也在Manifest元数据中定义,如下所示:

  <meta-data android:name="com.google.firebase.messaging.default_notification_icon"

            android:resource="@drawable/ic_notification" />
Run Code Online (Sandbox Code Playgroud)

元数据在application标签下,供参考.

  • 非常感谢清单中的元数据提示. (5认同)

N J*_*N J 29

我真的建议遵循Google的设计指南:

其中说"通知图标必须完全是白色的".

  • 404页面不存在 (4认同)
  • 您的答案甚至比我接受的答案还要好。希望我也能接受你的。我不能,但您有我的+1和感谢。干杯! (2认同)
  • 这不是一个很好的答案.如果项目的利益相关者需要针对Android 7,该怎么办?我之前不能只针对任何SDK版本. (2认同)
  • 我在当前的设计指南中找不到任何指定图标必须在透明背景上为白色的内容。尽管文档很差,但情况似乎仍然如此。 (2认同)

ole*_*234 29

(Android Studio 3.5)如果您使用的是最新版本的 Android Studio,则可以生成通知图像。右键单击您的 res 文件夹 > New > Image Asset。然后,您将看到配置图像资产,如下图所示。将图标类型更改为通知图标。您的图像必须是白色和透明的。此配置图像资产将强制执行该规则。 配置图像资产 重要提示:如果您希望图标用于云/推送通知,您必须在应用程序标签下添加元数据以使用新创建的通知图标。

  <application>
      ...
      <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
          android:resource="@drawable/ic_notification" />
Run Code Online (Sandbox Code Playgroud)


vic*_*ale 21

在Android Manifest中声明此代码:

<meta-data android:name="com.google.firebase.messaging.default_notification_icon" 

android:resource="@drawable/ic_stat_name" />
Run Code Online (Sandbox Code Playgroud)

我希望这对你有用.

  • 你把`ic_stat_name`放在哪里?是一张png吗?很多吗?请帮忙! (2认同)
  • @Luke Pighetti在Android Studio中右键单击应用&gt;&gt;新&gt;&gt;图像资产&gt;&gt; IconType(通知) (2认同)

小智 10

我们可以这样做:

创建通知构建器的新对象,并setSmallIcon()使用通知构建器对象进行调用,如下面的代码所示.

创建一个方法,我们将检查我们正在安装应用程序的操作系统版本.如果它低于Lolipop即API 21,那么它将采用带有背景颜色的普通应用程序图标,否则它将采用没有任何背景的透明应用程序图标.因此,使用os版本> = 21的设备将使用setColor()Notification builder类的方法设置图标的背景颜色.

示例代码:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);

notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder));

private int getNotificationIcon(NotificationCompat.Builder notificationBuilder) {

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             int color = 0x008000;
             notificationBuilder.setColor(color);
             return R.drawable.app_icon_lolipop_above;

    } 
    return R.drawable.app_icon_lolipop_below;
}
Run Code Online (Sandbox Code Playgroud)


Saj*_*hir 9

 <meta-data android:name="com.google.firebase.messaging.default_notification_icon"

        android:resource="@drawable/ic_notification" />
Run Code Online (Sandbox Code Playgroud)

在应用程序块的manifest.xml文件中添加此行


Sun*_*nil 8

试试这个

我面临同样的问题,我尝试了很多回答,但没有得到任何解决方案,最后我找到了解决我的问题的方法.

- 制作具有透明背景的通知图标.应用程序的宽度和高度必须类似于以下尺寸,并将所有这些粘贴到您的项目中 - > app-> src-> main-> res

  • MDPI 24*24

  • HDPI 36*36

  • XHDPI 48*48

  • XXHDPI 72*72


在上面粘贴了下面的onMessageReceived方法中的这一行


Intent intent = new Intent(this, News.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            {
                notificationBuilder.setSmallIcon(R.drawable.notify)
                                      //            .setContentTitle(title)
                            //                        .setContentText(message)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
            } else
                {
                    notificationBuilder.setSmallIcon(R.drawable.notify)
                       //                                .setContentTitle(title)
                        //                        .setContentText(message)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
            }
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
Run Code Online (Sandbox Code Playgroud)

不要忘记在清单文件中添加此代码

<meta-data 
android:name="com.google.firebase.messaging.default_notification_icon" 
android:resource="@drawable/app_icon" />
Run Code Online (Sandbox Code Playgroud)


Har*_*ana 7

如果您想提供棒棒糖支持通知图标,请制作两个类型的通知图标:

  1. 正常通知图标:用于以下棒棒糖版本.
  2. 带有透明背景的通知图标:用于棒棒糖及以上版本.

现在,根据操作系统版本在运行时将相应的图标设置为通知构建器:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    mBuilder.setSmallIcon(R.drawable.ic_push_notification_transperent);
} else {
    mBuilder.setSmallIcon(R.drawable.ic_push_notification);
}
Run Code Online (Sandbox Code Playgroud)


Ani*_*vle 7

最后我得到了解决这个问题的方法.

仅当应用程序未运行时才会出现此问题.(既不在背景中也不在前景中).当应用程序在前台或后台运行时,通知图标会正确显示.(不是白色方块)

因此,我们要设置的是后端API中的通知图标与前端的配置相同.

在前端,我们使用了React Native,对于推送通知,我们使用了react-native-fcm npm包.

FCM.on("notification", notif => {
   FCM.presentLocalNotification({
       body: notif.fcm.body,
       title: notif.fcm.title,
       big_text: notif.fcm.body,
       priority: "high",
       large_icon: "notification_icon", // notification icon
       icon: "notification_icon",
       show_in_foreground: true,
       color: '#8bc34b',
       vibrate: 300,
       lights: true,
       status: notif.status
   });
});
Run Code Online (Sandbox Code Playgroud)

我们使用fcm-push npm包使用Node.js作为推送通知的后端,并按如下方式设置有效负载结构.

{
  to: '/topics/user', // required
  data: {
    id:212,
    message: 'test message',
    title: 'test title'
  },
  notification: {
    title: 'test title',
    body: 'test message',
    icon : 'notification_icon', // same name as mentioned in the front end
    color : '#8bc34b',
    click_action : "BROADCAST"
  }
}
Run Code Online (Sandbox Code Playgroud)

什么它基本上搜索我们的Android系统本地存储的notification_icon图像.


Har*_*ris 7

我通过在清单中添加以下代码解决了这个问题,

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

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/black" />
Run Code Online (Sandbox Code Playgroud)

ic_stat_name在 Android Studio上创建的地方右键单击 res >> New >>Image Assets >> IconType(Notification)

还有一个我必须在服务器 php 端使用通知负载做的步骤

$message = [
    "message" => [
        "notification" => [
            "body"  => $title , 
            "title" => $message
        ],

        "token" => $token,

    "android" => [
           "notification" => [
            "sound"  => "default",
            "icon"  => "ic_stat_name"
            ]
        ],

       "data" => [
            "title" => $title,
            "message" => $message
         ]


    ]
];
Run Code Online (Sandbox Code Playgroud)

注意部分

    "android" => [
           "notification" => [
            "sound"  => "default",
            "icon"  => "ic_stat_name"
            ]
        ]
Run Code Online (Sandbox Code Playgroud)

图标名称的位置"icon" => "ic_stat_name"应与清单上的设置相同。


Jer*_*ank 5

通知是灰度的,如下所述。尽管其他人已经写过,但它们不是黑白的。您可能已经看到带有多种阴影的图标,例如网络强度条。

在API 21(Lollipop 5.0)之前,颜色图标可以使用。您可以强制应用程序以API 20为目标,但这限制了应用程序可用的功能,因此不建议这样做。您可以测试正在运行的API级别,并适当地设置颜色图标或灰度图标,但这可能不值得。在大多数情况下,最好使用灰度图标。

图像具有四个通道,RGBA(红色/绿色/蓝色/ alpha)。对于通知图标,Android会忽略R,G和B通道。唯一重要的渠道是Alpha,也称为不透明度。使用编辑器设计图标,该编辑器可让您控制绘图颜色的Alpha值。

Alpha值如何生成灰度图像:

  • Alpha = 0(透明)—这些像素是透明的,显示背景色。
  • Alpha = 255(不透明)—这些像素为白色。
  • Alpha = 1 ... 254-这些像素正是您所期望的,提供了透明和白色之间的阴影。

用以下方法进行更改setColor

  • 致电NotificationCompat.Builder.setColor(int argb)。从文档中Notification.color

    呈现此通知时,标准样式模板要应用的强调颜色(类似于Color中的常数的ARGB整数)。当前的模板设计通过将图标图像(以白色模版)覆盖在此颜色的字段上方来构造彩色标题图像。Alpha组件将被忽略。

    我对setColor的测试表明,忽略Alpha组件。较高的Alpha值会使像素变白。较低的Alpha值会将像素变为通知区域中的背景颜色(在我的设备上为黑色),或在下拉通知中变为指定的颜色。


小智 5

解决此问题的要求:

  1. 图像格式:32 位 PNG(带 Alpha)

  2. 图像应该是透明的

  3. 透明度颜色索引: 白色 (FFFFFF)

来源:http ://gr1350.blogspot.com/2017/01/problem-with-setsmallicon.html


Gha*_*yma 5

我找到了一个链接,我们可以在其中生成我们自己的白色图标,

尝试此链接生成启动器图标的白色图标。

打开此链接并上传您的 ic_launcher 或通知图标