1

I want to set notification number on the app icon by button click. I have tried to understand and follow several methods but none of them worked.

I can send notification using simply using channel but I also want to set notification numbers on app icon.

Here below my code:

public class MainActivity extends AppCompatActivity {

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

        Button Gn = (Button) findViewById(R.id.gn);

        Gn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shownotifications();
            }
        });
    }

    private void shownotifications() {
        //channel
        String id = "main_channel";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            CharSequence name = "Channel Name";
            String description = "Channel Description";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(id, name, importance);
            notificationChannel.setDescription(description);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.WHITE);
            notificationChannel.enableVibration(false);

            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }

        //notifications
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, id)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
                .setContentTitle("Notification Title")
                .setContentText("This is the description of this notification......")
                .setLights(Color.WHITE, 500, 5000)
                .setColor(Color.RED)
                .setDefaults(Notification.DEFAULT_SOUND);

        Intent nI = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, nI, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(contentIntent);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(1000, notificationBuilder.build());
    }
}
MattButtMatt
  • 461
  • 2
  • 11
  • 26
SA Sajib
  • 67
  • 1
  • 8
  • Possible duplicate of [How to change an application icon programmatically in Android?](https://stackoverflow.com/questions/1103027/how-to-change-an-application-icon-programmatically-in-android) – bummi Jan 07 '19 at 10:37

2 Answers2

0

You can only do it by changing your app's icon everytime you want to update the notification number.

Try this.

MattButtMatt
  • 461
  • 2
  • 11
  • 26
-1

use this link https://github.com/leolin310148/ShortcutBadger set count on button click whenever you want to update count user previous count incremented

Muhammad Haroon
  • 210
  • 2
  • 11