Officer Miosotis Familia, 48, was sitting in her marked vehicle when 34-year-old Alexander Bonds shot at her through the passenger side window near the intersection of 183rd Street and Grand Concourse in the Bronx.
Familia, a 12-year veteran of the force and mother of three, later died after being rushed to hospital for surgery.
Bonds was shot dead by other officers as he fled the scene.
He had a history of criminal convictions and made his hatred for police clear in anti-cop messages posted on social media.
In a Facebook Live video in September, Bonds ranted about law officers killing and abusing people and warned them to leave him alone or 'we gonna do something.'
'Police is f****ts, and this ain't no gimmick,' he said in the video.
'Don't think every brother, cousin or uncle you got that get (unintelligible) in jail is because of a Blood or Crip,' he said. 'Police be killing and saying an inmate killed them.'
N****s ain't taking it no more, Mr Officer. I'm here to tell you man. ... just keep your a** away from mine.'
The rant came months before Bonds ambushed officer Familia and killed her on Wednesday.
An innocent bystander was reportedly also shot in the stomach and is now in stable condition.
Bonds' criminal record included a 2001 arrest for beating up another NYPD officer with brass knuckles. Four others reportedly joined in assaulting the officer. The outcome of that case is not known.
Bonds was currently on parole for a 2014 robbery in Syracuse. He also had prior arrests for drug violations in 2000 and 2002. He is said to have had addresses all over the city, in the Bronx, Queens and several homeless shelters, and went by up to six different aliases.
New York City Police Commissioner James O'Neill said Familia was 'assassinated in an unprovoked attack on cops.'
The officer was in a command vehicle with her partner when the shooting occurred.
Familia's partner frantically radioed for help.
'Shots fired!' a panicked officer was heard shouting into his police radio just after shots rang out.
'I need a f*****g bus! 10-85 10-85! My partner's shot! My partner's shot! My partner's shot! Hurry up central!'
Officers caught up with Bonds about a block away and killed him when he pulled a revolver. A silver revolver was recovered from the scene, police said.
The burst of gunfire as the Fourth of July wound down was initially mistaken by some for fireworks.
Familia was a 12-year veteran of the force who spent her entire career with the department in the high-crime Bronx precinct. The command post there had been set up and staffed around the clock since a triple shooting in March.
'She was on duty serving this city, protecting people, doing what she believed in and doing the job she loved,' Mayor Bill de Blasio said.
She had three children, including a set of twins, lived with her mother and worked the midnight shift.
Friends said she became an officer to help her community, and her family was heartbroken.
'Put it this way: She'd give you the shirt off her back. She was the sweetest person you ever want to meet,' downstairs neighbor Tom Ritter said. He said his son, now 22, played with Familia's children, and she practically 'adopted' him.
Even a woman who said her husband had been arrested by Familia had only kind words for the slain officer.
'She gave me good advice, like a mother to a daughter,' said Keisha Williams, 31, who said her husband was arrested on a marijuana charge. 'She's good, but she's a tough cookie. She's a good cop. I'm just sad it was her.'
Police said they were trying to establish the motive for the shooting.
While tensions have been running high in recent years between police and black people around the country, there was no immediate indication the killing had a racial or ethnic dimension.
Bonds was black; Familia was black and Hispanic, her family having come from the Dominican Republic. She apparently had no previous contact with him.
Bonds was caught on video leaving a convenience store, then moving tightly along the wall, pulling a hoodie over his head and walking purposefully toward the command post vehicle with gloved hands.
The video didn't capture the shooting itself but showed him running away with a gun in his hand, police said.
While all the department's new patrol cars are outfitted with bullet-resistant windows, the rolling command posts don't have them.
The attack recalled the 2014 ambush killings of two New York City officers who were gunned down in their marked car in Bedford-Stuyvesant, Brooklyn.
They were killed by a man who had announced online moments before that he was planning to shoot two 'pigs' in retaliation for the police chokehold death of Eric Garner in New York. The gunman, 28-year-old Ismaaiyl Brinsley, then killed himself.