2017-04-19 97 views
0

我知道要支持棒棒糖材質設計指南,我們必須將通知圖標設置爲透明。通知欄圖標在Android中從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並會得到期望的結果(截圖 - 在通知欄第一個圖標)。當通知圖標變成白色(屏幕截圖 - 通知欄中的第二個圖標)時,它會將我的應用程序圖標(android:icon =「@ drawable/ic_launcher」)作爲暫停應用程序並進行FCM推送。

將應用程序圖標替換爲透明將起作用,但不是正確的解決方案。

Two notification icon are from the same application. The first icon is the push notification when application is foreground and other is when application background

+1

因爲您正在使用GetNotification()insteam使用getData()方法每次調用onmessageReceived,並且您可以使用相同的圖標 –

+0

請參閱此處:http://stackoverflow.com/questions/28387602/notification-bar-icon-turns -white-in-android-5-lollipop –

+0

我已經這樣做了......但是當我們暫停應用程序並且FCM推送到來時,通知圖標變爲白色(屏幕截圖 - 通知欄中的第二個圖標)。當應用程序前臺時,沒有問題。 –

回答

0

隨着FCM,您可以發送郵件的兩種類型的客戶applicatin

1)通知消息, 2)數據消息 Here fcm doumentation

通知消息僅在應用程序爲前景時調用onMessageReceived()。當應用處於由Android系統自動處理的背景中而不是調用onMessageReceived()時,通知消息會傳遞到通知托盤。系統使用應用圖標作爲通知圖標,這就是爲什麼圖標在背景推送中變爲白色的原因。 Android應用程序不需要是透明的。

的情況下數據消息無論應用程序在後臺還是前臺,它總是由onMessageReceived()處理。

數據消息

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

所以我應該使用專用於數據的消息有效載荷或與消息通知與數據有效載荷,所以我onMessageReceived()可以處理這個問題,正確的通知圖標會顯示出來。

0

加入這一行你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" /> 
+0

我已添加但不工作 –

+0

添加第二個元數據並嘗試...可能希望這一個是對你的幫助 –

+0

仍然無法正常工作...我的fcm版本是編譯'com.google.firebase:firebase-messaging:9.6.1' –

相關問題