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

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


当前回答

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

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

所以我们要设置的是在后端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图像。

其他回答

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

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

根据谷歌的设计指南:

通知图标必须完全为白色。

为了减少SDK的特定版本,你可以简单地这样做:(将'#'替换为'0x')

Notification notification = new NotificationCompat.Builder(this);
notification.setSmallIcon(R.drawable.icon_transperent);
notification.setColor(0x169AB9); //for color: #169AB9

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

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

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

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