Narrator: So let's now make sure we can also react 2 2
to a notification if the app was closed. 3 3
And for this notifications, this notifications package, 4 4
this one here, simply has a number useful method. 5 5
So again in use effect, 6 6
and here I'll use the same effect 7 7
as I did for handling incoming notifications 8 8
if the app was open, 9 9
we can also call a number method 10 10
on the notifications object here. 11 11
And that would be the add notification 12 12
response received listener. 13 13
This now allows you to define a function 14 14
that should run when a user interacted 15 15
with a notification whilst the app was not running. 16 16
So when a user taps on a notification. 17 17
Here we get a response object, 18 18
and we can console lock that response 19 19
to see what's inside of it. 20 20
Now just as before, 21 21
here, we also get a subscription, 22 22
and now to avoid a name clash, 23 23
I'll name this background subscription, 24 24
and I'll name this foreground subscription 25 25
but of course those names are up to you. 26 26
And then here in the cleanup function, 27 27
we wanna clean up both the background subscription 28 28
as well as the foreground subscription of course. 29 29
And you see those API's are very similar, 30 30
we work with them in the same way, 31 31
we get different data though. 32 32
Here we get the notification, 33 33
here we get a response object. 34 34
Well, let's look into this 35 35
and see what's inside of that response. 36 36
So for that, again, 37 37
I'll trigger the notification on Android, 38 38
actually I'll first of all clear those old notifications, 39 39
then I will trigger it and now 40 40
I'll put the app in background mode. 41 41
So I'm back on the homescreen, 42 42
and now we can wait for the notification to arrive. 43 43
Here it is, let's tap on it, 44 44
this opens the app and now here in the terminal, 45 45
we got a new output. 46 46
Down here it is. 47 47
This is now that response object. 48 48
And actually it's almost the same 49 49
as the notification we got before, 50 50
it's just an object that holds the notification 51 51
but it also gives us this action identifier, 52 52
which we can actually ignore here. 53 53
Instead, we see we have a notification key, 54 54
and there will again be able to get the data, 55 55
the body, the title, and whatever we need. 56 56
We even get information about 57 57
why the notification occurred now. 58 58
For example, that it's simply had a timer for 10 seconds. 59 59
So that's pretty neat. 60 60
Let's also test this on iOS, 61 61
and they offer I'll some random iOS 62 62
and make sure that I have the iOS console here, 63 63
and now let me trigger the notification, 64 64
put the app in background mode 65 65
and let's wait for the notification. 66 66
Let's wait for it to arrive and once it's there, 67 67
we'll tap on it here on iOS as well, 68 68
theses opens the app, and now here, 69 69
we also got an output on iOS, 70 70
we got an object here with notification data 71 71
just as on Android. 72 72
Now, what you'll see is that 73 73
some data fields differ, because it's iOS, 74 74
but generally your data is here, 75 75
with your special data, your title is here, 76 76
your body is here, so you can drill into this notification 77 77
there into the request, into the content, 78 78
just as on Android, and then into the body, 79 79
data or whatever you need. 80 80
And that's how you can handle notifications 81 81
when they arrive when your app is not running. 82 82
And again here, instead of console logging, 83 83
you can of course do whatever you need to do. 84 84
Navigate to a different screen, 85 85
send a HTTP requests to a server, lock the user out, 86 86
do what you wanna do in your app. 87 87
It's your app, you're running this inside of user fact, 88 88
inside of a component, so you can do 89 89
what you can always do in components. 90 90
There are basically no limits.