0

My user has the possibility to choose to change the application icon and application name, for that I've used an activity-alias, which is working great.

For example:

  • Default Icon with application name as "Application name"
  • Alternative Icon with application name as "Alternative Application name"

However when I send a notification to the user, if they chose the alternative icon/application name, the notification title contains the default application. How can I change ?

Here is my 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: I've checked and the title of my application is correctly set when using alternativeName, only the notification has the old title.


EDIT: Looking at the doc https://developer.android.com/guide/topics/ui/notifiers/notifications#Templates it is said:

App name: This is provided by the system

What I don't understand is, why is it not changing the app name ? My activity-alias look like this:

<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>
Biscuit
  • 4,840
  • 4
  • 26
  • 54
  • I think this will help you https://stackoverflow.com/questions/52076497/how-to-change-app-name-in-notification-bar-programmatically – zinonX May 05 '20 at 13:31
  • I'd rather avoid using a library just to change the app name, also I'm not sure to see any answer that I can apply there – Biscuit May 05 '20 at 13:36
  • Then you have to change your styles.xml programetically take a look here https://stackoverflow.com/questions/6936800/android-change-strings-resource-programmatically/6936861 – zinonX May 05 '20 at 13:43
  • how do I target the application name in the notification ? I haven't found any method to do so. – Biscuit May 05 '20 at 13:44

1 Answers1

0

I've found a workaround using 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());
Biscuit
  • 4,840
  • 4
  • 26
  • 54