Nidhi Dave
Nidhi Dave

Reputation: 141

I am not able to display notification count on application icon in home screen

Hello I want to display notification count on application icon in home screen but I am not able to do this.

This is my code.

public class MainActivity extends Activity {
   ImageView img;
    BadgeView badge;
    public int badgeCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
           badgeCount = 0;
            img = (ImageView)findViewById(R.id.notificationicon);
            Drawable icon;
            try {
                icon = getPackageManager().getApplicationIcon("com.example.notificationapp");
                  img.setImageDrawable(icon);
                    badge = new BadgeView(this, img);
                    badge.setText("10");

            } catch (NameNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            badge = new BadgeView(this, img);
            badge.setText("10");

            badgeCount = Integer.parseInt("5");
            ShortcutBadger.with(getApplicationContext()).count(badgeCount);
            img.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (badge.isShown()) {
                         try {
                                badgeCount = Integer.parseInt("5");
                            } catch (NumberFormatException e) {
                                Toast.makeText(getApplicationContext(), "Error input", Toast.LENGTH_SHORT).show();
                            }

//                              ShortcutBadger.setBadge(getApplicationContext(), badgeCount);
                            ShortcutBadger.with(getApplicationContext()).count(badgeCount);

                            Toast.makeText(getApplicationContext(), "Set count=" + badgeCount, Toast.LENGTH_SHORT).show();
                        badge.decrement(1);
                    } else {
//                      badge.decrement(1);
                        badge.show();
                    }
                }
            });
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
            String currentHomePackage = resolveInfo.activityInfo.packageName;
            ShortcutBadger.with(getApplicationContext()).count(badgeCount);
            TextView textViewHomePackage = (TextView) findViewById(R.id.textViewHomePackage);
            textViewHomePackage.setText("launcher:" + currentHomePackage);
    }

used this two line code to set notification count on app icon .

ShortcutBadger.setBadge(getApplicationContext(), badgeCount);
                              ShortcutBadger.with(getApplicationContext()).count(badgeCount);

But with this I am not able to do this.

Upvotes: 0

Views: 1864

Answers (1)

BrentM
BrentM

Reputation: 5757

ShortcutBadger only supports some third party launchers.

It does not support the stock Android launcher which is on many devices and the emulator.

Upvotes: 3

Related Questions