So in order to also get
local notifications on iOS,
we need to ask the user for permission.
And for this, we an use another package
which we also install with expo install,
and that's the expo-permissions package.
The expo-permissions package, as the name implies,
helps us manage permissions.
It helps us request permissions, and work with permissions.
So once this is installed,
in App.js or in any other file
where you want to get those permissions,
but here we only have this one component,
we simply import everything as permissions
from expo-permissions, just like that.
And with imported, we need to ask the user for permission
to show notifications before we try doing that.
Now, it depends on your application
when you want to ask the user.
Maybe you wanna ask when the app starts up,
maybe a better point of time is better.
After all, you wanna ask for permissions
when it's very likely that the user
gives you the permission, right?
Now here we'll do it right when the app starts up,
so I will actually import the useEffect hook from react
so that we can run logic when this component mounts,
simply by passing an empty dependencies array here.
And in there I now wanna use this permissions object
which we imported here, to first of all check
the permission status.
And thereafter, if we don't have permission, to ask for it.
So therefore here, we can call getAsync
to first of all check the current permission status.
We now need to specify for which permission
we wanna get that information,
and we can therefore access Permissions.NOTIFICATIONS...
Whoops, all caps, though.
To check how the current permissions status is
for notification permissions.
On Android this will do nothing,
on iOS it will find out whether we already are allowed
to send permissions.
Now, this is a async task,
and therefore it gives us a promise.
Hence, we can use then here to proceed.
Now in here, we'll get the status of that permission.
So we'll get the information whether we do have permission
to send notifications or not.
Now we can check which status we got.
There are a couple of possible values here,
but in the end we're interested in the granted status.
If we got that granted status, we don't need to continue.
But if we don't have the granted status yet,
we wanna ask for permission,
because we haven't been granted those permissions before.
So now we can use the permissions object again,
but this time we call askAsync.
Before, we checked the status,
now we're asking for a new permission
because now we know that we don't yet have the permission
which we need.
So now here we again access Permissions.NOTIFICATIONS,
and now this will ask for getting that permission.
This will return a promise, so we can overall return it here
to stay in this promise chain which we've started here.
And therefore, now here, we can again use then.
You could also use async await,
but in useEffect you shouldn't make this function async,
instead you would have to wrap this into a new function
which you immediately execute, just as a side note.
But here, I will use good old then catch,
and we'll reach the function in this then block
after askAsync finished.
Now again, here we'll get the status.
Because of course, just because we're asking
does not mean that we're getting the permission.
So the user might still deny the permission,
and in this case there is nothing we can do.
So now we wanna check this status
and find out if it's still not granted.
And if that is the case, then well, as I said,
there's nothing we can do.
So here we can just return.
We might show a message to the user,
we might an alert that we now can't send any notifications.
But in the end, there is nothing we can do here.
If the status is granted though, then we're good.
Now either way, we can definitely schedule a notification,
because the worst case scenario
is that we don't have permissions
and therefore nothing happens.
But now that we're asking, the best case scenario
is that we did get the permission
and therefore this here will succeed.
Now actually, I have to step in here.
This code has an error.
We don't get the status string like this here,
instead we get the statusObj,
which then has a status property.
And the same is the case here.
We just didn't get an error before,
because even though our code technically didn't work,
we did request their permission still.
But now this properly checks whether we have the permission.
And in addition, we also have to adjust it
in one other place.
Here, if we already did have the permission,
we of course will get granted as a response here, hopefully.
And we'll not ask again.
That means that in the next then block,
we have a problem though.
Because there we now expect to get our statusObj,
but since we didn't ask again because we didn't need to,
we'll not get that object here.
So, accessing status will fail and we'll think
that we didn't get the permission, even though we have it.
So here we should return statusObj in that first then block
so that in the next then block it is available
for the cases where we already had permission.
This is a change you should implement
to ensure that your application properly detects
its notifications permission status
and does not think it doesn't have permissions,
when it actually has them.
So let's now save this.
And make sure to restart that on your iPhone simulator.
And now, right at the start,
I'm asked whether I wanna grant this permission,
because of that code we just added.
And if we now click allow here,
and trigger the notification and close the application,
let's see if that now works here on iOS.
Let's wait for the 10 seconds to go by.
And here's our first local notification sent by our app.
And now we got that working on both iOS and Android.
And just as on Android, if we click on a notification here,
we can open our app.
So that's working.
Now, one thing that did not work or where nothing happened,
was when the app is in the foreground.
Now in some cases that is the behavior you want.
In some cases, if the app is open,
not showing the notification might be absolutely fine.
But if I come back to the reminder app example,
just because it's in the foreground
might not mean that you don't want to let the user know
that some task is due.
So therefore, let me next show you
how you can handle notifications
if the app is in the foreground.