If the can has been shaken, then the pressure differential inside the can during shaking will force some of the carbon dioxide out of solution to form bubbles. Some bubbles float to the top and some stick to the sides and bottom of the can. Given sufficient time, equilibrium will be achieved and the carbon dioxide will dissolve back into the beverage. If the can is opened immediately after shaking, it is likely that the beverage will spray all over the place, since some liquid from all parts of the can will be pushed ahead of escaping gas and out the can (hey, bubbles float to the top and these bubbles are under pressure too!). When the can is tapped before opening, bubbles adhering to the sides and bottom of the can may be jarred free. Assuming the can is upright, the bubbles, being lighter than the liquid, will float to the top of the can. Then when the can is opened, the bubbles are already near the opening, so they don't push through the beverage on their way out.
Here's a little experiment to try at home: Shake up two cans of cola (or whatever is handy). Place one can right side up and the other inverted. Tap the 'top' of each can. Now turn the upside-down can over and open both cans. Did you get sprayed more by the can that was inverted when it was tapped?
In addition to tapping on the can, the risk of getting soaked is reduced if the can or bottle is opened slowly rather than quickly because then the initial pressure change is less significant, so the gas can escape less forcefully. The wider the top of the container, the better the chance to avoid an accident, since there is more volume for gas without intervening liquid. It also follows that if you shake up a can in zero-gravity, then tapping on the can won't work (although opening the can slowly will help), since the bubbles aren't going to preferentially float toward the top of the can! Maybe tapping on the can makes the problem more likely, since tapping dislodges the bubbles clinging to the can, so there is nothing slowing them down when the can is cracked. Is that why cosmonauts drink vodka instead of Coca Cola? Hmmmm...


