繁体   English   中英

Android 颜色通知图标

[英]Android Color Notification Icon

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

状态栏中的白色通知:

在此处输入图片说明

下拉菜单中的彩色通知:

在此处输入图片说明

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

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

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

我在这里找到了我的问题的答案: 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(...)属性正常工作。

对于从控制台发送的 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

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

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

带颜色的样品:

在此处输入图片说明

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

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

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

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

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

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

我可能会迟到,但以上所有答案都不相关或已弃用。

在此处输入图片说明

您可以使用NotificationCompat.Builder setColor方法轻松实现这一点

示例:

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

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

  1. 右键单击可绘制>新建>图像资产
  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();

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

使用 Android Studio 本身中提供的“Asset Studio”创建您的通知图标(右键单击 res 文件夹和 New > Image Asset)

Android Studio 新建 Image Asset Studio 菜单

然后设置通知的颜色

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

对于像我一样使用 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",
    },
  };

结果在此处输入图片说明

暂无
暂无

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

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