Onwrite method for Firebase

I’m trying to use the onWrite() method in my Ionic app on the frontend, but it doesn’t work. The context is: I need to listen on changes in a value in Firebase Database to trigger changes in the Badge number (like Messenger unread messages). any idea how to do this. This is the code I’ve got so far, which doesn’t work:

    this.badge.hasPermission().then((success) => {
      if(success) {
        this.firebaseDatabase.firebaseGet('profiles', UID).then((snapshot) => {
          firebase.database().ref('/profiles/'+UID).on('notifications', (snapshot2) => {
               this.badge.set(snapshot.notifications + 1);
          });
        });
      } else {
        console.log("Don't have permistion to set App Badge");
      }
    });