在Android12以上的版本 通知使用svg图片,会被设置为灰色。(tint灰)
国内的可以这样,三星的原生的不能这么搞
Starting in Android 12 (API level 31), the system derives the icon color from the notification color you set in the app. If the app doesn't set the color, it uses the system theme color.
Previously, the color was gray.
Previously, the color was gray.
简单解决办法。不用svg图,直接用png图标就行
PS:
修改Firebase默认的通知图标
在AndroidManifest.xml中设置
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
firebase图标设置代码
com.google.firebase.messaging.DisplayNotification.handleNotification
boolean handleNotification() {
if (params.getBoolean(MessageNotificationKeys.NO_UI)) {
return true; // Fake notification, nothing else to do
}
if (isAppForeground()) {
return false; // Needs to be passed to onMessageReceived
}
ImageDownload imageDownload = startImageDownloadInBackground();
CommonNotificationBuilder.DisplayNotificationInfo notificationInfo =
CommonNotificationBuilder.createNotificationInfo(context, params);
waitForAndApplyImageDownload(notificationInfo.notificationBuilder, imageDownload);
showNotification(notificationInfo);
return true;
}
具体抓取icon
private static int getSmallIcon(
PackageManager packageManager,
Resources resources,
String pkgName,
String resourceKey,
Bundle manifestMetadata) {
if (!TextUtils.isEmpty(resourceKey)) {
// if the message contains a specific icon name, try to find it in the resources.
int iconId = resources.getIdentifier(resourceKey, "drawable", pkgName);
if (iconId != 0 && isValidIcon(resources, iconId)) {
return iconId;
}
// Also try the mipmap resources if not found in drawable
iconId = resources.getIdentifier(resourceKey, "mipmap", pkgName);
if (iconId != 0 && isValidIcon(resources, iconId)) {
return iconId;
}
Log.w(
TAG, "Icon resource " + resourceKey + " not found. Notification will use default icon.");
}
int iconId = manifestMetadata.getInt(METADATA_DEFAULT_ICON, 0);
if (iconId == 0 || !isValidIcon(resources, iconId)) {
// No icon found so far. Falling back to default App icon (launcher icon).
try {
/* flags= */ iconId = packageManager.getApplicationInfo(pkgName, 0).icon;
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Couldn't get own application info: " + e);
}
}
if (iconId == 0 || !isValidIcon(resources, iconId)) {
// Wow, app doesn't have a launcher icon. Falling back on icon-placeholder used by the OS.
iconId = android.R.drawable.sym_def_app_icon;
}
return iconId;
}