图标不显示在通知中:白色方块显示

我的应用程序生成通知,但是我为该通知设置的图标未显示。 相反,我得到一个白色的广场。

我已经尝试调整图标的PNG(尺寸720×720,66×66,44×44,22×22)。 奇怪的是,当使用更小的尺寸时,白色方块更小。

我已经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); } 

没有打开通知通知已打开

原因:对于5.0棒棒糖“通知图标必须是完全白色的”。

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

解决scheme为目标Sdk 21

如果你想支持棒棒糖素材图标,然后使棒棒糖和以上版本的透明图标。 请参阅以下内容: https : //design.google.com/icons/

请看http://developer.android.com/design/style/iconography.html ,我们将看到,白色的风格是如何通知在Android棒棒糖显示。

在棒棒糖中,Google也build议我们使用会在白色通知图标后显示的颜色。 请参阅链接: 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 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); } 

注意:setColor只在棒棒糖中可用,它只影响图标的背景。

它会完全解决你的问题!

我真的build议遵循Google的devise指南 :

“通知图标必须完全是白色的。”

以下是你可以如何实现它:

如果您使用Gradle / Android Studio构build应用程序,请使用build.gradle:

 defaultConfig { targetSdkVersion 20 } 

否则(Eclipse等)使用AndroidManifest.xml:

 <uses-sdk android:minSdkVersion="..." android:targetSdkVersion="20" /> 

我们可以做如下:

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

创build一个方法,我们将检查哪个操作系统版本,我们正在安装我们的应用程序。 如果它低于Lolipop即API 21那么它将采取与背景颜色的正常的应用程序图标,否则将采取透明的应用程序图标,没有任何背景。 所以使用OS版本> = 21的设备将使用通知构build器类的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; } 

如果你想提供棒棒糖支持通知图标,然后使两个types的通知图标:

  1. 正常通知图标:用于低于棒棒糖版本。
  2. 通知图标与透明背景:棒棒糖和以上版本。

现在,根据操作系统版本,在运行时为通知构build器设置适当的图标:

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

最后我已经得到解决这个问题。

仅当应用程序根本不运行时,才会出现此问题。 (既不在后台也不在前台) 。 当应用程序在前台或后台运行时,通知图标显示正确 (不是白色方框)

所以我们要设置的是后端API中的通知图标和前端相同的configuration。

在前端,我们使用了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 }); }); 

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

 { 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" } } 

它基本上search我们的Android系统本地存储的notification_icon图像。

在Android Manifest中声明这个代码:

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

我希望这对你有用。

尝试这个

我面对同样的问题,我尝试了很多anwers,但没有得到任何解决scheme,最后我find了解决我的问题的方式。

– 使透明背景的通知图标。应用程序的宽度和高度必须像下面的大小和粘贴所有这些在您的项目 – >应用程序> src-> main->资源

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

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

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

您可以为不同的版本使用不同的图标。 只需像这样设置你的图标的逻辑:

 int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.colored_: R.drawable.white_tint_icon_for_lolipop_or_upper; 

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

通知灰度如下所述。 他们不是黑白的,尽pipe其他人已经写了。 您可能已经看到了多个色调的图标,如networking强度条。

API 21之前(棒棒堂5.0),颜色图标的工作。 您可以强制您的应用程序定位到API 20,但是这会限制您的应用程序可用的function,所以不build议这样做。 您可以testing运行的API级别,并适当地设置颜色图标或灰度图标,但这可能不值得。 在大多数情况下,最好使用灰度图标。

图像有四个通道,RGBA(红/绿/蓝/阿尔法)。 对于通知图标,Android会忽略R,G和B通道。 唯一的重要渠道是阿尔法,也被称为不透明。 用一个编辑器devise你的图标,让你控制你的绘画颜色的Alpha值。

Alpha值如何生成灰度图像:

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

setColor改变它:

  • 调用NotificationCompat.Builder.setColor(int argb) 。 从Notification.color的文档:

    在呈现此通知时由标准样式模板应用的强调颜色(类似于Color中的常量的ARGB整数)。 当前的模板devise通过将图标图像(以白色印刷)叠加在该颜色的字段上来构造彩色标题图像。 Alpha组件被忽略。

    我用setColor进行testing显示Alpha组件被忽略。 较高的Alpha值将像素变成白色。 较低的Alpha值在通知区域中将像素转换为背景颜色(在我的设备上为黑色),或在下拉通知中将其指定为指定的颜色。

解决此问题的要求:

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

  2. 图像应该是透明的

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

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