2017-08-25 122 views
3

我正在爲應用程序創建用戶通知。我想讓圖標在狀態欄中顯示爲白色,但在下拉通知菜單中顯示時顯示爲藍色。以下是Google商店應用所執行的相同事情的示例。在狀態欄Android顏色通知圖標

白通知:

enter image description here

我怎麼能複製這樣的:在下拉菜單中

enter image description here

有色通知?我必須設置哪些屬性?

編輯: 這裏是我當前的代碼 - 我所做的圖像都是白色透明背景,所以它看起來很好在狀態欄,但在通知中下降,圖像仍然是相同的白色:

private NotificationCompat.Builder getNotificationBuilder() { 
     return new NotificationCompat.Builder(mainActivity) 
       .setDeleteIntent(deletedPendingIntent) 
       .setContentIntent(startChatPendingIntent) 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.skylight_notification) 
       .setColor(ContextCompat.getColor(mainActivity, R.color.colorPrimary)) 
       .setContentTitle(mainActivity.getString(R.string.notification_title)) 
       .setContentText(mainActivity.getString(R.string.notification_prompt)); 
    } 
+0

我能解決這個問題 - 請我的回答如下。 – Oblivionkey3

回答

4

我找到了答案,我的問題在這裏:https://stackoverflow.com/a/44950197/4394594

我並不完全知道問題是什麼,但把我正在使用的圖標進入這個工具https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=image&source.space.trim=1&source.space.pad=0&name=ic_skylight_notification 巨大PNG,並通過將所產生的它給我的mipmap文件夾的圖標,我能夠讓setColor(...)屬性正常工作。

+1

謝謝!我花了太長時間試圖弄清爲什麼我的通知圖標仍然在通知橫幅中顯示爲白色,最後使用該工具,我的通知圖標正確使用了橫幅中的顏色(雖然狀態欄中仍爲白色) –

+0

謝謝!我也花了太長時間! – user1032613

0

建立通知時,可以設置顏色和圖標。如果您的圖標是純白色圖像,則會在正確的位置爲您應用顏色。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
      val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 
      val notificationId = 10 // Some unique id. 

      // Creating a channel - required for O's notifications. 
      val channel = NotificationChannel("my_channel_01", 
        "Channel human readable title", 
        NotificationManager.IMPORTANCE_DEFAULT) 

      manager.createNotificationChannel(channel) 

      // Building the notification. 
      val builder = Notification.Builder(context, channel.id) 
      builder.setContentTitle("Warning!") 
      builder.setContentText("This is a bad notification!") 
      builder.setSmallIcon(R.drawable.skull) 
      builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary)) 
      builder.setChannelId(channel.id) 

      // Posting the notification. 
      manager.notify(notificationId, builder.build()) 

     } 
+0

不幸的是,所有改變顏色的方法都是在通知下拉菜單中改變圖像旁邊文本的顏色,而不是完全改變圖像的顏色。 – Oblivionkey3

+0

您是否使用純白色圖標作爲小圖標?它應該在需要時着色。 –

+0

這是純白色的透明背景,它保存爲PNG – Oblivionkey3

0

在設置drawable之前,可以使用drawable的DrawableCompat.setTint(int drawable);。 並做mutate()提拉否則色調將被應用到的那繪製

+0

我建議不要這樣做,否則在狀態欄中會出現彩色圖標。 –

2

這裏的每一個實例是我做我的應用程序...隨着顏色

private void showNotification(Context context) { 
    Log.d(MainActivity.APP_TAG, "Displaying Notification"); 
    Intent activityIntent = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 
    mBuilder.setSmallIcon(R.drawable.ic_notification); 
    mBuilder.setColor(Color.GREEN); 
    mBuilder.setContentIntent(pendingIntent); 
    mBuilder.setContentTitle("EarthQuakeAlert"); 
    mBuilder.setContentText("It's been a while you have checked out earthquake data!"); 
    mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
    mBuilder.setAutoCancel(true); 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(1, mBuilder.build()); 
} 

樣品:

enter image description here

樣品無顏色: enter image description here

1

對於從控制檯發送火力nofitications你只需要在您的清單補充一點:

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

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

哪裏white_logo是你的應用程序的白色標誌,以及CUSTOM_COLOR是你想擁有彩色圖標和文本的顏色。

更多細節在這裏:https://firebase.google.com/docs/cloud-messaging/android/client