奥利奥通知栏圆形图标

7

我想知道如何在安卓 Oreo (API 26) 中更改通知栏小图标。默认情况下,它显示为圆形白色图标。我该如何更改它?清单文件中默认的通知图标设置如下。

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

请查看下面的图片:

截图


这是通知栏截图的链接。https://ibb.co/d4i2P6 - Shanu
这个问题只在8.0上出现吗?您在其他5+设备上检查过吗?您的通知图标中有任何alpha值吗? - Srikar Reddy
是的,这只发生在Android Oreo设备上,在API 26以下的设备上,图标显示正确。 - Shanu
你能试试移除元数据吗? - propoLis
如果元数据被删除,当应用程序在后台或已关闭时,它是否会显示通知图标? - Shanu
@Shanu,当应用程序在后台运行时,你能解决这个问题吗? - Umar Hussain
2个回答

5

在Android 8.0(API 26)上,Firebase SDK 11.8.0存在一个bug,导致状态栏中显示的是白色版本的应用启动图标而非通知图标。

screenshot 1

有些人通过重写Application类的getResources()方法来解决这个问题。

我使用的另一种方法是使用HTTP POST请求将通知作为数据消息发送:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
   "to": "/topics/test",
   "data": {
       "title": "Update",
       "body": "New feature available"
    }
 }

然后,通过子类化 FirebaseMessagingService,以编程方式显示通知:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent i = new Intent(context, HomeActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 
                                         PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder builder = 
            new NotificationCompat.Builder(this, GENERAL_CHANNEL_ID)
                 .setSmallIcon(R.drawable.ic_stat_chameleon)
                 .setContentTitle(remoteMessage.getData().get("title"))
                 .setContentText(remoteMessage.getData().get("body"))
                 .setContentIntent(pi);

        NotificationManager manager = 
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
}

最终,我在Android 8.0上得到了这个结果: screenshot 2

-4
  1. AndroidManifest.xml中删除<meta-data>标签。
  2. 通知构建器有.setSmallIcon(int icon, int level),它接受图标作为必需参数和级别参数作为可选参数。

注意: .setSmallIcon 接受可绘制对象不接受mipmap

根据Android 8.0 Oreo Notification Channels和行为更改,应该是这样的:

public class MainActivity extends AppCompatActivity {

    private CharSequence name;
    private int notifyId;
    private int importance;
    private String id;
    private String description;

    private Notification mNotification;
    private NotificationManager mNotificationManager;
    private NotificationChannel mChannel;
    private PendingIntent mPendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.btnNotification.setOnClickListener(v -> notification());
    }

    private void notification() {

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notifyId = 1;
        description = "Hello World, welcome to Android Oreo!";

        Intent intent = new Intent(this, MainActivity.class);
        mPendingIntent = PendingIntent.getActivity(this, notifyId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        if (SDK_INT >= O) {
            id = "id";
            name = "a";
            importance = NotificationManager.IMPORTANCE_HIGH;

            mChannel = new NotificationChannel(id, name, importance);
            mChannel.setDescription(description);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.WHITE);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[] {100, 300, 200, 300});
            mNotificationManager.createNotificationChannel(mChannel);

            mNotification = new Notification.Builder(MainActivity.this, id)
                .setContentTitle(id)
                .setContentText(description)
                .setContentIntent(mPendingIntent)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .build();
        } else {
            mNotification = new Notification.Builder(MainActivity.this)
                .setContentTitle(id)
                .setContentText(description)
                .setContentIntent(mPendingIntent)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setLights(Color.WHITE, Color.RED, Color.GREEN)
                .setVibrate(new long[] {100, 300, 200, 300})
                .build();
        }
            mNotificationManager.notify(notifyId, mNotification);
    }
}

这里有点混淆,我想要更改的活动是哪一个?是我的MainActivity类还是FirebaseMessagingService类? - Shanu
你可以在任何想要触发它的地方放置 notification() 方法。我已经将其放置在一个活动中作为示例,当我点击相关按钮时通知会被触发。你可以通过服务或活动来触发通知。 - Sneh Pandya
我已将它放置在FirebaseMessagingService类中,并在onMessageReceived方法中调用它。当应用程序在前台时,它可以工作,但当应用程序在后台或被杀死时,它无法工作。 - Shanu
谢谢您先生,它正在运行。但在Android Oreo中,它只能使用小图标。感谢您的帮助。 - Shanu
@SnehPandya,您的解决方案如何解决应用程序不在前台时通知到系统托盘的问题? - Umar Hussain
显示剩余3条评论

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