All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
A Muslim should supplicate Allah while being certain of the answer, as narrated by Ibn Majah that the prophet said: “Supplicate Allah while you are certain of the answer.”
Muslims should know that the forms of having one’s supplication answered may vary; what he asked for may be fulfilled, Allah may turn away from him a harm that is similar to his supplication, or that Allah may keep its reward for him for the Day of Resurrection. Therefore, the Muslim must fulfill this condition and have a positive opinion and trust in Allah, The Almighty, and turn to Him with a humble and confident heart; confident of His great Bounty, Graciousness, and Beneficence. He has to get rid of whispers (obsessions), doubts, and delusions. If any doubt occurs to him that he is not certain of the answer, he should just get rid of this doubt and should not pay any attention to it.
For more benefit on why supplications are not answered sometimes, please refer to Fatwa 418060.
Allah knows best.