简体   繁体   English

Firebase 通知为灰色 flutter

[英]Firebase Notification is grey flutter

I'm using Firebase Cloud Messaging to send notifications to my Flutter app.我正在使用 Firebase Cloud Messaging 向我的 Flutter 应用程序发送通知。 It works fine but my app icon notification is grey on the Google Pixel XL.它工作正常,但我的应用程序图标通知在 Google Pixel XL 上是灰色的。 I can see the outline, but for some reason, it's still grey.我可以看到轮廓,但由于某种原因,它仍然是灰色的。 I've read in different articles that it has to do with the Android SDK level, but the minSdkVersion in Flutter is already 16 (less than 21) so I don't understand why the color won't show on.我在不同的文章中读到它与 Android SDK 级别有关,但是 Flutter 中的 minSdkVersion 已经是 16 了(为什么我不明白为什么会显示 2')

So is it possible to show my app icon for notifications with the color?那么是否可以用颜色显示我的应用程序图标通知?

--Thanks in advance - 提前致谢

Here you can read: 在这里你可以阅读:

Customize default notification自定义默认通知

Custom default icon自定义默认图标

Setting a custom default icon allows you to specify what icon is used for notification messages if no icon is set in the notification payload.如果未在通知负载中设置图标,则设置自定义默认图标允许您指定用于通知消息的图标。 Also use the custom default icon to set the icon used by notification messages sent from the Firebase console.还可以使用自定义默认图标来设置从 Firebase 控制台发送的通知消息所使用的图标。 If no custom default icon is set and no icon is set in the notification payload, the application icon (rendered in white) is used.如果未设置自定义默认图标且未在通知负载中设置图标,则使用应用程序图标(呈现为白色)。

Custom default Color自定义默认颜色

You can also define what color is used with your notification.您还可以定义通知使用的颜色。 Different android versions use this settings in different ways: Android < N use this as background color for the icon.不同的 android 版本以不同的方式使用此设置:Android < N 将此用作图标的背景颜色。 Android >= N use this to color the icon and the app name. Android >= N 使用它来为图标和应用程序名称着色。

Try putting that in your AndroidManifest.xml尝试将其放入您的AndroidManifest.xml

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<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.  -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

You can find more info here .您可以在此处找到更多信息。

You also need to have your colorAccent define.您还需要定义colorAccent You can create a colors.xml file in your res folder like that:您可以在 res 文件夹中创建一个colors.xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorAccent">#E91E63</color>
</resources>

In this folder: /android/app/src/main/res/values在此文件夹中: /android/app/src/main/res/values

Also the icon must be in the drawable folder.此外,图标必须在drawable文件夹中。

But remember that this icon must be white with a transparent background .但请记住,这个图标必须是white背景透明的

For some type of icons to be colored you need on some device sets meta-tags you already added in yuor AndroidManifest.xml and your icon must follow some specifications (must be white in a transparent background).对于要着色的某些类型的图标,您需要在某些设备上设置已添加到您或AndroidManifest.xml meta-tags ,并且您的图标必须遵循某些规范(必须在透明背景中为白色)。 You can try to generate one here .您可以尝试在此处生成一个。 Give it a try.试一试。 Remember to put it on drawable folder and update the manifest meta-tag icon name.请记住将其放在 drawable 文件夹中并更新清单meta-tag图标名称。

Read also this SO question that stengthens up what I've said here.另请阅读这个SO 问题,它加强了我在这里所说的内容。

I've tried on the Pixel 2 emulator running API level 27:我试过运行 API 级别 27 的 Pixel 2 模拟器:

在此处输入图片说明

Hope it helps.希望能帮助到你。

I know this question is regarding Firebase notification but since on searching the same for local_notifiction grey icon error this post came ...so finally I was able to find the solution to that(in my case icons work fine in debug mode but on building apk icons become grey boxes)我知道这个问题与 Firebase 通知有关,但是因为在搜索相同的 local_notifiction 灰色图标错误时,这篇文章来了......所以最后我找到了解决方案(在我的情况下,图标在调试模式下工作正常,但在构建 apk 时图标变成灰色框)

Solution:- in Android/app/src/main/res create a new directory named raw and in that directory add a file named keep.xml and copy paste the following:-解决方案:- 在Android/app/src/main/res 中创建一个名为raw的新目录,并在该目录中添加一个名为keep.xml的文件并复制粘贴以下内容:-

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@drawable/*" />

I did the following and it worked for me:我做了以下工作,它对我有用:

  1. Create a transparent and white notification icon (you can use the following tool: AndroidAssetStudio )创建一个透明的白色通知图标(可以使用以下工具: AndroidAssetStudio )

Download the zip folder, unzip and you'll see it contains a res folder with different drawable folders.下载 zip 文件夹,解压,你会看到它包含一个 res 文件夹,里面有不同的 drawable 文件夹。 Copy and paste the contents of the res folder in "android\\app\\src\\main\\res" path将res文件夹的内容复制粘贴到“android\\app\\src\\main\\res”路径下

文件夹结构

  1. Then open the AndroidManifest.xml file and add the following lines to it:然后打开 AndroidManifest.xml 文件并向其中添加以下行:

ic_stat_calendar_today is the name of my notification icon. ic_stat_calendar_today 是我的通知图标的名称。 And each of the drawable folders that have been pasted contain a different size of icon with the same name.并且每个粘贴的可绘制文件夹都包含不同大小的同名图标。

安卓清单

  1. If you want to change the color of the icon then check the above image.如果要更改图标的颜色,请查看上图。 Add the metadata tag after the notification icon tag在通知图标标签后添加元数据标签

  2. Go to "android\\app\\src\\main\\res\\values" and add a colors.xml file转到“android\\app\\src\\main\\res\\values”并添加一个colors.xml文件

颜色文件

<color name="colorAccent">#00FF00</color>

I have shared this answer in the following Github chain as well- Solution .我也在以下 Github 链中分享了这个答案 - 解决方案

Majority are making one single problem.大多数人都在制造一个问题。

They are placing the meta tags他们正在放置元标记

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

Inside the tags while they should be placing it above and inside the <application tag fe在标签内部,而他们应该将其放置在 <application 标签 fe 的上方和内部

    package="xxxx">
   <application
        android:label="xxxx"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
       <meta-data
           android:name="com.google.firebase.messaging.default_notification_icon"
           android:resource="@drawable/ic_notification" />
       <meta-data
           android:name="com.google.firebase.messaging.default_notification_color"
           android:resource="@color/colorAccent" />
        <activity
            android:name="xxxx"
            android:exported="true"
            android:launchMode="singleTask"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <meta-data
                android:name="com.google.android.gms.wallet.api.enabled"
                android:value="true" />
            <!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="flutterstripe" android:host="safepay" />
                <data
                    android:scheme="https"
                    android:host="xxxxx" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
```` </manifest>


Also its important to create separate file colors.xml and include color of your choice.
```` <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="splash_color">#00ABC8</color>
 </resources>



Rebuild app (close and run again)

and it will work.

add this in android/app/build.gradle in section "buildTypes" if not show icon only in release:如果仅在发行版中不显示图标,请在“buildTypes”部分的 android/app/build.gradle 中添加此内容:

shrinkResources false收缩资源错误

buildTypes {
    release {
        shrinkResources false  //  <------------

    }
}

In my case, the issue was that I was placing the就我而言,问题是我将

<meta-data android:name="com.google.firebase.messaging.default_notification_icon" … />

tag under the main <activity/> tag in the AndroidManifest.xml AndroidManifest.xml 中主<activity/>标签下的标签

Apparently, it should be under the <application/> tag (higher in the hierarchy).显然,它应该在<application/>标签下(在层次结构中较高)。

For anyone who is still having problems after adding the:对于添加后仍有问题的任何人:

<meta-data android:name="com.google.firebase.messaging.default_notification_icon" … />

Please make sure you are adding it directly under <application/> tag, instead of an <activity/> tag, in the AndroidManifest.xml请确保在 AndroidManifest.xml 中直接将其添加到<application/>标签下,而不是<activity/>标签下

Here is my finding.这是我的发现。

  1. Use AndroidAssetStudio to generate notification icon.使用AndroidAssetStudio生成通知图标。

  2. Try putting that in your AndroidManifest.xml试着把它放在你的AndroidManifest.xml


<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. -->
<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.  -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

Note: Above changes should be below <application> not in <activity> .注意:以上更改应低于<application>而不是<activity>

  1. Also check the minSdkVersion.还要检查 minSdkVersion。 When i use minSdkVersion 20 it was not working and when i set to minSdkVersion 21 it was work fine.当我使用minSdkVersion 20时它不工作,当我设置为minSdkVersion 21时它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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