Android 我希望我的通知徽章只读取/计数新输入,而不是全部。为科特林

Android 我希望我的通知徽章只读取/计数新输入,而不是全部。为科特林,android,firebase,kotlin,firebase-realtime-database,kotlin-coroutines,Android,Firebase,Kotlin,Firebase Realtime Database,Kotlin Coroutines,步骤:如果通知片段中有4个RecyclerView布局,则通知标记将读取计数4。当我单击通知导航btn时,计数清除。如果我输入一个新的recyclerView,计数器读数为5,而不是重置为1 我希望通知徽章只计算传入的新通知 此外,一旦应用程序关闭并重新打开通知徽章,将旧通知和新通知一起重新计数 类MainActivity():AppCompatActivity(){ val navView:BottomNavigationView=findViewById(R.id.bottom\u na

步骤:如果通知片段中有4个RecyclerView布局,则通知标记将读取计数4。当我单击通知导航btn时,计数清除。如果我输入一个新的recyclerView,计数器读数为5,而不是重置为1

我希望通知徽章只计算传入的新通知

此外,一旦应用程序关闭并重新打开通知徽章,将旧通知和新通知一起重新计数

类MainActivity():AppCompatActivity(){

val navView:BottomNavigationView=findViewById(R.id.bottom\u nav\u视图)

navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)

}

}

package supgro.com.Controller.Service
private lateinit var firebaseUser: FirebaseUser
var post: MutableList<Post>? = null
var noti: MutableList<Notification>? = null




/* This selectedFragment was used to test when the buttons were clicked they would go to that fragment/activity
 selectedFragment = HomeFragment()
 internal var selectedFragment: Fragment? = null*/

private val count: Int = 1

private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.nav_home -> {
                //passing in moveToFragment function

                moveToFragment(HomeFragment())
                return@OnNavigationItemSelectedListener true

            }
            R.id.nav_search -> {
                moveToFragment(SearchFragment())
                return@OnNavigationItemSelectedListener true



            }
            R.id.nav_add -> {
                moveToFragment(PostSelectionFragment())
                //item.isChecked = false
            //startActivity(Intent(this@MainActivity, AddPostActivity::class.java))
                return@OnNavigationItemSelectedListener true

            }
            R.id.nav_notifications -> {
                if (moveToFragment( NotificationsFragment()) != null){
                    badgeClear(R.id.nav_notifications).apply {
                        isDestroyed

                      //  new_notifications_icon.visibility = View.VISIBLE
                    }

                }
                return@OnNavigationItemSelectedListener true


            }
            R.id.nav_profile -> {

                 moveToFragment(ProfileFragment())
                return@OnNavigationItemSelectedListener true

              /*  if (item.isEnabled){
                    item.isVisible = false
                }*/


            }
        }
   /* if (selectedFragment != null){
        supportFragmentManager.beginTransaction().replace(R.id.fragment_container, selectedFragment!!).commit()
    }*/
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    firebaseUser = FirebaseAuth.getInstance().currentUser!!


    //bottom_nav_view is the name of the bottom navigation bar
    //this selects the first fragment activity to be loaded

    navView.getOrCreateBadge(R.id.nav_notifications).apply{
        val notiRef = FirebaseDatabase.getInstance().reference.child("Notifications")





        notiRef.addValueEventListener(object: ValueEventListener {
            override fun onDataChange(p0: DataSnapshot) {
                if (p0.exists()) {
                  number = p0.childrenCount.toInt()
                    isVisible = true
                   // noti!!.clear()


                }/*else if (p0.childrenCount <= 5){

                    number = p0.childrenCount.toInt()
                }*/




                }



            override fun onCancelled(p0: DatabaseError) {
                TODO("Not yet implemented")
            }

        })


    }

   // navView.removeBadge(R.id.nav_notifications)

    moveToFragment(HomeFragment())



}
val fragmentTrans = supportFragmentManager.beginTransaction()
fragmentTrans.replace(R.id.fragment_container, fragment)
fragmentTrans.commit()
private fun badgeClear(id: Int){
    val navView: BottomNavigationView = findViewById(R.id.bottom_nav_view)

    val badgeDrawable = navView.getOrCreateBadge(id)
    if (badgeDrawable != null){
        badgeDrawable.isVisible = false
        badgeDrawable.clearNumber()
    }
}