So what can we do
if we get a notification whilst the app is running?
Currently, as you saw, the notification is lost.
And sometimes that might be what you want.
But sometimes that's also not what you want.
Well, for that, we can again, use the notifications package,
because there we can actually define what should happen
if we get a notification whilst the app is running.
For that, we can use it outside of our component
just here in a JavaScript file,
which is guaranteed to be executed.
And there we can set a Notification Handler.
Now this one's an object.
And in that object,
we in the end define how incoming notifications
should be handled if the app is running.
For that we got this handle notification property,
which we can set here.
And that wants a function that should execute.
And this function and that's important,
must return an object,
which in the end tells the operating system,
what should happen when a notification is received
whilst the app is running.
So this function is not executed if we get a notification
and user taps on it or anything like that.
This is instead executed to let the operating system know
what it should do with that incoming notification
before it's even displayed to the user.
So here we return an object that describes
the desired behavior to the operating system.
And it is worth noting that actually,
you should not return the object like this,
but you should return a promise.
And a simple way of returning a promise
that yields that object eventually,
is to turn this into an async function.
Now that function here is guaranteed to return a promise.
And what you return in the function
is the value that promise will eventually yield.
And now into the object you got a couple of options.
For example, you can let the operating system know
whether it should play the default notification sound.
Whether it should set a batch to let the user know
that something happened.
Whether it should show an alert.
And here I will set should Show Alert to true.
And this tells the operating system
that it should show this default alert,
which it also shows if the app is closed.
But now that it should show it when the app is running.
So now let's save that.
And let's trigger another notification on Android.
And what we'll see is that after 10 seconds,
even though the app is running,
we see our notification here.
Here I even got a sound then as a default,
but of course we could have disabled
that with the should Play Sound option here.
Now let's all to test it on iOS, of course.
So there, let's click trigger notification.
And let's see what happens
there once the 10 seconds are up.
And we see our notification here as well.
So, that's great.
Now we know how we can control
how notifications are displayed.
Another thing we typically wanna do with notifications
is that we wanna react to when the user tapped on them.
So that we're able to do something
with the interaction of the user.
So that will be the next step.