Q: Change notification icon in Android Studio

2019-06-09 19:58发布

问题:

I configured the application icon (Image 1), but the notification icon (Notification sent via Firebase) shows a gray rectangle (Image 2). What is the procedure for changing the notification icon image (Image 2) via Android Studio 2.3 or via script?

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.studioshow.studioshow">
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />


        </intent-filter>
    </activity>
</application>

回答1:

In manifest file. Set meta data. Following code is my case.

   <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"  >
        <activity
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

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

    </application>


回答2:

EDIT: My answer is not relevant: I did not read about the Firebase messaging part.


Simply use setSmallIcon(int), provided by NotificationBuilder and its Compat counterpart.

You can retrieve the icon from your resource folder by passing, e.g. R.drawable.my_icon as parameter.