Set Badge Count in Android Push Notification with Firebase?

7k views Asked by At

I already built push notification with FCM, now I need to make badge counter on app icon. So when I got unread notification, it will be count. If I read/touch the notification on notification bar, it will be decrease the number.

This image below show What I need to use.

Notification Badge Count

1

There are 1 answers

1
Payal Sorathiya On

First Create notyfy.xml

     <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/counterPanel"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:background="@drawable/bell">

    <RelativeLayout
        android:id="@+id/counterValuePanel"
        android:layout_width="wrap_content"
        android:layout_gravity="end"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/counterBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_badge" />

        <TextView
            android:id="@+id/count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_centerInParent="true"
            android:textColor="@color/colorWhite" />
    </RelativeLayout>
</FrameLayout>

then in your drawable folder put image with red background in your question. now create menu file in menu folder with name home_main

      <menu xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto">
      <item android:id="@+id/action_settings" 
      android:title="title" android:icon="@drawable/bell" 
      app:showAsAction="always" /> 
      </menu>

in your Activity where you use badge count put below code

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends ActionBarActivity {

   public int count = 0;
   @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home_main, menu);
        final MenuItem setting = menu.findItem(R.id.action_settings);
        setting.setIcon(buildCounterDrawable(count, R.drawable.bell));
        return true;
    }
  public Drawable buildCounterDrawable(int NotiCount,int backgroundImageId) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.noty_count, null);
        view.setBackgroundResource(backgroundImageId);
        NotiCount = ((VApp) getApplicationContext()).getNoty();
        VLogger.infoLog("NotiCount" + NotiCount);
        if (NotiCount == 0) {
    View counterTextPanel =     view.findViewById(R.id.counterValuePanel);
            counterTextPanel.setVisibility(View.GONE);
        } else {
            TextView textView = (TextView) view.findViewById(R.id.count);
            textView.setText(NotiCount + "");
        }
     view.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

 view.setDrawingCacheEnabled(true);
 view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
 Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
 view.setDrawingCacheEnabled(false);

return new BitmapDrawable(getResources(), bitmap);
    }




    @Override
    protected void onResume() {
        super.onResume();
        invalidateOptionsMenu();
    }
}