[英]Set Badge Count in Android Push Notification with Firebase?
I already built push notification with FCM, now I need to make badge counter on app icon. 我已经使用FCM构建了推送通知,现在我需要在应用程序图标上设置徽章计数器。 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. 下图显示了我需要使用的内容。
First Create notyfy.xml 首先创建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
现在在菜单文件夹中创建名为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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.