The ambush slaying of two NYPD officers in 2014 “almost brought us to our knees” after demonstrations over grand jury decisions not to indict police officers accused of killing civilians, Commissioner James P. O’Neill said Wednesday.

“It almost brought us to our knees, but it didn’t,” he said.

Addressing the “Keeping New York Safe” breakfast before civic and business leaders in lower Manhattan, O’Neill said during “all the demonstrations . . . those were a tough time to be a cop. But we had to listen. I know I listened. We had to evolve.”

The NYPD’s community policing program, which sends cops to neighborhood beats to get to know people instead of answering 911 calls, sprung from those difficult days. The mayor earlier this week cited the program as a reason to raise police officers’ salaries.

“I knew we had to move forward and we had to change the way we do business,” said O’Neill, who had been the NYPD’s highest-ranking uniformed member under his predecessor as commissioner, William Bratton.

On Wednesday, O’Neill recounted an epiphany upon hearing a pastor in the Bronx describe how the NYPD polices unilaterally rather than collaboratively — “Police is something that’s done to us, and not with us,” O’Neill said the pastor put it.

“We think that just because crime’s down, everything in the city is great, but it’s not, it can’t be,” he said.

advertisement | advertise on newsday

O’Neill lamented the tenor in the city in 2014 and early 2015 surrounding the NYPD — a debate echoed around the nation. It began for the department in July, after Staten Islander Eric Garner died when a police officer put him in what the medical examiner said was a banned chokehold. At the end of 2014, within weeks of each other, prosecutor-led grand juries in Ferguson, Missouri, and on Staten Island declined to indict cop in the Garner case or another accused in the death of a civilian.

Those decisions inflamed simmering tensions over how police treat minority communities and, amid protests, a mentally ill gunman who said he was intent on putting “wings on pigs” killed NYPD Officers Wenjian Liu, 32, and Rafael Ramos, 40.

“That was a tough blow to take,” said O’Neill, who came on the job in 1983. “It took some time and we regained our composure, we understood our mission and we knew we had to move forward.”

At Wednesday’s speech, O’Neill said most of the NYPD’s 36,000 cops are good but added: “Not everyone’s good. We do have issues.”