Knowledge of the Unseen in Islam
Question

My friend asked me a question about knowledge of the unseen, which was as follows: We know for sure that Allah knows what has happened and what will happen, and how what did not happen would have happened if it did happen. His knowledge was written in the Preserved Book before He began to create everything. If I set the alarm on my phone for a certain time, and I tell the people around me that the phone will ring at the time that I already set, does that mean that I know the unseen?! The answer is definitely not, for I surely do not know the unseen. The question now is: how can we say that Allah knows the unseen (al-ghayb) when He has already written everything that will happen in this universe before He created it? This is similar to the example about setting the alarm.
Praise be to Allah.Types of the unseen (al-ghayb)      
The unseen is of two types:
1. That which is absolutely unseen and unknown . No one knows this type except Allah. For example, this includes knowledge of when the Hour will begin, when rain will fall, and so on.
2. That which is relatively unseen and unknown, knowledge of which is hidden from some people and others know it. This is described as unseen or unknown in relation to the one who does not know it, and it is not unknown or unseen for the one who does know it.
So in Islam, the unseen is that which is hidden from people’s senses, whether it remains hidden and concealed, and no human can comprehend it, in the sense that no one knows it except the All-Aware, or people may learn of it because of a religious text from the Quran or hadith.
A person may learn about some of the unseen on the basis of his rational thinking, or by some other means, such as obtaining knowledge through some means that help to enhance the senses, like telescopes and other equipment. This comes under the heading of things that are relatively unseen or unknown.
The importance of believing in the unseen (al-ghayb)
Belief in the unseen is one of the characteristics that distinguish humans from other living beings. Animals have in common with humans the ability to learn about what is tangible, but as for the unseen, humans alone are qualified to believe in it, unlike animals. Hence belief in the unseen (al-ghayb) is one of the pillars of faith in all the divinely-revealed religions. The revealed books spoke a great deal about unseen matters that man has no way of knowing except through proven revelation in the Quran and Sunnah, such as mention of Allah, may He be exalted, and His attributes and actions; the seven heavens and what they contain; the angels, the prophets, Paradise and Hell, the shayatin (devils) and jinn, and other matters of belief in the unseen that no one could learn about or understand except through the sound religious texts of the Quran and Sunnah.
Detailed account on types of the unseen (al-ghayb)
1. That which is absolutely unseen, which is that of which man has no way of becoming aware and learning through the means of his physical faculties. It is of two types:
The first type is that of which Allah, may He be exalted, has informed people, or some of it, through revelation to the messengers, who conveyed it to the people.
One example of that is the shayatin and the jinn, and what He has said about them, as in the verses (interpretation of the meaning):
{It has been revealed to me that a group of the jinn listened and said, ‘'Indeed, we have heard an amazing Quran. It guides to the right course, and we have believed in it. And we will never associate with our Lord anyone’.} [al-Jinn 72:1-2]
The second type is that of which He has kept the knowledge to Himself, and has not disclosed it to any of His creation, whether a prophet who was sent or an angel who is close to Him. This is what is referred to in the verse (interpretation of the meaning):
{And with Him are the keys of the unseen; none knows them except Him.} [al-An'am 6:59]
An example of that is knowledge of when the Hour will begin, or the time, place and cause of one’s death, and some of the names by which Allah, may He be exalted, has called Himself, as He says (interpretation of the meaning):
{Indeed, Allah [Alone] has knowledge of the Hour and sends down the rain and knows what is in the wombs. And no soul perceives what it will earn tomorrow, and no soul perceives in what land it will die.} [Luqman 31:34]
The Prophet (blessings and peace of Allah be upon him) said in one of his supplications: “O Allah, indeed I ask You by every name that belongs to You, by which You have called Yourself or taught it to any of Your creation, or kept it to Yourself in the knowledge of the unseen which is with You.”
2. That which is restricted and relative. This is what is unknown to some, such as historical events. They are unseen or unknown in relation to those who do not know of them. Hence Allah, may He be exalted, said to the Prophet (blessings and peace of Allah be upon him) after mentioning the story of the family of ‘Imran (interpretation of the meaning):
{That is from the news of the unseen which We reveal to you, [O Muhammad]. And you were not with them when they cast their pens as to which of them should be responsible for Mary. Nor were you with them when they disputed.} [Al ‘Imran 3:44]
3. That which is restricted but is not relative. This refers to that which people do not experience or see because of barriers of time (the future) or place, or other barriers, until that thing is made known because of the removal of the temporal or spatial barrier, as in the verse in which Allah, may He be exalted, says (interpretation of the meaning):
{And when We decreed for Solomon death, nothing indicated to the jinn his death except a creature of the earth eating his staff. But when he fell, it became clear to the jinn that if they had known the unseen, they would not have remained in humiliating punishment.} [Saba 4:14]
This refers to the death of Sulayman (peace be upon him).
Examples of matters of the unseen
1. The ruh (soul). Allah, may He be exalted, says (interpretation of the meaning):
{And they ask you, [O Muhammad], about the soul. Say, “The soul is of the affair of my Lord. And mankind has not been given of knowledge except a little”.} [al-Isra 17:85]
2. The minor and major signs of the Hour, of which the Prophet (blessings and peace of Allah be upon him) spoke in the hadith of Jibril: “When you see the barefoot, naked, destitute shepherds competing in the construction of lofty buildings.”
These are some of the matters of the unseen of which the Prophet (blessings and peace of Allah be upon him) spoke and they have come to pass. Among the major signs of which he spoke are: the Dajjal, who will emerge at the end of time; and the Beast, which will also emerge at the end of time. (From al-‘Aqidah, Ministry of Awqaf and Islamic affairs)
Thus it becomes clear to you that what your friend did come under the heading of that which is relatively unseen, and not that which is absolutely unseen. So the fact that he knows about it and similar things for which there are tangible causes is not something that should be a cause for confusion.
Moreover, it is well established for any man of reason that there are many matters which may turn out differently from what someone said would happen, and may turn out differently from what was forecast by one who has knowledge of such matters. How often one says that the train or plane will arrive at the appointed time, then that does not happen, or something may be said about a foetus on the basis of modern equipment, then it does not turn out as he said. It may be that you stop the clock before the alarm goes off, so it does not go off, or the battery runs out, so the alarm does not go off… Any confusion about such matters is baseless and pointless, and it is not difficult to resolve. It is just a trick from the Shaytan to deceive one who has nothing to do about an issue that has no reality and no basis, were it not for this person allowing himself to pursue trivial matters and the shayatin distracting the sons of Adam.
The issue of the decree being written comes under the heading of belief in the divine will and decree (al-qada’ wa’l-qadar)
With regard to the decree being written, it has nothing to do with what you mentioned at all, and we are surprised at this question of yours.
Asking about such matters may be valid, if Allah had revealed to us in His Book that the alarm would never ring, then it rang. In this case, what He told us would be different from reality.
As for a person knowing about something, whether in the past or present or even in the future, that did not happen, and he came to know about that through his own means, where is the confusion about that when we believe that everything that will happen until the Day of Resurrection is written in al-Lawh al-Mahfuz?
Whatever the case, the belief that Allah wrote everything comes under the same heading as believing in the divine will and decree, which means believing that Allah wrote all of that in al-Lawh al-Mahfuz fifty thousand years before He created the heavens and the earth.
What is required for belief in the divine decree to be sound is to believe the following:
1.   That man has free will and choice on the basis of which he carries out his actions, as Allah, may He be exalted, says (interpretation of the meaning):
{For whoever wills among you to take a right course.} [at-Takwir 81:28]
{Allah does not charge a soul except [with that within] its capacity.} [al-Baqarah 2:286].
2.  A person’s will and power does not go beyond the power and will of Allah, for He is the One Who granted that to the person and made him able to distinguish and choose, as He, may He be exalted, says (interpretation of the meaning):
{And you do not will except that Allah wills - Lord of the worlds.} [at-Takwir 81:29].
With regard to setting your alarm, knowing that it is going to ring, if it rings, then that is something that Allah decreed and He created it, and you expect to happen. Otherwise some impediment could occur to prevent the phone from doing that task. All of that was decreed by Allah.
You should understand that His written decree is of two types: the first type is that which cannot be altered or changed, which is what is in al-Lawh al-Mahfuz.
The second type is that which can be changed and altered, which is what is in the hands of the angels, but how it will be settled with them in the end will be in accordance with what Allah decreed in al-Lawh al-Mahfuz. This is one of the meanings of the verse (interpretation of the meaning): {Allah eliminates what He wills or confirms, and with Him is the Mother of the Book.} [ar-Ra‘d 13:39]
Hence we can understand what is mentioned in the sahih Sunnah about the fact that upholding ties of kinship increases a person’s lifespan or provision, or that supplication (du‘a) can put back the divine decree , for Allah, may He be exalted, knew that His slave would uphold ties of kinship and that he would offer supplication, so He wrote in al-Lawh al-Mahfuz that that person would have ample provision and an extended lifespan.
And Allah knows best.