Your dog could have several things wrong with it- he could have kennel cough, which involves dry gagging sounds. Or, he could have a gastrointestinal blockage or cancer. It is important to take your dog to the vet.
Could be heartworm, cancer, pneumonia, plus several other (less common) things... some very serious conditions. Please take your dog to the vet right away!
It could be something as simple as an upper respiratory infection or allergies or it could be as serious as parvo. I recommend getting your dog to the vet as soon as possible. Diseases such as parvo can be deadly to the dog if not treated right away and he can give it to other animals.
This sounds like a serious problem and you should take your dog to a vet. It can be normal for dogs to heave a little, but it is not normal for them to spit up blood.