The snub was not entirely unexpected. The president has repeatedly disparaged Mr. McCain, usually without using his name, even in the months since the senator has largely retreated from the Washington scene to his Arizona home for cancer treatment.

At a June rally, Mr. Trump criticized Mr. McCain for voting against a Republican bill to repeal the Affordable Care Act, saying that “one senator decided to put the thumb down, late in the morning. That was not a good thing when he put the thumb down.”

The president mocked him the same way during a speech to conservatives in February, prompting loud and angry boos from the audience at the reference to Mr. McCain. And just hours after signing the military spending bill on Monday, he did the same thing at a fund-raiser in upstate New York.

The relationship between the two men has never been good. As a candidate, Mr. Trump said he did not like Mr. McCain because the senator, a Navy pilot during the war in Vietnam, was shot down and captured, held for more than five years in a North Vietnamese prison and repeatedly tortured.

“He’s not a war hero,” Mr. Trump said at the time. “He’s a war hero because he was captured. I like people who weren’t captured.”