【问题标题】:White Square is displayed instead of my notification icon [duplicate]显示白色方块而不是我的通知图标[重复]
【发布时间】:2018-07-04 19:29:32
【问题描述】:

我正在尝试在我的 FCM 接收器中收到来自服务器的有效负载后立即构建自己的通知,我将通知图标设置为 PNG 格式的应用程序图标,但是,我的通知显示为而是白色方块,我不知道为什么.. 这是我的代码:

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle("Your chat is going to expire tomorrow!");

        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, main_activity.class);

        resultIntent.putExtra("launchedFromNotification",true);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your app to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(main_activity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mNotificationId is a unique integer your app uses to identify the
        mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());

【问题讨论】:

  • 你好 Ahmed Khairy,我一直很绝望,就像你在寻找解决方案一样,我发现这篇文章如果你完全按照它所说的那样做,图标的问题就消失了。我的目标是 Android O 设备和带有 SDK (27) 的应用程序,现在一切正常。 stackoverflow.com/a/48562435/3438335

标签: android push-notification icons notification-icons


【解决方案1】:

这通常发生在您使用非字母图标时

当您在通知上使用图标时,android 会在图标的所有非 alpha(非透明)部分上进行绘制。这意味着如果您使用方形图像作为通知图标,它只会显示为白色方形。

要解决此问题,只需使用类似形状的图标即可。查看以下内容以供参考:

Icon Reference

【讨论】:

  • 那么我该怎么做才能把它变成 alpha 图标?
  • 将您的图标添加到问题中