You have a loose electrical connection and a loose mechanical connection.
First you need to fix the mechanical attachment mechanism. It may be a loose nut on a threaded pipe holding the individual fixture to the base. It could be some other locking mechanism, such as bolts or screws. In some fixtures, the sockets are held in by pressure fits, which are harder to repair.
In either case, you probably need to take at least the lighting unit off of the fan, and maybe the whole fan down, depending on how it is put together. Before you do anything, turn off the circuit to the fan, not just the wall switch or the remote control switch. Once the wiring is exposed, double check that all wires are not hot by using a non-contact tester.
You need to restore the original attachment method, not just a glue or putty to wedge it in place. Fan vibration will almost certainly cause a makeshift repair to break again.
It sounds as if you have a loose wire. You need to make sure that all wires, hot neutral, and ground are all connected (in the US, these are black or red, white, and green or bare). The ground may be established by physical contacts among the metal parts, and restoring the mechanical connection may fix an intermittent ground. Sometimes a wire may be under a wire nut, but not well connected. If there is no obvious disconnected wire, remove and check wire connections under wire nuts.
If you cannot make the physical socket stable as originally designed, you need to replace the fixture (or at least that part of it). Underwriters approval (a safety standard) is dependent on the structure as designed. If you cannot restore the mechanical structure, you probably need to replace the entire fan.