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

我已经尝试调整图标的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);
}


当前回答

对于SDK >= 23,请添加setLargeIcon

notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(context.getResources(), R.drawable.lg_logo))
            .setContentTitle(title)
            .setStyle(new Notification.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setContentText(msg)
            .setContentIntent(contentIntent)
            .setSound(sound)
            .build();

其他回答

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

应用程序的宽度和高度必须像下面的大小和粘贴所有这些在你的项目->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" />

如果您正在使用谷歌云消息传递,那么这个问题将不能通过简单地更改图标来解决。例如,这将不起作用:

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

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

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

            android:resource="@drawable/ic_notification" />

元数据放在应用程序标签下,以供参考。

删除图标的背景使用任何网站,建议一个是https://www.remove.bg/ 然后简单地使用该图像下载后,您的问题将得到解决。

对于SDK >= 23,请添加setLargeIcon

notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(context.getResources(), R.drawable.lg_logo))
            .setContentTitle(title)
            .setStyle(new Notification.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setContentText(msg)
            .setContentIntent(contentIntent)
            .setSound(sound)
            .build();

当你想保持彩色图标-变通 在图标中添加颜色略有不同的像素。在我的情况下,有黑色图标的阴影和光。当添加深蓝色像素它的工作。