安卓推送通知状态图标问题

3
我在使用Firebase推送通知时遇到问题, 问题: 应用程序图标在Android 6.0及以上的系统中会变成白色,因此我创建了白色和透明的图标集,现在推送通知的图标看起来符合我要求,但只有当应用程序在前台时才能看到。当用户收到通知并关闭应用程序时,图标仍然是白色。
我的清单文件:
<application
        android:name="com.xxxx"
        android:allowTaskReparenting="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_xxxx"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

通知代码:当我收到推送通知时

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder;
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.xxxxxx)
                        .setColor(getResources().getColor(R.color.accent))
                        .setContentTitle(messageTitle)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(contentIntent);
            } else {
                notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(messageTitle)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(contentIntent);
            }



            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(count, notificationBuilder.build());

收到消息时的代码

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Displaying data in log
        //It is optional
        Log.d(TAG, "Notification Message TITLE: " + remoteMessage.getNotification().getTitle());
        Log.d(TAG, "Notification Message BODY: " + remoteMessage.getNotification().getBody());
        Log.d(TAG, "Notification Message DATA: " + remoteMessage.getData().toString());

        //Calling method to generate notification
        //remoteMessage.getNotification().getBody()
        sendNotification(remoteMessage.getNotification().getTitle(),
                remoteMessage.getNotification().getBody(), remoteMessage.getData());
    }

你可以发一下你的 onMessageReceived 代码吗? - Bhuvanesh BS
你尝试过使用drawable文件夹(drawable/drawable-v21)上的平台添加图标吗?这是一种更干净的解决此问题的方法,不会依赖于运行时检查。 - GhostDerfel
@GhostDerfel:不是的,我把图标添加到了另一个文件夹中,即drawable-hdpi、drawable-ldpi等。 - Pratik Vyas
@GhostDerfel:没有进展,我尝试在drawable/drawable-v21上添加图标,但没有成功,然而当我将我的应用程序图标(在清单中)更改为透明和白色的图标时,背景推送显示了这一点,但这不是我想要的,我需要我的应用程序图标是彩色的。 - Pratik Vyas
2个回答

0

这里有两种类型的通知。

1.) 使用默认参数

    String title = remoteMessage.getNotification().getTitle();
    String body = remoteMessage.getNotification().getBody();

服务器端代码:

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

这种实现方式仅在应用程序打开或在后台运行时有效。你会收到消息接收的通知(如你所说,会出现空白通知),但你将无法从中获取任何数据。

2.) 使用地图

   Map data =  remoteMessage.getData();

服务器端代码:

{
  "message":{
    "token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification" : {
      "body" : "This is an FCM notification message!",
      "title" : "FCM Message",
      }
   }
}

这将在应用程序关闭后仍然接收到通知。它不会产生空白的白色通知。


是的,我知道,检查一下我的消息接收代码。 - Pratik Vyas
好的,就像我说的,当应用程序关闭时,您的消息接收代码将无法工作。您如何使用标题、正文和数据?您只能使用其中一种方法。 - Bhuvanesh BS
我和你使用的是相同的代码,并且在方法中使用了remoteMessage.getData()。remoteMessage.getNotification().getTitle()没有在实际方法中使用,请避免使用它。 - Pratik Vyas

0

如果通知图标显示为白色,尝试将以下代码添加到清单文件中

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

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