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

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


当前回答

对于自定义的本地通知,在AndroidManifest.xml中添加以下元数据,然后它就可以工作了。

 <application
    android:name="xxxxxx"
        android:label="xxxxxx"
        android:icon="@mipmap/ic_launcher"
        
        >

       <meta-data
                android:name="your_apps_bundle_id.default_notification_icon"
                android:resource="@drawable/ic_notif" />

......

其他回答

不同的版本可以使用不同的图标。简单地在图标上设置逻辑,就像这样:

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

我在android 8.0上也有类似的问题。尝试使用白色图标资源。当我试图使用彩色图像图标时,我有白色正方形,当我将其替换为白色图标时,它开始工作。

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

在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.

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

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

目标Sdk 21的解决方案

如果你想支持棒棒糖材质图标,那么为棒棒糖和上面的版本制作透明图标。请参阅以下资料: https://design.google.com/icons/

请查看http://developer.android.com/design/style/iconography.html,我们将看到白色风格是通知在Android Lollipop中显示的方式。

在Lollipop中,谷歌还建议我们使用一种将显示在白色通知图标后面的颜色。参考链接:https://developer.android.com/about/versions/android-5.0-changes.html

在我们想添加颜色的地方 https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html改变颜色(int)

以下和以上棒棒糖操作系统版本的通知生成器的实现将:

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只在Lollipop中可用,它只影响图标的背景。

它将彻底解决你的问题!!

最后我得到了这个问题的解决方案。

此问题仅发生在应用程序根本没有运行时。(既不在背景也不在前景)。当应用程序在前台或后台运行时,通知图标将正常显示。(不是白色方块)

所以我们要设置的是在后端api中通知图标的配置与Frontend相同。

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

我们使用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"
  }
}

基本上它会搜索存储在Android系统本地的notification_icon图像。