简体   繁体   中英

Notification bar icon turns white in Android from FCM

I know that to support Lollipop Material design guidelines we have to make notification icon as transparent.

Here is my FCM onMessageReceived() function to show noticication.

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

But here my issue is that when the application is running in foreground and visible, it will take my logo_a_transparent and will get desired result (screenshot - first icon in the notification bar).

But when we are pausing the application and an FCM push came, It takes my app icon (android:icon="@drawable/ic_launcher") as notification icon became white (screenshot - second icon in the notification bar).

Replacing app icon as transparent will work, But not a correct solution.

两个通知图标来自同一应用程序。当应用程序为前台时,第一个图标为推送通知;当应用程序为后台时,第一个图标为推送通知

Add this line in your menifestfile set your resource as your choice add this one

<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" />

With FCM, you can send two types of messages to clients applicatin

1) Notification messages, 2) Data messages Here fcm doumentation

Notification Message calls onMessageReceived() only when the application is foreground. Notification messages are delivered to the notification tray when the app is in the background that's automatically handled by Android system rather than calling onMessageReceived(), The system uses app icon as notification icon that's why icon became white in background push. The android application needs not to be transparent.

In the case of Data Message whether the app is in the background or the foreground it will always be handled by onMessageReceived().

Data Message

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

So I should use a data-only message payload or Messages with both notification and data payloads, so my onMessageReceived() can handle this and correct notification icon will displayed.

Fix at firebase 12.0.0. Just update your build.gradle to 12.0.0

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM