19

I'm using Firebase Cloud Messaging to send notifications to my Flutter app. It works fine but my app icon notification is grey on the 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.

So is it possible to show my app icon for notifications with the color?

--Thanks in advance

Prince Hodonou
  • 615
  • 1
  • 9
  • 15

7 Answers7

23

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. 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 >= N use this to color the icon and the app name.

Try putting that in your 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. You can create a colors.xml file in your res folder like that:

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

In this folder: /android/app/src/main/res/values

Also the icon must be in the drawable folder.

But remember that this icon must be white with a transparent background.

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). 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.

Read also this SO question that stengthens up what I've said here.

I've tried on the Pixel 2 emulator running API level 27:

enter image description here

Hope it helps.

shadowsheep
  • 14,048
  • 3
  • 67
  • 77
  • 1
    Thanks for the answer, but I'm getting a `error: resource color/colorAccent (aka com.package.package:color/colorAccent) not found` error. Where do I add the color resource? – Prince Hodonou Jan 05 '19 at 22:35
  • I'l looking at Stack Overflow's notification icon. It's also greyed out. Maybe this is how it's supposed to be on newer versions of Android? But then again, snapchat, FB and others have color. – Prince Hodonou Jan 05 '19 at 22:37
  • @PrinceHodonou Btw, if you didn't have colorAccent defined you get that error. If you use AS. Give it a try. I'll update my answer so that you can see what to do. – shadowsheep Jan 05 '19 at 22:40
  • I've tried it as such : ` ` It compiles and everything, but the icon is still grey. Thanks for your help though! – Prince Hodonou Jan 05 '19 at 22:57
  • @PrinceHodonou so strange. Unfortunately I'm not so lucky to have a Pixel XL to give it a try ^_^. On different devices do you see the icon in true colors or alway in bw? May I ask you if you set the icon onto notification JSON payload or not? By now all my trials with flutter FCM, without setting the icon in JSON payload, give me the flutter icon in bright colors on my devices. – shadowsheep Jan 05 '19 at 23:16
  • Haha, I've ran the app on an older Android device and the icon is in color. No, I didn't set the icon in my payload. I didn't even know you can do that haha. So when you work with FCM, the icons are all in colors? This is very weird indeed. :) – Prince Hodonou Jan 05 '19 at 23:26
  • @PrinceHodonou for these [icons](https://developer.android.com/guide/topics/ui/notifiers/notifications) to be colored you need on some device sets meta-tags you already added in yuor manifest.xml and your icon must follow some specifications. You can try to generate one [here](https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit). Give it a try. Remember to put it on drawable folder and update the manifest meta-tag. Let me know. – shadowsheep Jan 05 '19 at 23:45
  • Thank you very much for this answer. What finally did it for me was to use the icon generator. My normal icon would not work, and I actually needed to remove the background from it for it to even show up in the generator. No wonder it kept showing up as grey/white. – Jason Simpson Jan 22 '20 at 03:06
9

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)

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:-

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@drawable/*" />
Pushkara Sharma
  • 101
  • 1
  • 2
  • You literally made my day and saved me a day of searching. But just out of curiosity how did you figure this out? – techpool Aug 26 '20 at 02:08
  • 2
    just lot of searching and hit and trials. And when I find the answer I just get back to this question so that no one else has to face the same. Thanks @techpool for appreciation, – Pushkara Sharma Sep 12 '20 at 18:56
  • Why this doesn't work with "tools:keep="@mipmap/*"" ? – Ania Feb 09 '21 at 10:05
7

I did the following and it worked for me:

  1. Create a transparent and white notification icon (you can use the following tool: AndroidAssetStudio )

Download the zip folder, unzip and you'll see it contains a res folder with different drawable folders. Copy and paste the contents of the res folder in "android\app\src\main\res" path

Folder Structure

  1. Then open the AndroidManifest.xml file and add the following lines to it:

ic_stat_calendar_today is the name of my notification icon. And each of the drawable folders that have been pasted contain a different size of icon with the same name.

Android Manifest

  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

colors.xml

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

I have shared this answer in the following Github chain as well- Solution.

user2549980
  • 593
  • 7
  • 20
2

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 f.e.

    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.
petras J
  • 2,538
  • 3
  • 16
  • 23
1

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

Volodymyr Lymar
  • 312
  • 1
  • 5
  • 10
1

Here is my finding.

  1. Use AndroidAssetStudio to generate notification icon.

  2. Try putting that in your 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>.

  1. Also check the minSdkVersion. When i use minSdkVersion 20 it was not working and when i set to minSdkVersion 21 it was work fine.
Abhijeet Navgire
  • 683
  • 7
  • 20
0

add this in android/app/build.gradle in section "buildTypes" if not show icon only in release:

shrinkResources false

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

    }
}