2016-06-13 179 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()); 
相关问题