我的用户可以选择更改应用图标和应用名称,为此我使用了一个 "应用图标"。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时是正确设置的,只有通知有旧的标题。
EDIT:看了一下文档 https:/developer.android.comguidetopicsuinotifiersnotifications#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>
我找到了一个变通的方法,使用 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());