I am not sure but it could be something like relux.Dogs like people as they get older can have these problems. Unfortunately heartworms also causes hacking in the later stages. Dogs cough for a variety of reasons, some of which can be serious in themselves and others that are signs of an underlying problem. Internal parasites, heartworm disease, distemper, fungus diseases, tuberculosis, allergies, and pollutants such as cigarette smoke can cause Fido to cough.
Coughs are triggered by irritants in the air passages and can be characterized as dry and hacking; moist and bubbly; gagging; wheezy; harsh; or weak. It can be frequent, chronic, or intermittent, and is often self-perpetuating as it dries the throat and leads to further irritation
When was his last heartwoem test? Have you changed his food lately? New treats? I would get him to his regular vet just to be on the safe side because even something as simple as reflux can be very uncomfortable for him and can cause some health issues depending on how long he's had it.... I wish u guys the best