How does the gym's badge level system works?

We do know we'll be able to level up Gym Badges by performing the following activities:
(thanks to the Silph Road sub-Reddit user for clarifying some of these numbers):

  1. Battling at Gyms (CP of Pokemon defeated divided by 10)
  2. Adding Pokémon to the Gym (+ 100 Badge XP)
  3. Holding a creature at the Gym (+1 Badge XP per minute)
  4. Giving Berries to Pokémon held at the Gym (+ 10 Badge XP, as well as 20 Stardust and a possibility of Candy)
  5. Defeat a Raid Boss (1000 XP for the Badge)
  6. Win a battle from a Pokemon defending the gym (0,01 XP * pokemon defending CP)

By performing the above activities, whether you are passive and prefer to increase Motivation as you pass a Gym, or are actively battling and holding creatures there, you can level up your Badge over time.

Badge XP needed for a gym medal is:

  • Bronze: 500 XP
  • Silver: 4000 XP
  • Gold: 30000 XP

(So Silver is 3500 XP more needed after getting the Bronze gym badge)

Edit on 9 July:
Found this webpage that also gives a lot of information about XP on Gym systems: Gym Badge Experience