【问题标题】:Android notification icon colour sometimes white, sometimes colourfulAndroid 通知图标颜色有时为白色,有时为彩色
【发布时间】:2016-06-13 18:24:31
【问题描述】:

我的 Android 应用有一个小问题。它通过 FCM 接收通知并将其显示为推送通知。到目前为止一切正常,但奇怪的问题是,有时图标是白色的,有时是彩色的。

当应用程序在屏幕上打开并且此时我收到推送通知时,屏幕顶部会显示彩色的推送通知。

当应用程序关闭时,我会收到一个带有白色图标的推送通知。

我附上了截图: Screenshot

这里是创建推送通知的代码sn-p:

        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());

我的移动设备是 Android 6.0.1,我的 SDK 版本是 23。

感谢您的帮助。

【问题讨论】:

    标签: android android-notifications


    【解决方案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'
        ];
    

    现在我在应用内以及应用关闭时都会看到一个浅蓝色背景图标。

    【讨论】:

      【解决方案2】:

      按照google的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());
      

      【讨论】:

        【解决方案3】:

        我认为更好的解决方案是在应用程序中添加一个剪影图标,并在设备运行 Android Lollipop 时使用它。

        例如:

        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;
        }
        

        【讨论】:

        • 您应该在所有版本的 Android 上都使用剪影 - 应用图标从来都不是正确使用的小图标。
        • 你至少应该提到你的答案来自stackoverflow.com/a/29207365/976367