2016-06-13 97 views
1

我的Android應用程序出現小問題。它通過FCM收到通知並將它們顯示爲推送通知。到目前爲止,這一切都是有效的,但奇怪的問題是,有時圖標是白色的,有時它是多彩的。Android通知圖標顏色有時是白色的,有時是多彩的

當應用程序在屏幕上打開並且此時收到推送通知時,多彩推送通知會顯示在屏幕頂部。

當應用程序關閉時,我收到帶有白色圖標的推送通知。

我附上一個screenhot: Screenshot

這裏是代碼片段,在其中創建推送通知:

 Notification.Builder notificationBuilder = new Notification.Builder(this) 
      .setSmallIcon(android.R.drawable.ic_dialog_alert) 
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) 
      .setAutoCancel(true) 
      .setVisibility(Notification.VISIBILITY_PUBLIC) 
      .setPriority(Notification.PRIORITY_HIGH) 
      .setColor(Color.parseColor("#83c3ed")) 
      .setLights(Color.RED, 1000, 500) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 
    Notification.InboxStyle inboxStyle = new Notification.InboxStyle(); 
    inboxStyle.setBigContentTitle("WetterApp"); 
    inboxStyle.addLine(notification.getTitle()); 
    inboxStyle.addLine(notification.getBody()); 
    notificationBuilder.setStyle(inboxStyle); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0, notificationBuilder.build()); 

我的移動設備已經安卓6.0.1,我的SDK版本是23.

感謝您的幫助。

回答

1

嗯,我認爲這個問題是不同的東西。只有當應用程序在屏幕上打開時,纔會調用創建通知的代碼。當我收到通知並關閉應用程序時,通知由Android系統自動處理。

我必須設置在通知中的顏色,這是發送到服務器FCM:

$data = [ 
     'notification' => [ 
      'title' => 'Warnung: Wohnzimmer', 
      'text' => 'Innen: 20,3°C Außen: 24,5°C, Tendenz: -0,2°C', 
      'color' => '#83c3ed', 
      'sound' => 'default' 
     ], 
     'to' => '/topics/testNotification' 
    ]; 

現在,我得到了應用程序內的lightblue背景圖標,也當應用程序被關閉。

-1

我認爲更好的解決方案是在應用程序中添加輪廓圖標並在設備運行Android棒棒糖時使用它。

例如:

Notification notification = new Notification.Builder(context) 
      .setAutoCancel(true) 
      .setContentTitle("My notification") 
      .setContentText("Look, white in Lollipop, else color!") 
      .setSmallIcon(getNotificationIcon()) 
      .build(); 

    return notification; 

而且,在getNotificationIcon方法:

private int getNotificationIcon() { 
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP); 
    return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher; 
} 
+0

您應該在所有版本的Android上使用剪影 - 應用程序圖標從來都不是正確的小圖標。 – ianhanniballake

+2

您至少應該提及您從http://stackoverflow.com/a/29207365/976367獲得了答案 –

0

效仿谷歌的guide創建你的圖標,它可能是,它是在狀態欄中出現了。

那麼,試試這個:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
      builder.setTicker(context.getResources().getString(R.string.app_name)); 
      builder.setSmallIcon(R.mipmap.ic_your_status_bar_logo); 
      builder.setAutoCancel(true); 
      builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); 
      builder.setContentIntent(pendingIntent);   
     builder.setContentTitle(
context.getResources().getString(R.string.app_name)); 
      builder.setContentText(message); 
      builder.setDefaults(Notification.DEFAULT_SOUND); 
      builder.setPriority(NotificationCompat.PRIORITY_HIGH); 
      builder.setColor(ContextCompat.getColor(context, R.color.color_primary)); 
      NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      nm.notify(NOTIFICATION_ID, builder.build()); 
相關問題