如何更改通知的应用程序名称?

How can I change the application name of my notification?

我的用户可以选择更改应用程序图标和应用程序名称,为此我使用了 activity-alias,效果很好。

例如:

然而,当我向用户发送通知时,如果他们选择了替代 icon/application 名称,则通知标题包含默认应用程序。我该如何改变?

这是我的 notificationBuilder:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default")
                .setSmallIcon(isAlternativeLaunched ? R.drawable.ic_alternative : R.drawable.ic_default)
                .setContentTitle("This is my title")
                .setContentText("This is my content")
                .setSubText("This is my text")
                .setColor(ContextCompat.getColor(context, R.color.colorPrimary));

PS: 我已经检查过,当使用 alternativeName 时我的应用程序的标题设置正确,只有通知有旧标题。


编辑:查看文档 https://developer.android.com/guide/topics/ui/notifiers/notifications#Templates 据说:

App name: This is provided by the system

我不明白的是,为什么不更改应用名称? 我的 activity-alias 看起来像这样:

<activity-alias
    android:name="my.package.name.DefaultActivity"
    android:enabled="true"
    android:icon="@mipmap/ic_launcher_default"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round_default"
    android:targetActivity="my.package.name.MainActivity">

    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity-alias>

<activity-alias
    android:name="my.package.name.AlternativeActivity"
    android:enabled="false"
    android:label="@string/app_name_alternative"
    android:icon="@mipmap/ic_launcher_alternative"
    android:roundIcon="@mipmap/ic_launcher_alternative_round"
    android:targetActivity="my.package.name.MainActivity">

    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity-alias>

我找到了使用 Custom Notification

的解决方法
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp">

    <TextView android:id="@+id/text_appname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"
        android:textSize="8sp"
        android:text="Appname"
        android:paddingLeft="5dp" />

    <TextView android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
        android:layout_below="@id/text_appname"/>

    <TextView android:id="@+id/text_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:layout_below="@id/text_title" />
</RelativeLayout>
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notif_custom_view);
remoteViews.setTextViewText(R.id.text_appname, context.getString(isAlternativeLaunched ? R.string.app_name_alternative : R.string.app_name));
remoteViews.setTextViewText(R.id.text_title, "This is my title");
remoteViews.setTextViewText(R.id.text_message, "This is my text");

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default")
        .setCustomContentView(remoteViews)
        .setColor(ContextCompat.getColor(context, R.color.colorPrimary));

NotificationManagerCompat.from(context).notify(0, notificationBuilder.build());