# Изменение иконки приложения Android

Чтобы изменить иконку приложения в зависимости от количества уведомлений в центре уведомлений, выполните следующие шаги.

1.  Добавьте новую иконку в ваш проект в папку 'mipmap'.

<img src="/android-notification-customisation-change-android-app-icon-1.webp" alt="Скриншот, показывающий структуру папки mipmap в Android Studio с добавленным новым файлом иконки"/>

2. Создайте новый класс Activity.

```java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivityAlias extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}
```

3. **Создайте класс с логикой изменения иконки.**

```java
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;

public class MyPushReceiver  {

    public static void changeAppIcon(Context context, int count) {
        PackageManager pm = context.getPackageManager();
        if (count > 0) {
            pm.setComponentEnabledSetting(
                    new ComponentName(context, "com.example.app.MainActivityAlias"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP
            );
            pm.setComponentEnabledSetting(
                    new ComponentName(context, "com.example.app.MainActivity"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP
            );
        } else {
            pm.setComponentEnabledSetting(
                    new ComponentName(context, "com.example.app.MainActivity"),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP
            );
            pm.setComponentEnabledSetting(
                    new ComponentName(context, "com.example.app.MainActivityAlias"),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP
            );
        }
    }
}
```

4. **В вашем классе NotificationServiceExtension реализуйте отслеживание получения/удаления push-уведомлений.**

```java
@Override
public boolean onMessageReceived(final PushMessage message) {
	super.onMessageReceived(message);

	if (!message.isSilent()) {
		/**
		 * In this example, a random number of notifications is simply indicated.
		 * You need to implement the logic for counting notifications yourself.
		 */
		int notificationCount = 1;

		if (getApplicationContext() != null) {
			MyPushReceiver.changeAppIcon(getApplicationContext(), notificationCount);
		}
	}
}

/**
* Pushwoosh callback for tracking push notification removal from Notification Center.
*/
@Override
protected void onMessageCanceled(PushMessage message) {
	super.onMessageCanceled(message);

	if (!message.isSilent()) {
		/**
		* In this example, a random number of notifications is simply indicated.
		* You need to implement the logic for counting notifications yourself.
		*/
		int notificationCount = 0;

		if (getApplicationContext() != null) {
			MyPushReceiver.changeAppIcon(getApplicationContext(), notificationCount);
		}
	}
}
```

5. **Готово!**

### Пример того, как это выглядит на устройстве

<video src="/android-notification-customisation-change-android-app-icon-2.webm" title="Анимированная демонстрация, показывающая динамическое изменение иконки приложения при получении или удалении уведомлений" autoplay loop muted playsinline />