簡體   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