Как я могу изменить название приложения моего уведомления? - PullRequest
0 голосов
/ 05 мая 2020

У моего пользователя есть возможность изменить значок приложения и имя приложения, для этого я использовал activity-alias, который отлично работает.

Например:

  • Значок по умолчанию с именем приложения как «Имя приложения»
  • Альтернативный значок с именем приложения как «Альтернативное имя приложения»

Однако, когда я отправляю уведомление пользователю, если они выбрали альтернативный значок / название приложения, заголовок уведомления содержит приложение по умолчанию. Как я могу изменить?

Вот мой 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, только в уведомлении старый заголовок.


РЕДАКТИРОВАТЬ: Глядя на do c https://developer.android.com/guide/topics/ui/notifiers/notifications#Templates, говорится:

Имя приложения: Это предоставляется системой

Я не понимаю, почему имя приложения не меняется? Мой 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>

1 Ответ

0 голосов
/ 05 мая 2020

Я нашел обходной путь, используя 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());
...