Android通知中心推送的图标为灰色

3
我创建了一个应用程序,当你按下按钮时,它应该发送推送通知。它确实可以发送,但存在一个问题:通知中心中的图标变灰。但是,当我在应用程序中并查看左上角时,图标是存在的。
我的Android版本是9,因此这不是安卓棒棒糖问题。
一些图片:
通知中心中的推送通知:

Push notification in notification center

在应用程序中的推送通知:

Push notification in notification center

这个人遇到了同样的问题,通过将图像的透明/白色版本放入res文件夹中解决了这个问题。我也这样做了(它仍然在那里,所以你可以检查我是否做对了),但对我没有用:

为什么我的通知小图标始终是灰色的?

我找不到确切的问题,但有人建议在清单文件中加入一行代码(也仍然在那里,如果我做对了,你可以查看):

<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/whatsapp_icon" />

MainActivity.java:

package Package name that I chose;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        NotificationManager NM;
        Notification Note;

        NM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");

        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);

        String CHANNEL_ID = "my_channel_01";
        CharSequence name = "my_channel";
        String Description = "This is my channel";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        mChannel.setDescription(Description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mChannel.setShowBadge(true);
        NM.createNotificationChannel(mChannel);

        Notification.Builder builder = new Notification.Builder(MainActivity.this, CHANNEL_ID);

        builder.setAutoCancel(false);
        builder.setTicker("this is ticker text");
        builder.setContentTitle("WhatsApp Notification");
        builder.setContentText("You have a new message");
        builder.setSmallIcon(R.mipmap.whatsapp_icon);
//        builder.setLargeIcon(R.mipmap.whatsapp_icon_round);
        builder.setContentIntent(pendingIntent);
        builder.setOngoing(false);
        builder.setSubText("This is subtext...");   //API level 16
        builder.setNumber(100);
        builder.build();

        Note = builder.getNotification();
        NM.notify(11, Note);
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ste999.whatsapprelay">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/whatsapp_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/whatsapp_icon_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/whatsapp_icon" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

这里有一个资源文件夹的链接。

我希望有人能指出我的错误,并使得图标也能在通知中心中显示,而不是一个灰色的圆圈。

1个回答

0

1
那么其他应用程序如何在通知中为它们的图标添加颜色呢?我已经尝试过了,也许你可以下载我的资源文件夹并查看我是否将其放错了文件夹? - novun
@novun 应用程序图标和通知图标是清单文件中的两个不同事物。设置您创建的通知图标,例如在我的情况下:<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/ic_launcher_background" /> - Kailash Chouhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接