2016-12-30 109 views
1

我已經使用FCM構建推送通知,現在我需要在應用程序圖標上製作徽章計數器。 所以當我有未讀的通知時,它會被計數。如果我閱讀/觸摸通知欄上的通知,它會減少數量。使用Firebase在Android推送通知中設置徽章計數?

下面的圖片顯示了我需要使用的東西。

Notification Badge Count

+0

? –

+0

@PatelPinkal是的,我想在應用程序圖標上顯示它。有任何想法可以做到嗎?或者有可能? – Yuw

+0

它的唯一可能的部件不在應用程序圖標,三星電話提供默認的通知櫃檯徽章 –

回答

0

首先創建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(); 
    } 
} 
+0

@Yuw。試試這個。 –

+1

他希望應用程序圖標上的徽章計數器不在應用程序內 –