1 00:00:02,230 --> 00:00:03,270 So what can we do 2 00:00:03,270 --> 00:00:06,900 if we get a notification whilst the app is running? 3 00:00:06,900 --> 00:00:10,230 Currently, as you saw, the notification is lost. 4 00:00:10,230 --> 00:00:12,470 And sometimes that might be what you want. 5 00:00:12,470 --> 00:00:15,103 But sometimes that's also not what you want. 6 00:00:16,550 --> 00:00:19,790 Well, for that, we can again, use the notifications package, 7 00:00:19,790 --> 00:00:22,830 because there we can actually define what should happen 8 00:00:22,830 --> 00:00:26,130 if we get a notification whilst the app is running. 9 00:00:26,130 --> 00:00:28,980 For that, we can use it outside of our component 10 00:00:28,980 --> 00:00:31,030 just here in a JavaScript file, 11 00:00:31,030 --> 00:00:33,750 which is guaranteed to be executed. 12 00:00:33,750 --> 00:00:37,843 And there we can set a Notification Handler. 13 00:00:38,840 --> 00:00:40,710 Now this one's an object. 14 00:00:40,710 --> 00:00:42,290 And in that object, 15 00:00:42,290 --> 00:00:46,010 we in the end define how incoming notifications 16 00:00:46,010 --> 00:00:49,720 should be handled if the app is running. 17 00:00:49,720 --> 00:00:52,610 For that we got this handle notification property, 18 00:00:52,610 --> 00:00:54,330 which we can set here. 19 00:00:54,330 --> 00:00:58,390 And that wants a function that should execute. 20 00:00:58,390 --> 00:01:00,750 And this function and that's important, 21 00:01:00,750 --> 00:01:02,960 must return an object, 22 00:01:02,960 --> 00:01:05,780 which in the end tells the operating system, 23 00:01:05,780 --> 00:01:09,590 what should happen when a notification is received 24 00:01:09,590 --> 00:01:11,230 whilst the app is running. 25 00:01:11,230 --> 00:01:15,480 So this function is not executed if we get a notification 26 00:01:15,480 --> 00:01:18,600 and user taps on it or anything like that. 27 00:01:18,600 --> 00:01:22,440 This is instead executed to let the operating system know 28 00:01:22,440 --> 00:01:25,330 what it should do with that incoming notification 29 00:01:25,330 --> 00:01:28,070 before it's even displayed to the user. 30 00:01:28,070 --> 00:01:31,060 So here we return an object that describes 31 00:01:31,060 --> 00:01:34,400 the desired behavior to the operating system. 32 00:01:34,400 --> 00:01:37,060 And it is worth noting that actually, 33 00:01:37,060 --> 00:01:39,370 you should not return the object like this, 34 00:01:39,370 --> 00:01:41,380 but you should return a promise. 35 00:01:41,380 --> 00:01:43,760 And a simple way of returning a promise 36 00:01:43,760 --> 00:01:46,280 that yields that object eventually, 37 00:01:46,280 --> 00:01:49,060 is to turn this into an async function. 38 00:01:49,060 --> 00:01:52,590 Now that function here is guaranteed to return a promise. 39 00:01:52,590 --> 00:01:54,630 And what you return in the function 40 00:01:54,630 --> 00:01:58,630 is the value that promise will eventually yield. 41 00:01:58,630 --> 00:02:01,110 And now into the object you got a couple of options. 42 00:02:01,110 --> 00:02:03,820 For example, you can let the operating system know 43 00:02:03,820 --> 00:02:06,803 whether it should play the default notification sound. 44 00:02:08,250 --> 00:02:10,870 Whether it should set a batch to let the user know 45 00:02:10,870 --> 00:02:12,410 that something happened. 46 00:02:12,410 --> 00:02:14,920 Whether it should show an alert. 47 00:02:14,920 --> 00:02:19,030 And here I will set should Show Alert to true. 48 00:02:19,030 --> 00:02:20,810 And this tells the operating system 49 00:02:20,810 --> 00:02:23,460 that it should show this default alert, 50 00:02:23,460 --> 00:02:26,240 which it also shows if the app is closed. 51 00:02:26,240 --> 00:02:29,003 But now that it should show it when the app is running. 52 00:02:32,080 --> 00:02:34,010 So now let's save that. 53 00:02:34,010 --> 00:02:36,693 And let's trigger another notification on Android. 54 00:02:38,280 --> 00:02:41,510 And what we'll see is that after 10 seconds, 55 00:02:41,510 --> 00:02:43,420 even though the app is running, 56 00:02:43,420 --> 00:02:45,760 we see our notification here. 57 00:02:45,760 --> 00:02:48,330 Here I even got a sound then as a default, 58 00:02:48,330 --> 00:02:49,750 but of course we could have disabled 59 00:02:49,750 --> 00:02:52,373 that with the should Play Sound option here. 60 00:02:55,960 --> 00:02:58,530 Now let's all to test it on iOS, of course. 61 00:02:58,530 --> 00:03:01,173 So there, let's click trigger notification. 62 00:03:02,220 --> 00:03:03,570 And let's see what happens 63 00:03:03,570 --> 00:03:05,943 there once the 10 seconds are up. 64 00:03:10,320 --> 00:03:13,640 And we see our notification here as well. 65 00:03:13,640 --> 00:03:14,990 So, that's great. 66 00:03:14,990 --> 00:03:17,350 Now we know how we can control 67 00:03:17,350 --> 00:03:20,029 how notifications are displayed. 68 00:03:20,029 --> 00:03:23,820 Another thing we typically wanna do with notifications 69 00:03:23,820 --> 00:03:27,890 is that we wanna react to when the user tapped on them. 70 00:03:27,890 --> 00:03:29,930 So that we're able to do something 71 00:03:29,930 --> 00:03:31,960 with the interaction of the user. 72 00:03:31,960 --> 00:03:34,153 So that will be the next step.