So let's now make sure we can also react
to a notification if the app was closed.
And for this notifications, this notifications package,
this one here, simply has a number useful method.
So again in use effect,
and here I'll use the same effect
as I did for handling incoming notifications
if the app was open,
we can also call a number method
on the notifications object here.
And that would be the add notification
response received listener.
This now allows you to define a function
that should run when a user interacted
with a notification whilst the app was not running.
So when a user taps on a notification.
Here we get a response object,
and we can console lock that response
to see what's inside of it.
Now just as before,
here, we also get a subscription,
and now to avoid a name clash,
I'll name this background subscription,
and I'll name this foreground subscription
but of course those names are up to you.
And then here in the cleanup function,
we wanna clean up both the background subscription
as well as the foreground subscription of course.
And you see those API's are very similar,
we work with them in the same way,
we get different data though.
Here we get the notification,
here we get a response object.
Well, let's look into this
and see what's inside of that response.
So for that, again,
I'll trigger the notification on Android,
actually I'll first of all clear those old notifications,
then I will trigger it and now
I'll put the app in background mode.
So I'm back on the homescreen,
and now we can wait for the notification to arrive.
Here it is, let's tap on it,
this opens the app and now here in the terminal,
we got a new output.
Down here it is.
This is now that response object.
And actually it's almost the same
as the notification we got before,
it's just an object that holds the notification
but it also gives us this action identifier,
which we can actually ignore here.
Instead, we see we have a notification key,
and there will again be able to get the data,
the body, the title, and whatever we need.
We even get information about
why the notification occurred now.
For example, that it's simply had a timer for 10 seconds.
So that's pretty neat.
Let's also test this on iOS,
and they offer I'll some random iOS
and make sure that I have the iOS console here,
and now let me trigger the notification,
put the app in background mode
and let's wait for the notification.
Let's wait for it to arrive and once it's there,
we'll tap on it here on iOS as well,
theses opens the app, and now here,
we also got an output on iOS,
we got an object here with notification data
just as on Android.
Now, what you'll see is that
some data fields differ, because it's iOS,
but generally your data is here,
with your special data, your title is here,
your body is here, so you can drill into this notification
there into the request, into the content,
just as on Android, and then into the body,
data or whatever you need.
And that's how you can handle notifications
when they arrive when your app is not running.
And again here, instead of console logging,
you can of course do whatever you need to do.
Navigate to a different screen,
send a HTTP requests to a server, lock the user out,
do what you wanna do in your app.
It's your app, you're running this inside of user fact,
inside of a component, so you can do
what you can always do in components.
There are basically no limits.