Change Android App icon

Changing the Application icon based on notification count

To change the application icon based on the number of notifications in the notification center, follow the steps below.

1. Add a new icon to your project in the 'mipmap' folder.

  1. Create a new Activity class.

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);

    }
}
  1. Create a class with the logic for changing the icon.

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
            );
        }
    }
}
  1. In your NotificationServiceExtension class, implement tracking for receiving/deleting push notifications.

@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);
		}
	}
}
  1. Done!

Example of how it looks on the device

Last updated