Scholars have given various explanations for the hadith, some are as follows:
It is possible that the angel of death came inside the home of Moses عليه السلام without permission and in human form. Moses عليه السلام did not recognize him and mistook him for an intruder. When the angel said: "أجب ربك" Moses عليه السلام perceived it as a death threat from someone who wished to murder him, hence he attacked him.
It is possible for an angel to come in the form of a human. It is also possible for a prophet to not recognize an angel in this form. This is proven from the example of Abraham عليه السلام , Lot عليه السلام and Mary عليه السلام in the Quran:
فلما جاء آل لوط المرسلون قال إنكم قوم منكرون
And when the messengers came to the family of Lot, He said, "Indeed, you are people unknown."
— Quran 15:61-62
هل أتاك حديث ضيف إبراهيم المكرمين إذ دخلوا عليه فقالوا سلاما قال سلام قوم منكرون
Has there reached you the story of the honored guests of Abraham? - When they entered upon him and said, "[We greet you with] peace." He answered, "[And upon you] peace, [you are] a people unknown.
— Quran 51:24-25
فأرسلنا إليها روحنا فتمثل لها بشرا سويا قالت إني أعوذ بالرحمن منك إن كنت تقيا
Then We sent to her Our Angel, and he represented himself to her as a well-proportioned man. he said, "Indeed, I seek refuge in the Most Merciful from you, [so leave me], if you should be fearing of Allah."
— Quran 19:17-18
The angel of death came to Moses عليه السلام in the shape of a human. And it is likely that Moses عليه السلام did not recognize him. This is evident from some versions of the hadith::
إن ملك الموت كان يأتي الناس عيانا، فأتى موسى بن عمران فلطمه موسى ففقأ عينه
The angel of death came to people openly i.e. in human form. So he came to Moses, Moses slapped him and put out his eye
— Musnad Ahmad , Mustadrak al-Hakim
When the angel came he did not identify himself as an angel, nor did he explain his task, rather it seems that he only said the following words (which can be misunderstood):
جاء ملك الموت إلى موسى عليه السلام . فقال له : أجب ربك قال فلطم موسى عليه السلام عين ملك الموت ففقأها
The Angel of Death came to Musa and said: “Answer the call of your Lord.” Musa, slapped the eye of the Angel of Death and put it out.
— Muslim
When the angel returned with a healed eye, Moses عليه السلام realized that he was an angel and that is why he listened and did not attack him again.
Attacking an intruder in your home is permissible.
لو أعلم أنك تنظر لطعنت به في عينك
Had I known that you were looking I would have pierced your eye
— Bukhari
من اطلع في بيت قوم بغير إذنهم ، فقد حل لهم أن يفقؤوا عينه
Whoever looks into a house without the people's permission, it is permissible for them to put out his eyes
— Muslim
Similarly, attacking someone who wishes to kill you is permissible.
Allah giving Moses عليه السلام a choice and allowing him the possibility of living longer is not problematic, nor is it opposition to Allah's wish. Rather, it is an honor which He confers on every Prophet:
لا يموت نبي حتى يخير بين الدنيا والآخرة
No Prophet dies till he is given the option to select either the worldly life or the life of the Hereafter
— Bukhari
Allah's command does not necessarily reflect His wish. We know that He commanded Abraham to sacrifice Ishmael, but His wish was to test Abraham عليه السلام and to save Ishmael عليه السلام - hence when Abraham عليه السلام had passed the test He later changed His command and told him to sacrifice a sheep. In the same way, Allah's wish was for this event to unfold and then to give Moses the choice.
Other scholars have proposed that Moses عليه السلام recognized the angel but was given permission by Allah to hit and repel him. This event was ordained by Allah as a test for the angel of death, i.e. to check what he would do in this situation. And Allah tests His creation as He wills. Allah also tested the angels at other times, such as at the creation of Adam عليه السلام .
Some have also proposed that Moses عليه السلام attacked the angel to chastise him, as he felt that he was not following protocol.
As mentioned before, Allah has ordained a general protocol for the angel to follow before taking the soul of a prophet: when the time of death approaches, the prophet is to be given a choice to either continue living or to die.
لا يموت نبي حتى يخير بين الدنيا والآخرة
No Prophet dies till he is given the option to select either the worldly life or the life of the Hereafter
— Bukhari
While in this instance the angel on his first visit simply said:
Respond (return) to your Lord
Moses عليه السلام is more senior in status to the angel of death and hence can chastise him. We already know from the Quran that he also chastised his brother, Prophet Aaron عليه السلام :
قال يا ابن أم لا تأخذ بلحيتي ولا برأسي إني خشيت أن تقول فرقت بين بني إسرائيل ولم ترقب قولي
[Aaron] said, "O son of my mother, do not seize [me] by my beard or by my head. Indeed, I feared that you would say, 'You caused division among the Children of Israel, and you did not observe [or await] my word.' "
— Quran 20:94
And chastising an angel is less than this.
References: Fath al-Bari , Sharah Nawawi - Sahih Muslim, Tafsir al-Qurtubi, Mufhim lima Ashkala min Talkhees Muslim, Ikmaal al-Mu‘lim bi Fawaa’id Muslim , https://shamela.ws/book/95991/320 , https://shamela.ws/book/146418/4273