New York City Mayor Bill de Blasio called the fatal shooting of two Brooklyn police officers an attack on "the foundation of society." (YouTube: NYC Mayor's Office)

Tensions between New York Mayor Bill de Blasio and the city’s police — which boiled over in the wake of the assassination-style slayings of two officers Saturday — have been simmering since the mayor’s 2013 campaign and represent a sharp turn from the close alliance between the city’s mayors and law enforcement over the past two decades.

Recriminations against de Blasio began within hours of the news that the officers had been shot at point-blank range as they sat in their patrol car near a housing project in the Bedford-Stuyvesant area of Brooklyn — and that the gunman had been motivated to kill them as retribution for the black men whose deaths at the hands of police in New York City and Ferguson, Mo., have sparked protests around the country.

A video of the arrival of de Blasio and Police Commissioner William J. Bratton at the hospital where officers Wenjian Liu and Rafael Ramos had been taken showed dozens of police officers silently turning their backs.

“There’s blood on many hands tonight,” Patrick Lynch, president of the largest police union, said late Saturday. “Those that incited violence on the street in the guise of protest, that tried to tear down what New York City police officers did every day. We tried to warn it must not go on, it shouldn’t be tolerated. That blood on the hands starts at the steps of City Hall in the office of the mayor.”

Although New York is a city where Democrats outnumber Republicans by more than 6 to 1, de Blasio is its first Democratic mayor in 20 years, and his stewardship of the city is being watched nationally as a test of unabashedly liberal leadership. After his landslide victory, he declared: “Make no mistake. The people of this city have chosen a progressive path. And tonight we set forth on it together, as one city.”

As de Blasio nears the end of his first year governing a huge and diverse city, there is a striking racial divide in how his constituents feel about his performance. A Quinnipiac University poll released Dec. 18 found that about 47 percent of those surveyed approved of how he has handled his job. But only 34 percent of whites had a favorable view, compared with 70 percent of African Americans and 47 percent of Hispanics.

On the question of how the mayor is handling police relations with the community, 56 percent of those surveyed disapproved and 36 percent approved.

Relations between the mayor and uniformed police officers have become so strained that “he probably needs an intermediary to go between himself and the unions, maybe a religious leader,” former New York City police commissioner Raymond Kelly said in an interview. “I don’t know how receptive the unions would be.”

Kelly added that in his experience, the Patrolmen’s Benevolent Association — the largest of several dozen law enforcement unions, representing uniformed officers — has been a good barometer of the sentiments of its 23,000 rank-and-file members.

There have been a number of flash points between de Blasio and police, including one earlier this month when the mayor spoke to George Stephanopoulos of ABC News about his fears for his biracial son.

“It’s different for a white child. That’s just the reality in this country,” de Blasio said. “And with Dante, very early on with my son, we said, ‘Look, if a police officer stops you, do everything he tells you to do, don’t move suddenly, don’t reach for your cellphone,’ because we knew, sadly, there’s a greater chance it might be misinterpreted if it was a young man of color.”

That echoed previous statements the mayor had made, going back to a campaign ad in which he invoked his Afro-wearing teenage son to explain his opposition to the New York Police Department’s controversial “stop and frisk” tactic, which entailed stopping hundreds of thousands of people a year for what was deemed suspicious activity. The vast majority of those targeted were nonwhite and innocent of any wrongdoing.

Lynch, head of the Patrolmen’s Benevolent Association of the City of New York, complained that such comments made police officers feel as though they had been “thrown under the bus.”

Members of the city’s police force, along with its firefighters, achieved iconic status for their performance as first responders in the terrorist attacks of Sept. 11, 2001. People wore hats and shirts emblazoned with “NYPD” not just in New York, but around the country.

In recent years, the attention on the department has focused more on the question of whether enforcement is applied fairly. De Blasio’s opposition to stop-and-frisk was a major emphasis when the former New York City public advocate ran for mayor, and a New York Times analysis in September found that the tactic “all but vanished” after he took office.

The department found itself in the middle of another controversy in July, when a group of officers descended upon Eric Garner, a Staten Island African American suspected of illegally selling loose cigarettes. Garner — an asthmatic father of six and grandfather of two — died after one of them, Officer Daniel Pantaleo, used a chokehold on him.

All of it was captured on amateur video, as were Garner’s pleas of, “I can’t breathe.” After a grand jury declined earlier this month to bring the officer to trial, protests erupted. Early on, they were seen as a peaceful, more productive contrast to those that ensued around the shooting of teenager Michael Brown by a police officer in Ferguson.

Recently, however, the demonstrations have taken a different turn — and that has increased tensions between the mayor and the department. After two police lieutenants were attacked by protesters on the Brooklyn Bridge, de Blasio described them as having been “allegedly assaulted” — terminology that rankled police.

The police union has posted a form on its Web site where officers can request that de Blasio and City Council Speaker Melissa Mark-Viverito “refrain from attending my funeral services in the event that I am killed in the line of duty.”

Minor incidents also have fueled the sense of grievance against the mayor among police. When he was late for a November memorial service for plane crash victims, for example, he blamed weather and a police ferry. De Blasio later acknowledged he “had a very rough night, woke up sluggish and I should have gotten myself moving quicker.”

One longtime associate of de Blasio, who spoke on the condition of anonymity because of the sensitivity of the moment, said that the standoff with city police “is a very potentially dangerous situation for the mayor politically.”

But the de Blasio ally predicted that the mayor is “going to weather it through. He will be evenhanded. He’s not anti-cop. [But] he is not going to be buffaloed.”