You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android通知:如何在应用名称右侧、时间戳前添加文本?

嘿,要在Android通知的应用名称右侧、时间戳之前加自定义文本,原生的通知模板可没给这个现成的位置,得靠自定义通知布局来搞定!我给你一步步拆解实现方法:

实现步骤

1. 编写自定义通知布局XML

首先在res/layout目录下创建一个布局文件,比如notification_custom_layout.xml,模仿系统通知的结构,把应用名称、自定义文本、时间戳放在同一行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <!-- 标题行:图标 + 应用名称+自定义文本 + 时间戳 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <!-- 应用图标 -->
        <ImageView
            android:id="@+id/notification_icon"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@mipmap/ic_launcher"
            android:layout_marginEnd="12dp"/>

        <!-- 应用名称与自定义文本容器 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/app_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textColor="@android:color/black"
                android:textStyle="bold"/>

            <!-- 核心:要添加的自定义文本 -->
            <TextView
                android:id="@+id/custom_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textColor="@android:color/darker_gray"
                android:layout_marginStart="4dp"/>
        </LinearLayout>

        <!-- 时间戳 -->
        <TextView
            android:id="@+id/notification_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textColor="@android:color/darker_gray"/>
    </LinearLayout>

    <!-- 通知正文内容 -->
    <TextView
        android:id="@+id/notification_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textColor="@android:color/black"
        android:layout_marginTop="8dp"/>
</LinearLayout>

2. 在代码中加载布局并构建通知

接下来在Activity或Service里,用RemoteViews加载这个自定义布局,动态填充应用名称、自定义文本和时间戳,再构建通知:

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat

fun showCustomNotification(context: Context) {
    val channelId = "custom_notification_channel"
    val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    // 适配Android O及以上,创建通知渠道
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(
            channelId,
            "自定义通知渠道",
            NotificationManager.IMPORTANCE_DEFAULT
        ).apply {
            description = "展示带自定义文本的通知"
        }
        notificationManager.createNotificationChannel(channel)
    }

    // 加载自定义布局
    val remoteViews = RemoteViews(context.packageName, R.layout.notification_custom_layout)

    // 动态设置应用名称(从系统获取,避免写死)
    val appName = context.applicationInfo.loadLabel(context.packageManager).toString()
    remoteViews.setTextViewText(R.id.app_name, appName)

    // 设置你要添加的自定义文本,比如"[VIP]"或者其他标识
    remoteViews.setTextViewText(R.id.custom_text, " [VIP]")

    // 设置时间戳(用系统时间格式化,和系统通知样式统一)
    val currentTime = System.currentTimeMillis()
    val timeFormatter = android.text.format.DateFormat.getTimeFormat(context)
    remoteViews.setTextViewText(R.id.notification_time, timeFormatter.format(currentTime))

    // 设置通知正文
    remoteViews.setTextViewText(R.id.notification_content, "您有一条新的专属消息")

    // 构建通知(必须设置小图标,否则通知不显示)
    val notification = NotificationCompat.Builder(context, channelId)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setCustomContentView(remoteViews)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .build()

    // 显示通知
    notificationManager.notify(1, notification)
}

3. 关键注意事项

  • 必须设置小图标:setSmallIcon是Android通知的强制要求,不设置的话通知根本不会显示。
  • 控件ID要对应:代码里设置文本的控件ID,必须和XML布局里的ID完全一致,否则会失效。
  • 系统版本适配:Android 12及以上的通知有新的样式规范,如果需要兼容,可能要调整布局的 padding 或者控件尺寸。
  • 时间戳灵活性:如果想让时间戳自动随系统更新,也可以考虑隐藏自定义的时间控件,改用系统原生的时间,但自定义布局里直接格式化时间的方式更灵活可控。

内容的提问来源于stack exchange,提问作者Keivan Noroozi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:12:40