Edward Archer, 30, would tell police later that he attacked for ISIS. And Hartnett's bravery in the face of mortal danger would ensure the suspect didn't get away.
The shooter was firing as he approached Hartnett's car on foot late Thursday. Surveillance camera footage shows him continuing to fire as he reached inside the lowered driver's side window.
"He was trying to assassinate this police officer," police Commissioner Richard Ross said.
Hartnett survived despite being hit three times at close range. The bullets hit his left arm, shattering his elbow, piercing the major artery and tearing through nerves.
Hartnett, 33, was patrolling alone, and managed to radio for help.
"Shots fired! ... I'm bleeding heavily!" he shouted. "I'm bleeding. Get us another unit out here!"
Backup swarms in
Officer Jonny Castro heard the call and raced toward scene.
"I was only 2 minutes away, but it felt like it took me an hour. We almost crashed 3 separate times trying to get to you," he wrote in an open Facebook letter to Harnett, celebrating his survival.
Castro gave a play-by-play of what happened next.
Like Castro, many officers were swarming to Hartnett's location. In the meantime, the officer's wounds did not keep him down. With his left arm dangling by his side, he hopped out of his squad car -- gun drawn in his right hand -- and chased Archer, surveillance video showed.
Hartnett fired, hitting the fleeing man in the buttocks.
"Other officers chased after the shooter and caught him a block away, the smoking pistol still in his possession," Castro wrote.
Two officers put Hartnett into their patrol car and rushed him to a hospital, where surgeons worked to stop the bleeding.
'Allegiance to the Islamic State'
Archer was arraigned Saturday on charges of attempted murder, aggravated assault, assault on a police officer, reckless endangerment and firearms offenses, police said.
It was not known whether Archer entered a plea. He is being held without bail and has not yet been assigned a public defender, a court official said.
Under police questioning, Archer was specific about his alleged motivation, Philadelphia Police Capt. James Clark, commander of the homicide unit, said Friday.
"I follow Allah. I pledge my allegiance to the Islamic State and that's why I did what I did," Archer said, according to Clark.
"He believed that the police defend laws that are contrary to the teachings of the Quran," Commissioner Ross said.
Archer, who is from Yeadon, a Philadelphia suburb, traveled to Saudi Arabia in 2011 and Egypt in 2012, FBI Special Agent Eric Ruona said.
Travel to Saudi Arabia is normal for Muslims who are required once in their lifetimes to complete the Hajj pilgrimage to Mecca.
Investigators do not know if Archer had any connections to ISIS or other terrorists.
Mayor Jim Kenney said the shooting had nothing to do with Islam. "It is abhorrent," he said. "It does not represent the religion in any shape or form or any of the teachings."
Imam Asim Abdur-Rashid, head of a mosque one block away from an address associated with Archer, condemned the attack. He said he did not know whether the alleged shooter attended his mosque.
Archer's criminal history
Archer was armed with a 9mm Glock 17 that was reported stolen in 2013 from the home of a police officer, police said.
"It is one of the things that you regret the most, when an officer's gun is stolen that it is used against one of your own," Commissioner Ross said.
Archer has been in trouble with the law before over gun violence.
He was arrested in 2012 after a domestic dispute. Archer "pulled a small black and silver semiautomatic handgun from his waist and pointed it towards the complainant's stomach while grabbing the complainant's shirt," according to the affidavit for probable cause.
He pleaded guilty to assault and carrying a gun without a license, court records show.
At the time of the Thursday attack, Archer was out on probation and scheduled to be sentenced Monday for careless driving, forgery and driving with a suspended or revoked license in a 2014 case.
Almost a police funeral
Hartnett was in critical but stable condition Saturday. His colleagues were astonished he survived.
"After watching the surveillance video and replaying the scenario over and over again in my head, you definitely shouldn't be here right now," Castro wrote after standing next to the squad car where shell casings littered the street.
"It should have been 100 times worse. Not many people would have survived an ambush like that. He got the drop on you and had every intention of killing a cop," Castro wrote in the post, which has since disappeared from public view on his Facebook page.
The 9mm Glock 17 can hold 17 rounds in a standard magazine. Police said at least 11 shots were fired at Hartnett.
"His will to live undoubtedly saved his life, and we are ever so thankful to God that he is here today because this could easily have been a police funeral," Commissioner Ross said.
Hartnett will need multiple surgeries, Ross said. But Hartnett's father Robert told CNN affiliate KYW his son is improving.
His father is feeling "positive, now that I am seeing he's waking up."
Archer was treated for a gunshot wound to his buttocks and released from the hospital, police said.