【问题标题】:Android Color Notification IconAndroid 颜色通知图标
【发布时间】:2018-02-03 02:39:52
【问题描述】:

我正在开发一个为用户创建通知的应用程序。我希望图标在状态栏中显示为白色,但在下拉通知菜单中显示时显示为蓝色。以下是 Google Store 应用执行相同操作的示例。

状态栏中的白色通知:

下拉菜单中的彩色通知:

我怎样才能复制这个?我必须设置哪些属性?

编辑: 这是我当前的代码 - 我将图像全部设为白色,背景透明,因此在状态栏中看起来不错,但在通知下拉中,图像仍然是相同的白色:

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

【问题讨论】:

  • 我能够解决这个问题 - 请在下面查看我的答案。
  • 对于那些使用 admin sdk 进行通知的人!

标签: java android colors push-notification android-notifications


【解决方案1】:

我在这里找到了我的问题的答案:https://stackoverflow.com/a/44950197/4394594

我不完全知道问题出在哪里,但是通过将我用于图标的巨大 png 放入此工具 https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=image&source.space.trim=1&source.space.pad=0&name=ic_skylight_notification 通过将生成的图标放入我的 mipmap 文件夹中,我能够让 setColor(...) 属性正常工作。

【讨论】:

  • 谢谢!我花了太长时间试图弄清楚为什么我的通知图标在通知横幅中仍然显示为白色,最后使用该工具我的通知图标正确使用了横幅中的颜色(同时在状态栏中仍然是白色)
  • 致所有来到这里并使用 Photoshop(或任何其他图形设计工具)生成图标的人。不要使用 CMYK 模式!在创建/导出图标时使用 RGB - 这对我来说很有帮助
  • 我使用了这个工具,我的图像是纯白色透明PNG,我确保它在RGB色彩空间中,但SetColor仍然不起作用。我错过了什么?
  • 对于仍然卡住的人,请尝试缩小图像。 72x72 的图标不适合我,但 48x48 的图标可以。
【解决方案2】:

对于从控制台发送的 Firebase 通知,您只需将其添加到您的清单中:

    <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

【讨论】:

  • 添加了“Oblivionkey3”建议的图标,但在 adding &lt;meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/custom_color" /&gt; 图标颜色设置为自定义之后。在 Andriod Oreo 通知上此图标颜色为灰色之前(状态栏图标显示为白色)。
  • custom_color 是什么?以及如何设置?
  • @mukesh.kumar,custom_color 是您想要使用的任何颜色。你只需要在颜色文件中声明它
【解决方案3】:

这是我为我的应用所做的...

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

带颜色的样本:

没有颜色的样品:

【讨论】:

    【解决方案4】:

    在构建通知时,您可以设置颜色和图标。 如果您的图标是纯白色图像,它会在正确的位置为您应用颜色。

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

    【讨论】:

    • 不幸的是,更改颜色对我所做的只是更改通知下拉菜单中图像旁边的文本颜色,而不是更改图像的颜色。
    • 您是否使用纯白色图标作为您的小图标?它应该在需要时为它着色。
    • 纯白色,透明背景,保存为png格式
    • 我编辑了我的帖子,并在 O 上测试了一些代码,上面的代码可以让我的图标和文本在被拉下时都成为我的 color_primary,而在不被拉下时只是纯白色。希望这会有所帮助。
    • 上帝保佑你咨询狗。这就是我今晚需要的答案。
    【解决方案5】:

    我可能迟到了,但以上所有答案都不相关或不推荐使用。

    您可以使用NotificationCompat.BuildersetColor 方法轻松实现此目的

    例子:

    val builder = NotificationCompat.Builder(this, "whatever_channel_id")
            .setSmallIcon(R.drawable.ic_notification) //set icon for notification
            .setColor(ContextCompat.getColor(this, R.color.pink))
            .setContentTitle("Notification Title")
            .setContentText("Notification Message!")
    

    现在它会将通知显示为粉红色

    注意: 如果您使用的是 firebase,则不会直接看到颜色。您必须将其添加到清单文件中。

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

    【讨论】:

    • 他们应该改名为“setColor”“setIconColor”,这很明显。
    【解决方案6】:

    如果您想在推送通知或内置通知中根据 gmail 和 twitter 更改颜色和标题名称,则需要在通知中添加这些行。

     builder.setSmallIcon(R.drawable.skull)
        builder.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
    

    第一行用于图标,第二行需要定义颜色

    【讨论】:

      【解决方案7】:

      对于像我这样使用 admin sdk 的人,请按照此在 manifest.xml 中添加这些

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

      在您的消息负载中添加您想要的图标名称颜色!

      var payloadImage = {
          notification: {
            title: data.title,
            image: `${data.body}`,
            sound: "default",
            color: "#b75061",
          },
        };
      

      结果

      【讨论】:

        【解决方案8】:

        我也遇到过同样的问题。我找到的简单解决方案

        1. 右键单击drawable>新建>图片资源
        2. 为通知图标选择图标类型
        3. 根据您的需要调整尺寸。
          NotificationManagerCompat compat = NotificationManagerCompat.from(this);
                Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setColor(ContextCompat.getColor(getApplicationContext(), R.color.white))
                        .setVibrate(new long[]{100, 500, 100, 5000})
                        .setContentTitle(title)
                        .setContentText(message)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(message))
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setVibrate(vibrate)
                        .build();
        

        【讨论】:

          【解决方案9】:

          您可以在设置drawable之前使用drawable的DrawableCompat.setTint(int drawable);。 并做mutate()drawable,否则颜色将应用于该drawable的每个实例

          【讨论】:

          • 我建议不要这样做,否则状态栏中会出现一个彩色图标。
          【解决方案10】:

          使用 Android Studio 本身提供的 "Asset Studio" 创建通知图标(右键单击 res 文件夹和新建 > 图像资源)

          Android Studio New Image Asset Studio Menu

          然后设置通知的颜色

          int color = Color.argb(255, 228, 14, 18);
          
          NotificationCompat.Builder notificationBuilder =
                      new NotificationCompat.Builder(this, channelId)
                              .setSmallIcon(R.drawable.ic_stat_notification)
                              .setContentTitle(title)
                              .setContentText(messageBody)
                              .setAutoCancel(true)
                              .setSound(defaultSoundUri)
                              .setContentIntent(pendingIntent)
                              .setColor(color)
                              .setPriority(NotificationCompat.PRIORITY_HIGH);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-07
            • 2018-10-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-29
            相关资源
            最近更新 更多