【问题标题】:Notification bar icon turns white in Android from FCM通知栏图标在 FCM 中的 Android 中变为白色
【发布时间】:2017-09-15 12:16:55
【问题描述】:

我知道要支持 Lollipop Material 设计指南,我们必须让通知图标透明。

这是我的 FCM onMessageReceived() 函数,用于显示通知。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    NotificationCompat.Builder mBuilder;
    mBuilder =   new NotificationCompat.Builder(this)
            .setContentTitle(remoteMessage.getNotification().getBody()) // title for notification
            .setContentText(remoteMessage.getNotification().getTitle()) // message for notification
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
            .setSmallIcon(getNotificationIcon())
            .setAutoCancel(true); // clear notification after click

    Intent intent = new Intent(this, CheckInListPage.class);
    PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
    mBuilder.setContentIntent(pi);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.logo_a_transparent : R.drawable.logo_notifc;
}

但这里我的问题是,当应用程序在前台运行并且可见时,它将采用我的 logo_a_transparent 并获得所需的结果(屏幕截图 - 通知栏中的第一个图标)。

但是当我们暂停应用程序并且 FCM 推送到来时,它需要我的应用程序图标 (android:icon="@drawable/ic_launcher") 作为通知图标变成白色(屏幕截图 - 通知栏中的第二个图标)。

将应用图标替换为透明可以,但不是正确的解决方案。

【问题讨论】:

  • 因为你在使用GetNotification() insteam 每次都使用getData() 方法调用onmessageReceived,而且你可以使用相同的图标
  • 我已经这样做了...但是当我们暂停应用程序并且 FCM 推送到来时,通知图标变为白色(屏幕截图 - 通知栏中的第二个图标)。应用前台时,没有问题。
  • 它只是给一个颜色,但问题还没有解决。问题是当应用程序暂停时,fcm 推送通知使用应用程序图标而不是我的透明图标。这可以通过将应用程序图标设置为透明来解决,但这对应用程序图标来说并不舒适。

标签: java android push-notification


【解决方案1】:

在您的清单文件中添加这一行,将您的资源设置为您的选择添加这一行

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

<meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@android:color/transparent" />

【讨论】:

  • 我已添加 但无法正常工作
  • 添加第二个元数据并尝试...可能希望这个对您有所帮助
  • 仍然无法正常工作...我的 fcm 版本是 compile 'com.google.firebase:firebase-messaging:9.6.1'
  • 尝试更改这一行 compile 'com.google.firebase:firebase-messaging:10.2.1'
  • 我也试过了,但不幸的是它不起作用。假设它是否正常工作,但即使它不是一个完整的解决方案,因为在棒棒糖之前的设备的情况下,我们想要彩色图标而不是透明图标。所以我们希望根据条件设置图标,而不是简单地设置图标。
【解决方案2】:

使用 FCM,您可以向应用程序的客户端发送两种类型的消息

1) 通知消息, 2) 数据信息 Here fcm doumentation

通知消息仅在应用程序处于前台时调用 onMessageReceived()。当应用程序在后台时,通知消息会传递到通知托盘,由 Android 系统自动处理,而不是调用 onMessageReceived(),系统使用应用程序图标作为通知图标,这就是为什么图标在后台推送时变为白色的原因。 android 应用程序不需要是透明的。

数据消息的情况下,无论应用程序是在后台还是前台,它都将始终由 onMessageReceived() 处理。

数据消息

  {  "to" : "Ahd8fsdfu78sd...", "data" : {
     "Name" : "...",
     "body" : "...",
    }
}

所以我应该使用纯数据消息有效负载或同时包含通知和数据有效负载的消息,这样我的 onMessageReceived() 可以处理此问题并显示正确的通知图标。

【讨论】:

    【解决方案3】:

    在 firebase 12.0.0 修复。只需将您的 build.gradle 更新为 12.0.0

    https://firebase.google.com/support/release-notes/android#20180320

    【讨论】: