简体   繁体   English

通知栏图标在Android中从FCM变为白色

[英]Notification bar icon turns white in Android from FCM

I know that to support Lollipop Material design guidelines we have to make notification icon as transparent. 我知道要支持棒棒糖材料设计指南,我们必须使通知图标透明。

Here is my FCM onMessageReceived() function to show noticication. 这是我的FCM onMessageReceived()函数,用于显示通知。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    NotificationCompat.Builder mBuilder;
    mBuilder =   new NotificationCompat.Builder(this)
            .setContentTitle(remoteMessage.getNotification().getBody()) // title for notification
            .setContentText(remoteMessage.getNotification().getTitle()) // message for notification
            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
            .setSmallIcon(getNotificationIcon())
            .setAutoCancel(true); // clear notification after click

    Intent intent = new Intent(this, CheckInListPage.class);
    PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
    mBuilder.setContentIntent(pi);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.logo_a_transparent : R.drawable.logo_notifc;
}

But here my issue is that when the application is running in foreground and visible, it will take my logo_a_transparent and will get desired result (screenshot - first icon in the notification bar). 但是,这里的问题是,当应用程序在前台运行且可见时,它将采用我的logo_a_transparent,并会得到所需的结果(屏幕截图-通知栏中的第一个图标)。

But when we are pausing the application and an FCM push came, It takes my app icon (android:icon="@drawable/ic_launcher") as notification icon became white (screenshot - second icon in the notification bar). 但是,当我们暂停应用程序并进行FCM推送时,它将我的应用程序图标(android:icon =“ @ drawable / ic_launcher”)作为通知图标变为白色(屏幕截图-通知栏中的第二个图标)。

Replacing app icon as transparent will work, But not a correct solution. 将应用程序图标替换为透明将起作用,但不是正确的解决方案。

两个通知图标来自同一应用程序。当应用程序为前台时,第一个图标为推送通知;当应用程序为后台时,第一个图标为推送通知

Add this line in your menifestfile set your resource as your choice add this one 在menifestfile中添加这一行,将您的资源设置为您的选择,然后添加此行

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

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

With FCM, you can send two types of messages to clients applicatin 使用FCM,您可以向客户发送两种类型的消息

1) Notification messages, 2) Data messages Here fcm doumentation 1)通知消息,2)数据消息, 此处为fcm重复

Notification Message calls onMessageReceived() only when the application is foreground. 通知消息仅在应用程序为前台时才调用onMessageReceived()。 Notification messages are delivered to the notification tray when the app is in the background that's automatically handled by Android system rather than calling onMessageReceived(), The system uses app icon as notification icon that's why icon became white in background push. 当应用程序在后台运行时,通知消息会传递到通知托盘,而该应用程序是由Android系统自动处理的,而不是调用onMessageReceived()。系统使用应用程序图标作为通知图标,这就是为什么该图标在后台推送时变为白色的原因。 The android application needs not to be transparent. android应用程序不必透明。

In the case of Data Message whether the app is in the background or the foreground it will always be handled by onMessageReceived(). 对于数据消息,无论应用程序是在后台还是在前台,它将始终由onMessageReceived()处理。

Data Message 数据信息

  {  "to" : "Ahd8fsdfu78sd...", "data" : {
     "Name" : "...",
     "body" : "...",
    }
}

So I should use a data-only message payload or Messages with both notification and data payloads, so my onMessageReceived() can handle this and correct notification icon will displayed. 因此,我应该使用仅数据消息有效负载或具有通知和数据有效负载的Messages,以便我的onMessageReceived()可以处理此问题,并显示正确的通知图标。

Fix at firebase 12.0.0. 在Firebase 12.0.0处修复。 Just update your build.gradle to 12.0.0 只需将build.gradle更新为12.0.0

https://firebase.google.com/support/release-notes/android#20180320 https://firebase.google.com/support/release-notes/android#20180320

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM