繁体   English   中英

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

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

我知道要支持棒棒糖材料设计指南,我们必须使通知图标透明。

这是我的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;
}

但是,这里的问题是,当应用程序在前台运行且可见时,它将采用我的logo_a_transparent,并会得到所需的结果(屏幕截图-通知栏中的第一个图标)。

但是,当我们暂停应用程序并进行FCM推送时,它将我的应用程序图标(android:icon =“ @ drawable / ic_launcher”)作为通知图标变为白色(屏幕截图-通知栏中的第二个图标)。

将应用程序图标替换为透明将起作用,但不是正确的解决方案。

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

在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" />

使用FCM,您可以向客户发送两种类型的消息

1)通知消息,2)数据消息, 此处为fcm重复

通知消息仅在应用程序为前台时才调用onMessageReceived()。 当应用程序在后台运行时,通知消息会传递到通知托盘,而该应用程序是由Android系统自动处理的,而不是调用onMessageReceived()。系统使用应用程序图标作为通知图标,这就是为什么该图标在后台推送时变为白色的原因。 android应用程序不必透明。

对于数据消息,无论应用程序是在后台还是在前台,它将始终由onMessageReceived()处理。

数据信息

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

因此,我应该使用仅数据消息有效负载或具有通知和数据有效负载的Messages,以便我的onMessageReceived()可以处理此问题,并显示正确的通知图标。

在Firebase 12.0.0处修复。 只需将build.gradle更新为12.0.0

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

暂无
暂无

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

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