【问题标题】:Set Badge Count in Android Push Notification with Firebase?使用 Firebase 在 Android 推送通知中设置徽章计数?
【发布时间】:2017-05-14 14:34:21
【问题描述】:

我已经用 FCM 构建了推送通知,现在我需要在应用图标上制作徽章计数器。 所以当我收到未读通知时,它会被计算在内。如果我阅读/触摸通知栏上的通知,它将减少数字。

下面这张图片显示了我需要使用的内容。

【问题讨论】:

  • 你想在应用图标上显示吗?
  • @PatelPinkal 是的,我想在应用程序图标上显示它。有什么想法吗?还是有可能?
  • 它只能在小部件中而不是在应用程序图标上,并且三星手机为通知提供默认计数器徽章
  • @PatelPinkal 嗯,好吧,但是 Android 上的 Facebook、WhatsApp 徽章计数器呢?
  • 您在哪个手机上看到 facebook 和 whatsapp 的徽章计数器

标签: android push-notification firebase-cloud-messaging badge


【解决方案1】:

首先创建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>

然后在您的可绘制文件夹中将带有红色背景的图像放入您的问题中。 现在在菜单文件夹中创建名为 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>

在您使用徽章计数的活动中,将代码放在下面

@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();
    }
}

【讨论】:

  • 他希望应用程序图标上的徽章计数器不在应用程序内
猜你喜欢
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
相关资源
最近更新 更多