我的应用程序生成了一个通知,但我为该通知设置的图标不显示。相反,我得到了一个白色的正方形。

我已经尝试调整图标的png大小(尺寸720x720, 66x66, 44x44, 22x22)。奇怪的是,当使用更小的维度时,白色正方形也更小。

我已经谷歌了这个问题,以及生成通知的正确方式,从我所读到的我的代码应该是正确的。不幸的是,事情并不像他们应该的那样。

我的手机是装有安卓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);
}


当前回答

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

尝试这个链接来生成启动器图标的白色图标。

打开这个链接并上传你的ic_launcher或通知图标

其他回答

通知是灰色的,如下所述。不管别人写了什么,它们都不是黑白分明的。你可能见过带有多种色调的图标,比如网络强度条。

在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更改:

Call NotificationCompat.Builder.setColor(int argb). From the documentation for Notification.color: Accent color (an ARGB integer like the constants in Color) to be applied by the standard Style templates when presenting this notification. The current template design constructs a colorful header image by overlaying the icon image (stenciled in white) atop a field of this color. Alpha components are ignored. My testing with setColor shows that Alpha components are not ignored. Higher Alpha values turn a pixel white. Lower Alpha values turn a pixel to the background colour (black on my device) in the notification area, or to the specified colour in the pull-down notification.

修复此问题的要求:

图片格式:32位PNG(带alpha) 图像应该是透明的 透明度颜色指数:白色(FFFFFF)

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

如果你想提供棒棒糖支持的通知图标,那么做两种类型的通知图标:

普通通知图标:适用于棒棒糖以下版本。 通知图标与透明背景:棒棒糖和以上版本。

现在根据OS版本在运行时设置适当的图标为通知生成器:

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

我们可以这样做:

创建一个通知生成器的新对象,并使用下面代码所示的通知生成器对象调用setSmallIcon()。

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

示例代码:

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

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

应用程序的宽度和高度必须像下面的大小和粘贴所有这些在你的项目->app->src->main->res


MDPI 24 * HDPI 36 * 36 XHDPI 48 * 48 XXHDPI 72 * 72


在上面粘贴这个在你的onmessagerreceived方法下面行


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

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

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