繁体   English   中英

Firebase 通知为灰色 flutter

[英]Firebase Notification is grey flutter

我正在使用 Firebase Cloud Messaging 向我的 Flutter 应用程序发送通知。 它工作正常,但我的应用程序图标通知在 Google Pixel XL 上是灰色的。 我可以看到轮廓,但由于某种原因,它仍然是灰色的。 我在不同的文章中读到它与 Android SDK 级别有关,但是 Flutter 中的 minSdkVersion 已经是 16 了(为什么我不明白为什么会显示 2')

那么是否可以用颜色显示我的应用程序图标通知?

- 提前致谢

在这里你可以阅读:

自定义默认通知

自定义默认图标

如果未在通知负载中设置图标,则设置自定义默认图标允许您指定用于通知消息的图标。 还可以使用自定义默认图标来设置从 Firebase 控制台发送的通知消息所使用的图标。 如果未设置自定义默认图标且未在通知负载中设置图标,则使用应用程序图标(呈现为白色)。

自定义默认颜色

您还可以定义通知使用的颜色。 不同的 android 版本以不同的方式使用此设置:Android < N 将此用作图标的背景颜色。 Android >= N 使用它来为图标和应用程序名称着色。

尝试将其放入您的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" />

您可以在此处找到更多信息。

您还需要定义colorAccent 您可以在 res 文件夹中创建一个colors.xml文件,如下所示:

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

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

此外,图标必须在drawable文件夹中。

但请记住,这个图标必须是white背景透明的

对于要着色的某些类型的图标,您需要在某些设备上设置已添加到您或AndroidManifest.xml meta-tags ,并且您的图标必须遵循某些规范(必须在透明背景中为白色)。 您可以尝试在此处生成一个。 试一试。 请记住将其放在 drawable 文件夹中并更新清单meta-tag图标名称。

另请阅读这个SO 问题,它加强了我在这里所说的内容。

我试过运行 API 级别 27 的 Pixel 2 模拟器:

在此处输入图片说明

希望能帮助到你。

我知道这个问题与 Firebase 通知有关,但是因为在搜索相同的 local_notifiction 灰色图标错误时,这篇文章来了......所以最后我找到了解决方案(在我的情况下,图标在调试模式下工作正常,但在构建 apk 时图标变成灰色框)

解决方案:- 在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/*" />

我做了以下工作,它对我有用:

  1. 创建一个透明的白色通知图标(可以使用以下工具: AndroidAssetStudio )

下载 zip 文件夹,解压,你会看到它包含一个 res 文件夹,里面有不同的 drawable 文件夹。 将res文件夹的内容复制粘贴到“android\\app\\src\\main\\res”路径下

文件夹结构

  1. 然后打开 AndroidManifest.xml 文件并向其中添加以下行:

ic_stat_calendar_today 是我的通知图标的名称。 并且每个粘贴的可绘制文件夹都包含不同大小的同名图标。

安卓清单

  1. 如果要更改图标的颜色,请查看上图。 在通知图标标签后添加元数据标签

  2. 转到“android\\app\\src\\main\\res\\values”并添加一个colors.xml文件

颜色文件

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

我也在以下 Github 链中分享了这个答案 - 解决方案

大多数人都在制造一个问题。

他们正在放置元标记

<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" />

在标签内部,而他们应该将其放置在 <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.

如果仅在发行版中不显示图标,请在“buildTypes”部分的 android/app/build.gradle 中添加此内容:

收缩资源错误

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

    }
}

就我而言,问题是我将

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

AndroidManifest.xml 中主<activity/>标签下的标签

显然,它应该在<application/>标签下(在层次结构中较高)。

对于添加后仍有问题的任何人:

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

请确保在 AndroidManifest.xml 中直接将其添加到<application/>标签下,而不是<activity/>标签下

这是我的发现。

  1. 使用AndroidAssetStudio生成通知图标。

  2. 试着把它放在你的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" />

注意:以上更改应低于<application>而不是<activity>

  1. 还要检查 minSdkVersion。 当我使用minSdkVersion 20时它不工作,当我设置为minSdkVersion 21时它工作正常。

暂无
暂无

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

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