1 00:00:02,150 --> 00:00:04,570 So let's now make sure we can also react 2 00:00:04,570 --> 00:00:07,630 to a notification if the app was closed. 3 00:00:07,630 --> 00:00:12,113 And for this notifications, this notifications package, 4 00:00:13,020 --> 00:00:17,540 this one here, simply has a number useful method. 5 00:00:17,540 --> 00:00:20,500 So again in use effect, 6 00:00:20,500 --> 00:00:22,440 and here I'll use the same effect 7 00:00:22,440 --> 00:00:25,120 as I did for handling incoming notifications 8 00:00:25,120 --> 00:00:27,000 if the app was open, 9 00:00:27,000 --> 00:00:29,350 we can also call a number method 10 00:00:29,350 --> 00:00:32,120 on the notifications object here. 11 00:00:32,120 --> 00:00:34,900 And that would be the add notification 12 00:00:34,900 --> 00:00:37,660 response received listener. 13 00:00:37,660 --> 00:00:40,920 This now allows you to define a function 14 00:00:40,920 --> 00:00:44,090 that should run when a user interacted 15 00:00:44,090 --> 00:00:49,090 with a notification whilst the app was not running. 16 00:00:49,600 --> 00:00:52,420 So when a user taps on a notification. 17 00:00:52,420 --> 00:00:54,600 Here we get a response object, 18 00:00:54,600 --> 00:00:57,040 and we can console lock that response 19 00:00:57,040 --> 00:00:58,563 to see what's inside of it. 20 00:01:00,260 --> 00:01:01,560 Now just as before, 21 00:01:01,560 --> 00:01:04,020 here, we also get a subscription, 22 00:01:04,020 --> 00:01:06,040 and now to avoid a name clash, 23 00:01:06,040 --> 00:01:09,150 I'll name this background subscription, 24 00:01:09,150 --> 00:01:12,410 and I'll name this foreground subscription 25 00:01:12,410 --> 00:01:14,400 but of course those names are up to you. 26 00:01:14,400 --> 00:01:16,510 And then here in the cleanup function, 27 00:01:16,510 --> 00:01:19,760 we wanna clean up both the background subscription 28 00:01:19,760 --> 00:01:23,520 as well as the foreground subscription of course. 29 00:01:23,520 --> 00:01:26,860 And you see those API's are very similar, 30 00:01:26,860 --> 00:01:29,240 we work with them in the same way, 31 00:01:29,240 --> 00:01:31,140 we get different data though. 32 00:01:31,140 --> 00:01:33,490 Here we get the notification, 33 00:01:33,490 --> 00:01:36,400 here we get a response object. 34 00:01:36,400 --> 00:01:37,830 Well, let's look into this 35 00:01:37,830 --> 00:01:40,370 and see what's inside of that response. 36 00:01:40,370 --> 00:01:41,640 So for that, again, 37 00:01:41,640 --> 00:01:44,610 I'll trigger the notification on Android, 38 00:01:44,610 --> 00:01:48,630 actually I'll first of all clear those old notifications, 39 00:01:48,630 --> 00:01:51,350 then I will trigger it and now 40 00:01:51,350 --> 00:01:54,140 I'll put the app in background mode. 41 00:01:54,140 --> 00:01:55,830 So I'm back on the homescreen, 42 00:01:55,830 --> 00:01:59,720 and now we can wait for the notification to arrive. 43 00:01:59,720 --> 00:02:02,310 Here it is, let's tap on it, 44 00:02:02,310 --> 00:02:05,480 this opens the app and now here in the terminal, 45 00:02:05,480 --> 00:02:07,193 we got a new output. 46 00:02:11,440 --> 00:02:13,000 Down here it is. 47 00:02:13,000 --> 00:02:15,530 This is now that response object. 48 00:02:15,530 --> 00:02:17,320 And actually it's almost the same 49 00:02:17,320 --> 00:02:19,890 as the notification we got before, 50 00:02:19,890 --> 00:02:23,400 it's just an object that holds the notification 51 00:02:23,400 --> 00:02:26,550 but it also gives us this action identifier, 52 00:02:26,550 --> 00:02:29,250 which we can actually ignore here. 53 00:02:29,250 --> 00:02:32,190 Instead, we see we have a notification key, 54 00:02:32,190 --> 00:02:35,070 and there will again be able to get the data, 55 00:02:35,070 --> 00:02:38,610 the body, the title, and whatever we need. 56 00:02:38,610 --> 00:02:40,580 We even get information about 57 00:02:40,580 --> 00:02:42,620 why the notification occurred now. 58 00:02:42,620 --> 00:02:46,083 For example, that it's simply had a timer for 10 seconds. 59 00:02:47,310 --> 00:02:48,520 So that's pretty neat. 60 00:02:48,520 --> 00:02:50,230 Let's also test this on iOS, 61 00:02:50,230 --> 00:02:52,430 and they offer I'll some random iOS 62 00:02:52,430 --> 00:02:55,840 and make sure that I have the iOS console here, 63 00:02:55,840 --> 00:02:58,350 and now let me trigger the notification, 64 00:02:58,350 --> 00:03:00,440 put the app in background mode 65 00:03:00,440 --> 00:03:03,400 and let's wait for the notification. 66 00:03:03,400 --> 00:03:05,670 Let's wait for it to arrive and once it's there, 67 00:03:05,670 --> 00:03:08,130 we'll tap on it here on iOS as well, 68 00:03:08,130 --> 00:03:11,150 theses opens the app, and now here, 69 00:03:11,150 --> 00:03:13,320 we also got an output on iOS, 70 00:03:13,320 --> 00:03:16,170 we got an object here with notification data 71 00:03:16,170 --> 00:03:18,200 just as on Android. 72 00:03:18,200 --> 00:03:20,140 Now, what you'll see is that 73 00:03:20,140 --> 00:03:23,930 some data fields differ, because it's iOS, 74 00:03:23,930 --> 00:03:26,820 but generally your data is here, 75 00:03:26,820 --> 00:03:30,150 with your special data, your title is here, 76 00:03:30,150 --> 00:03:34,500 your body is here, so you can drill into this notification 77 00:03:34,500 --> 00:03:37,320 there into the request, into the content, 78 00:03:37,320 --> 00:03:39,810 just as on Android, and then into the body, 79 00:03:39,810 --> 00:03:41,563 data or whatever you need. 80 00:03:42,670 --> 00:03:45,410 And that's how you can handle notifications 81 00:03:45,410 --> 00:03:48,710 when they arrive when your app is not running. 82 00:03:48,710 --> 00:03:52,040 And again here, instead of console logging, 83 00:03:52,040 --> 00:03:54,140 you can of course do whatever you need to do. 84 00:03:54,140 --> 00:03:55,980 Navigate to a different screen, 85 00:03:55,980 --> 00:04:00,280 send a HTTP requests to a server, lock the user out, 86 00:04:00,280 --> 00:04:02,310 do what you wanna do in your app. 87 00:04:02,310 --> 00:04:05,440 It's your app, you're running this inside of user fact, 88 00:04:05,440 --> 00:04:07,670 inside of a component, so you can do 89 00:04:07,670 --> 00:04:09,690 what you can always do in components. 90 00:04:09,690 --> 00:04:11,823 There are basically no limits.