【问题标题】:Android Notification is not showing Colour Icon in MarshmallowAndroid 通知未在棉花糖中显示颜色图标
【发布时间】:2015-11-25 10:24:52
【问题描述】:

我正在制作应用程序,我从Parse 获取数据并将该数据传输到通知以生成并显示给用户。

但由于某种原因,我无法在棉花糖中显示正确的彩色图标

在其他所有 Android 版本中,它都运行良好,但在 Marshmallow 中,它令人毛骨悚然的白色图标不是我选择的实际。

这是我的通知代码。

 Intent cIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentText(data)
                .setContentTitle("Notification from Parse")
                .setContentIntent(pendingIntent);

        Notification notification = builder.build();
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(100, notification);

请帮我解决这个问题,或者告诉我任何其他方法可以解决这个问题。

【问题讨论】:

    标签: android parse-platform notifications push-notification


    【解决方案1】:

    首先:它不是来自 Marshmallow,通知图标开始从 Lollipop 本身变成白色。

    结帐http://developer.android.com/design/style/iconography.html 您会看到白色样式是通知在Android Lollipop 中的显示方式。

    在 Android Lollipop 中,Google 还建议您使用将显示在(白色)通知图标后面的颜色 - https://developer.android.com/about/versions/android-5.0-changes.html

    第二个:解决方案是将LargeIcon设置为通知生成器

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setLargeIcon(largeIcon)
                    .setContentText(data)
                    .setContentTitle("Notification from Parse")
                    .setContentIntent(pendingIntent);
    

    然后,您的通知将如下所示:

    您也可以使用.setColor()设置通知图标的背景颜色。

    【讨论】:

    • 你的工作真棒
    • 但是右下角还有一个小图标,无论如何都可以删除它
    • 不,您不能,因为实际上当您的手机被锁定并出现通知时,该图标会显示在您的通知中。
    • 是的,太好了.. 但是我的老板看到 Twitter 和 Facebook 之类的通知带有正确的图标。所以他们希望我也这样做,你能帮我吗
    • 在 Facebook 和 twitter 中,他们根据各自的主题颜色(Facebook 为深蓝色,Twitter 为浅蓝色)应用了setColor。他们没有使用setLargeIcon
    【解决方案2】:

    在某些型号(如 Pixel 等)上,您只能添加一种颜色,只需添加:

    .setColor(ContextCompat.getColor(context, R.color.your_color))

    【讨论】:

      猜你喜欢
      • 2019-08-31
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多