FCM默认图标使用无效渐变

The*_*hat 31 android firebase firebase-cloud-messaging

未使用Android清单的默认FCM图标.而是使用Android标准图标.在logcat中它说:

E/FirebaseMessaging:ID为2131230849的图标使用无效渐变.使用后备图标.

默认图标仅包含一种颜色.我用多个不同的图标测试了它,但总是使用Android标准图标.

djx*_*eam 14

更新:由版本12.0.0中的firebase修复

下面的错误代码已经使用此更新,它在8.0上运行良好,同时防止我原来的答案中列出的错误.

@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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这并没有解决问题,但从评论中我被要求作为答案.以下是来自firebase 11.8.0的代码,它是罪魁祸首,仅适用于Android 8.0(API 26).检查的原因是因为自适应图标存在Android 8.0通知错误https://www.bleepingcomputer.com/news/mobile/android-oreo-adaptive-icons-bug-sends-thousands-of-phones-into -infinite-boot-loops / 所以这段代码阻止了这一点,但这样做也可以防止非自适应图标正确显示

    @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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的logcat中注意到,我的应用程序每次通知都会触发此代码两次,它会尝试我的通知drawable,我根据Firebase的说明在清单中设置,然后再次点击它尝试为启动器图标执行此操作.两者都失败了,即使我将它们制作成纯色的drawables.

根据来自southrop的另一条评论,firebase团队意识到了这个问题并正在努力修复,但没有给出时间表.

此代码不在11.6.0及更低版本中,因此如果您确实需要暂时使用此代码,请降级您的firebase.

希望这有助于找到此帖子的其他人搜索错误


Eug*_*dov 10

只有当您的应用程序针对API 26(或更高版本)并且您使用Firebase 11.8.0并在装有Android 8.0的设备上启动时,才会出现此错误.所以我决定只为这些版本修复它.解决方法:您应该覆盖Application类并覆盖它的getResources()以返回修补的Resources类:

@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;
}
Run Code Online (Sandbox Code Playgroud)

你应该为我们的目的创建补丁资源类:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并检查您的AndroidManifest.xml是否包含此元数据:

<meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_firebase_notification"
        />
Run Code Online (Sandbox Code Playgroud)

如果您按照说明执行操作,将在FirePro SDK上呈现的Android 8.0上的所有通知都将显示为默认图标@ drawable/ic_firebase_notification

但我希望Firebase团队能够修复他们的有害检查并阅读它是如何工作的