Instructor: So what can we do 2 2
if we get a notification whilst the app is running? 3 3
Currently, as you saw, the notification is lost. 4 4
And sometimes that might be what you want. 5 5
But sometimes that's also not what you want. 6 6
Well, for that, we can again, use the notifications package, 7 7
because there we can actually define what should happen 8 8
if we get a notification whilst the app is running. 9 9
For that, we can use it outside of our component 10 10
just here in a JavaScript file, 11 11
which is guaranteed to be executed. 12 12
And there we can set a Notification Handler. 13 13
Now this one's an object. 14 14
And in that object, 15 15
we in the end define how incoming notifications 16 16
should be handled if the app is running. 17 17
For that we got this handle notification property, 18 18
which we can set here. 19 19
And that wants a function that should execute. 20 20
And this function and that's important, 21 21
must return an object, 22 22
which in the end tells the operating system, 23 23
what should happen when a notification is received 24 24
whilst the app is running. 25 25
So this function is not executed if we get a notification 26 26
and user taps on it or anything like that. 27 27
This is instead executed to let the operating system know 28 28
what it should do with that incoming notification 29 29
before it's even displayed to the user. 30 30
So here we return an object that describes 31 31
the desired behavior to the operating system. 32 32
And it is worth noting that actually, 33 33
you should not return the object like this, 34 34
but you should return a promise. 35 35
And a simple way of returning a promise 36 36
that yields that object eventually, 37 37
is to turn this into an async function. 38 38
Now that function here is guaranteed to return a promise. 39 39
And what you return in the function 40 40
is the value that promise will eventually yield. 41 41
And now into the object you got a couple of options. 42 42
For example, you can let the operating system know 43 43
whether it should play the default notification sound. 44 44
Whether it should set a batch to let the user know 45 45
that something happened. 46 46
Whether it should show an alert. 47 47
And here I will set should Show Alert to true. 48 48
And this tells the operating system 49 49
that it should show this default alert, 50 50
which it also shows if the app is closed. 51 51
But now that it should show it when the app is running. 52 52
So now let's save that. 53 53
And let's trigger another notification on Android. 54 54
And what we'll see is that after 10 seconds, 55 55
even though the app is running, 56 56
we see our notification here. 57 57
Here I even got a sound then as a default, 58 58
but of course we could have disabled 59 59
that with the should Play Sound option here. 60 60
Now let's all to test it on iOS, of course. 61 61
So there, let's click trigger notification. 62 62
And let's see what happens 63 63
there once the 10 seconds are up. 64 64
And we see our notification here as well. 65 65
So, that's great. 66 66
Now we know how we can control 67 67
how notifications are displayed. 68 68
Another thing we typically wanna do with notifications 69 69
is that we wanna react to when the user tapped on them. 70 70
So that we're able to do something 71 71
with the interaction of the user. 72 72
So that will be the next step.