To make a very modern reference, The Fonz was really onto something when it came to electronics. Believe it or not, there's actually science behind why sometimes the best way to fix your electronics is by SMACKING them.
"Percussive maintenance" is the sophisticated term for shaking, banging on, or pounding something to make it work. And it works especially well for products that have batteries or older electronics that have parts that might've become slightly out of line.
But, if you're going to try to fix things this way, tap them, don't hit them. Because if you hit something too hard, or you hit a device that's too fragile like an iPhone, you risk breaking it.
And percussive maintenance is a temporary fix at best. When a device is at the "I need to hit this to make it work" stage, it probably needs an actual professional fix. Or new batteries.