Android 通知图标颜色有时是白色,有时是彩色

Android notification icon colour sometimes white, sometimes colourful

我的 Android 应用程序有一个小问题。它通过 FCM 接收通知并将它们显示为推送通知。到目前为止一切正常,但奇怪的问题是,有时图标是白色的,有时是彩色的。

当应用程序在屏幕上打开并且此时我收到推送通知时,彩色推送通知显示在屏幕顶部。

当应用程序关闭时,我收到带有白色图标的推送通知。

我附上了截图: Screenshot

这是创建推送通知的代码片段:

        Notification.Builder notificationBuilder = new Notification.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_alert)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setAutoCancel(true)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setPriority(Notification.PRIORITY_HIGH)
            .setColor(Color.parseColor("#83c3ed"))
            .setLights(Color.RED, 1000, 500)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
    inboxStyle.setBigContentTitle("WetterApp");
    inboxStyle.addLine(notification.getTitle());
    inboxStyle.addLine(notification.getBody());
    notificationBuilder.setStyle(inboxStyle);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

我的移动设备 Android 6.0.1,我的 SDK 版本是 23。

感谢您的帮助。

我认为更好的解决方案是在应用程序中添加一个剪影图标,如果设备是 运行 Android Lollipop 就可以使用它。

例如:

Notification notification = new Notification.Builder(context)
            .setAutoCancel(true)
            .setContentTitle("My notification")
            .setContentText("Look, white in Lollipop, else color!")
            .setSmallIcon(getNotificationIcon())
            .build();

    return notification;

并且,在 getNotificationIcon 方法中:

private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}

按照 google 的 guide 创建您的图标,它可能也出现在状态栏中。

然后,试试这个:

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setTicker(context.getResources().getString(R.string.app_name));
            builder.setSmallIcon(R.mipmap.ic_your_status_bar_logo);
            builder.setAutoCancel(true);
            builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            builder.setContentIntent(pendingIntent);          
       builder.setContentTitle(
context.getResources().getString(R.string.app_name));
            builder.setContentText(message);
            builder.setDefaults(Notification.DEFAULT_SOUND);
            builder.setPriority(NotificationCompat.PRIORITY_HIGH);
            builder.setColor(ContextCompat.getColor(context, R.color.color_primary));
            NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(NOTIFICATION_ID, builder.build());

嗯,我认为问题有所不同。当应用程序在屏幕上打开时,我用于创建通知的代码只会被调用。当我收到通知并关闭应用程序时,通知由 Android 系统自动处理。

我必须在发送到 FCM 服务器的通知中设置颜色:

 $data = [
        'notification' => [
            'title' => 'Warnung: Wohnzimmer',
            'text' => 'Innen: 20,3°C Außen: 24,5°C, Tendenz: -0,2°C',
            'color' => '#83c3ed',
            'sound' => 'default'
        ],
        'to' => '/topics/testNotification'
    ];

现在我在应用程序中以及关闭应用程序时都会看到浅蓝色背景图标。