I've used an app on Android for Spotify called ad-free that does something similar. I think it polls the notification list and somehow gets info on the currently played song, and if the notification is a link to something being advertised, then volume is set to 0. Pretty neat.
Edit: Here's the source... https://github.com/abertschi/ad-free