简体   繁体   English

Android 颜色通知图标

[英]Android Color Notification Icon

I'm working on an app where I create a notification for the user.我正在开发一个为用户创建通知的应用程序。 I want the icon to appear as white when it's in the status bar, but colored blue when it's being displayed in the drop down notification menu.我希望图标在状态栏中显示为白色,但在下拉通知菜单中显示时显示为蓝色。 Here's an example of the same thing being done by the Google Store app.以下是 Google 商店应用执行相同操作的示例。

White notification in status bar:状态栏中的白色通知:

在此处输入图片说明

Colored notification in drop down menu:下拉菜单中的彩色通知:

在此处输入图片说明

How can I replicate this?我怎样才能复制这个? What properties do I have to set?我必须设置哪些属性?

Edit: Here's my current code - I made the image all white with a transparent background, so it looks fine in the status bar, but in the notification drop, the image is still the same white color:编辑:这是我当前的代码 - 我将图像全部设为白色并带有透明背景,因此在状态栏中看起来不错,但在通知下拉菜单中,图像仍然是相同的白色:

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

I found the answer to my question here: https://stackoverflow.com/a/44950197/4394594我在这里找到了我的问题的答案: https : //stackoverflow.com/a/44950197/4394594

I don't know entirely what the problem was, but by putting the huge png that I was using for the icon into the this tool https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=image&source.space.trim=1&source.space.pad=0&name=ic_skylight_notification and by placing the generated icons it gave into my mipmap folder, I was able to get the setColor(...) property to work correctly.我不完全知道问题是什么,但是通过将我用于图标的巨大 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(...)属性正常工作。

For firebase nofitications sent from console you just need to add this in your manifest:对于从控制台发送的 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" />

Where white_logo is your app white logo, and custom_color is the color you want to have the icon and text colored.其中 white_logo 是您的应用程序白色徽标, custom_color 是您希望图标和文本着色的颜色。

More details here:https://firebase.google.com/docs/cloud-messaging/android/client更多细节在这里:https ://firebase.google.com/docs/cloud-messaging/android/client

Here is what I did for my app ...这是我为我的应用程序所做的......

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

Sample With Color:带颜色的样品:

在此处输入图片说明

Sample without Color:无色样品: 在此处输入图片说明

When building the notification, you can set the color and the icon.在构建通知时,您可以设置颜色和图标。 If your icon is a pure white image , it'll apply the color for you in the correct spots.如果您的图标是纯白色图像,它会在正确的位置为您应用颜色。

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

If you want to change color and title name as per gmail and twitter in Push notification or inbuilt notification then you need to add these lines in notification.如果您想在推送通知或内置通知中根据 gmail 和 twitter 更改颜色和标题名称,则需要在通知中添加这些行。

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

First line used for icon and in second line you need to define color第一行用于图标,第二行你需要定义颜色

I might be late to the party but all above answers are not relevant or deprecated.我可能会迟到,但以上所有答案都不相关或已弃用。

在此处输入图片说明

You can achieve this easily by using setColor method of NotificationCompat.Builder您可以使用NotificationCompat.Builder setColor方法轻松实现这一点

Example:示例:

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!")

Now it will show notification as pink color现在它会将通知显示为粉红色

Note: If you are using firebase then the color won't be seen directly.注意:如果您使用的是 firebase,则不会直接看到颜色。 You have to add this in manifest file.您必须在清单文件中添加它。

<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" />

I have faced the same issue.我遇到了同样的问题。 simple solution that i have found我找到的简单解决方案

  1. Right click on drawable>new>image assets右键单击可绘制>新建>图像资产
  2. select icon type to Notifications icon选择图标类型到通知图标
  3. Play around with the dimensions according to your needs.根据您的需要调整尺寸。 在此处输入图片说明
  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();

You can use the DrawableCompat.setTint(int drawable);您可以使用DrawableCompat.setTint(int drawable); of the drawable before setting the drawable.在设置 drawable 之前的 drawable。 And do mutate() the drawable otherwise the color tint will be applied to every instance of that drawable并且做mutate() drawable 否则颜色将应用于该 drawable 的每个实例

Create your Notification Icon using "Asset Studio" available in the Android Studio itself (Right Click res folder and New > Image Asset)使用 Android Studio 本身中提供的“Asset Studio”创建您的通知图标(右键单击 res 文件夹和 New > Image Asset)

Android Studio New Image Asset Studio Menu Android Studio 新建 Image Asset Studio 菜单

Then set the color for notification然后设置通知的颜色

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

For those who is using admin sdk like me follow this add these in manifest.xml对于像我一样使用 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" />

in your message payload add the icon name color You want!在您的消息有效负载中添加您想要的图标名称颜色!

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

Result结果在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM