30

The default FCM icon of the Android manifest is not used. Instead the Android standart icon is used. In the logcat it says:

E/FirebaseMessaging: Icon with id: 2131230849 uses an invalid gradient. Using fallback icon.

The default icon only contains one color. I tested it with multiple different icons but always the Android standart icon is used.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
TheRedhat
  • 473
  • 6
  • 13
  • I'm having the same problem, but only on Android 8.0. 8.1 and 7.x work wonderfully. – djxstream Jan 08 '18 at 23:04
  • 1
    FWIW not happening with `firebase-messaging:11.6.0` (happening with version 11.8.0). – ozbek Jan 10 '18 at 10:47
  • I am having the same issue. And this page is literally the only one on the web which references the problem (Ok, here is one more without any info). It would be great if someone could have a look at it. – namxam Jan 11 '18 at 09:45
  • 1
    I submitted this as a bug report to the Firebase team with a MCVE, and they've confirmed the issue. Waiting to see if they have a solution. Will report back when I hear. – southrop Jan 17 '18 at 06:06
  • Apparently, this is fixed now (maybe a result of @southrop's report:) Can anyone else confirm? – ozbek Jan 17 '18 at 09:32
  • 2
    @ozbek Unfortunately the issue isn't fixed on my side. I'm not sure if it's something they can fix on the server side; it appears like an issue with the handling on the SDK side. I got a reply from the Firebase team half an hour or so ago saying they're investigating it. – southrop Jan 18 '18 at 01:12
  • 2
    found the code, here is the check firebase 11.8.0 does, only if SDK != 26` Drawable var2; if((var2 = this.mContext.getResources().getDrawable(var1, (Theme)null)).getBounds().height() != 0 && var2.getBounds().width() != 0) { return true; } else { Log.e("FirebaseMessaging", (new StringBuilder(72)).append("Icon with id: ").append(var1).append(" uses an invalid gradient. Using fallback icon.").toString()); return false; }` – djxstream Jan 19 '18 at 16:46
  • @djxstream write this as an answer with some helpful info, if you found why this is happening or/and found a solution to this – Peter Haddad Jan 22 '18 at 11:49
  • 3
    Got an email 11 hours ago from the Firebase Team after I sent them the code snippet from djxstream: "Our engineers are currently working on a fix for this issue, but I can't share any details nor timelines at this time as to when the fix will be available to the public." Hope that gets released soon, but until then I think there's nothing else we can do. – southrop Jan 24 '18 at 01:47
  • Can any one of you (southrop? djxstream?) summarize all of that and post as an answer, please? – ozbek Jan 24 '18 at 08:22
  • @ozbek added an answer – djxstream Jan 24 '18 at 16:21
  • @theredhat seems there is only dirty hack to fix this issue with this versions - I wrote it below. mark my answer as correct please – Eugene Nefedov Mar 15 '18 at 16:04

5 Answers5

15

UPDATE: fixed by firebase in version 12.0.0

The faulty code below has been updated with this and it works great on 8.0, while preventing the bug outlined in my original answer.

@TargetApi(26)
private final boolean zza(int var1) {
    if(VERSION.SDK_INT != 26) {
        return true;
    } else {
        try {
            if(this.zzb.getResources().getDrawable(var1, (Theme)null) instanceof AdaptiveIconDrawable) {
                Log.e("FirebaseMessaging", (new StringBuilder(77)).append("Adaptive icons cannot be used in notifications. Ignoring icon id: ").append(var1).toString());
                return false;
            } else {
                return true;
            }
        } catch (NotFoundException var2) {
            return false;
        }
    }
}

This doesnt solve the issue, but from the comments I was asked to put it as an answer. Here is the code from firebase 11.8.0 that is the culprit and only on Android 8.0 (API 26). The reason for this check is because there is an Android 8.0 notification bug with adaptive icons https://www.bleepingcomputer.com/news/mobile/android-oreo-adaptive-icons-bug-sends-thousands-of-phones-into-infinite-boot-loops/ so this code prevents that, but in doing so, also prevents non-adaptive icons from showing up properly

    @TargetApi(26)
private final boolean zzid(int var1) {
    if(VERSION.SDK_INT != 26) {
        return true;
    } else {
        try {
            Drawable var2;
            if((var2 = this.mContext.getResources().getDrawable(var1, (Theme)null)).getBounds().height() != 0 && var2.getBounds().width() != 0) {
                return true;
            } else {
                Log.e("FirebaseMessaging", (new StringBuilder(72)).append("Icon with id: ").append(var1).append(" uses an invalid gradient. Using fallback icon.").toString());
                return false;
            }
        } catch (NotFoundException var3) {
            return false;
        }
    }
}

I've noticed in my logcat, that my app hits this code twice per notification, it tries for my notification drawable which i set in the manifest as per Firebase's instructions, then hits it again trying to do it for the launcher icon. Both fail, even when I make them solid color drawables.

As per another comment from southrop, the firebase team is aware of the issue and working on a fix but no timeline given.

This code is not in 11.6.0 and lower, so if you really need this working for the time being, downgrade your firebase.

Hope this helps others who find this post searching for the error

djxstream
  • 565
  • 2
  • 12
  • I have the same problem on older Firebase version. My app has 2 flavors with different icons, first flavor fails and fallback icon is used, second one displays notification icon just fine. However, I am unable to undarstand why. – user1209216 Jan 31 '18 at 12:26
  • I have found this solution to avoid the white square in app in background on Android O with SDK (27).https://stackoverflow.com/a/48562435/3438335 – Ivor Feb 26 '18 at 13:25
  • Any idea why this would work on some Oreo devices? I can reproduce the problem on certain Oreo devices, but not all of them :) - edit: nevermind, I found the answer, the test is actually only on 26 (8.0), and not 27 (8.1) or more. Doh! ;) – BoD Mar 05 '18 at 18:11
10

This bug happens only if your application targeting API 26 (or greater) and you use Firebase 11.8.0 and launching it on device with Android 8.0. So I decided to fix it only for these version. The workaround: You should override Application class and override it's getResources() to return patched Resources class:

@Override public Resources getResources() {
    if (resources == null) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
            resources = new ResourcesForSupportFirebaseNotificationsOnAndroid8(super.getResources());
        } else {
            resources = super.getResources();
        }
    }
    return resources;
}

And you should create patched for our purposes Resources class:

public class ResourcesForSupportFirebaseNotificationsOnAndroid8 extends Resources {

    private final Resources resources;

    public ResourcesForSupportFirebaseNotificationsOnAndroid8(final Resources resources) {
        super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
        this.resources = resources;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override public Drawable getDrawable(final int id, @Nullable final Theme theme) throws NotFoundException {
        if (id == R.drawable.ic_firebase_notification) {
            final Drawable drawable = resources.getDrawable(id, theme);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            return drawable;
        } else {
            return resources.getDrawable(id, theme);
        }
    }
}

And check that your AndroidManifest.xml contains this meta-data:

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

If you will do as described all notifications on Android 8.0 that will be rendered by Firebase SDK will be shown with default icon @drawable/ic_firebase_notification

But I hope that Firebase team fix their harmfull check and read how it's works

Eugene Nefedov
  • 480
  • 3
  • 11
  • it's fixed with v12.0.0: https://firebase.google.com/support/release-notes/android#20180320 – Boni2k Mar 23 '18 at 07:42
1

Fixed with v12.0.0: https://firebase.google.com/support/release-notes/android#20180320

FIXED Fixed a regression that caused custom notification icons to be rejected on Android 8.0.

Boni2k
  • 3,255
  • 3
  • 23
  • 27
-1

What's the version of the android studio? Android studio work with new icon design feature starts from Android Studio 3.0. If you are using android studio 3.0 or above go to the drawable folder and call context menu to add new vector or image asset. Look for the legacy tab, which is the same setting or feature of the earlier version of android studio.

  • 1
    I have the newest android studio and I used the new vector asset feature. I fixed it by downgrading Firebase to an older version. – TheRedhat Jan 24 '18 at 06:14
  • 1
    Your answer doesn't address the problem. The issue is to do with Firebase Cloud Messenger, not Android Studio. – southrop Jan 24 '18 at 06:51
-2

Your icon have to be .png with transparent background, converted from vector graphics editor(AI, Sketch), or, more better, it should be .svg Just try to convert your .png icon in drawable resources with http://romannurik.github.io/AndroidAssetStudio web-service. It have to help to solve your problem, or if not, you have to redraw your icon.

Scrobot
  • 1,911
  • 3
  • 19
  • 36
  • 1
    Your answer doesn't address the problem. The issue here is that FCM doesn't use the specified icon for background notification messages on Android 8.0, and only this one version. All other versions work as normal. The issue is not with the icon itself, but elsewhere. – southrop Jan 23 '18 at 01:30