Why push notification icon is coming white instead of app icon android using FCM?

Viewed 1138

In marshmallow = Showing white icon. in Marsmello. In Kitkat = showing app icon. In Oreo = showing notification with app icon when is background but when in foreground notification is arriving but not showing.

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());

    if (remoteMessage == null)
        return;

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
        handleNotification(remoteMessage.getNotification().getBody());
    }

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        //Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        String title = remoteMessage.getData().get("title");
        String message = remoteMessage.getData().get("body");
        String click_action = remoteMessage.getData().get("click_action");
        System.out.println("clickAction==@@@" + click_action);

        handleDataMessage(title, message, click_action);
    }
}

private void handleNotification(String message) {

    Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
    pushNotification.putExtra("message", message);
    LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
    // play notification sound
    NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
    notificationUtils.playNotificationSound();

}



@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void handleDataMessage(String noti_title,String noti_message,String noti_click_action) {

    try {

        System.out.println("noti_title="+ noti_title);
        System.out.println("noti_message="+ noti_message);
        System.out.println("noti_click_action="+ noti_click_action);

        Intent intent=new Intent(noti_click_action);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(noti_title);
        notificationBuilder.setContentText(noti_message);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());


    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}


/**
 * Showing notification with text only
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}

/**
 * Showing notification with text and image
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}

Using the above code i am getting the different icon in different version.Is there any mistake?

3 Answers

it seems that backend is sending data in notification key and then the control of the icons comes from the server and you can't handle it ask the backend to send you notification with null and send all data in the data key

As far as I remember, you should have a white-transparent icon. So if you have a colored icon, it will be shown differently.

notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);

This is a usual mistake. You have to create your own PNG and lay it in @drawable. Draw a silhouette, containing only white color (and transparent background).

You need to add this in AndroidManifest.xml

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

Also, for your notification Builder, set the same icon with

notificationBuilder.setSmallIcon(R.drawable.statusbar_not_icon)

How to create status bar icon?

Related