Instructor: So in order to also get 2 2
local notifications on iOS, 3 3
we need to ask the user for permission. 4 4
And for this, we an use another package 5 5
which we also install with expo install, 6 6
and that's the expo-permissions package. 7 7
The expo-permissions package, as the name implies, 8 8
helps us manage permissions. 9 9
It helps us request permissions, and work with permissions. 10 10
So once this is installed, 11 11
in App.js or in any other file 12 12
where you want to get those permissions, 13 13
but here we only have this one component, 14 14
we simply import everything as permissions 15 15
from expo-permissions, just like that. 16 16
And with imported, we need to ask the user for permission 17 17
to show notifications before we try doing that. 18 18
Now, it depends on your application 19 19
when you want to ask the user. 20 20
Maybe you wanna ask when the app starts up, 21 21
maybe a better point of time is better. 22 22
After all, you wanna ask for permissions 23 23
when it's very likely that the user 24 24
gives you the permission, right? 25 25
Now here we'll do it right when the app starts up, 26 26
so I will actually import the useEffect hook from react 27 27
so that we can run logic when this component mounts, 28 28
simply by passing an empty dependencies array here. 29 29
And in there I now wanna use this permissions object 30 30
which we imported here, to first of all check 31 31
the permission status. 32 32
And thereafter, if we don't have permission, to ask for it. 33 33
So therefore here, we can call getAsync 34 34
to first of all check the current permission status. 35 35
We now need to specify for which permission 36 36
we wanna get that information, 37 37
and we can therefore access Permissions.NOTIFICATIONS... 38 38
Whoops, all caps, though. 39 39
To check how the current permissions status is 40 40
for notification permissions. 41 41
On Android this will do nothing, 42 42
on iOS it will find out whether we already are allowed 43 43
to send permissions. 44 44
Now, this is a async task, 45 45
and therefore it gives us a promise. 46 46
Hence, we can use then here to proceed. 47 47
Now in here, we'll get the status of that permission. 48 48
So we'll get the information whether we do have permission 49 49
to send notifications or not. 50 50
Now we can check which status we got. 51 51
There are a couple of possible values here, 52 52
but in the end we're interested in the granted status. 53 53
If we got that granted status, we don't need to continue. 54 54
But if we don't have the granted status yet, 55 55
we wanna ask for permission, 56 56
because we haven't been granted those permissions before. 57 57
So now we can use the permissions object again, 58 58
but this time we call askAsync. 59 59
Before, we checked the status, 60 60
now we're asking for a new permission 61 61
because now we know that we don't yet have the permission 62 62
which we need. 63 63
So now here we again access Permissions.NOTIFICATIONS, 64 64
and now this will ask for getting that permission. 65 65
This will return a promise, so we can overall return it here 66 66
to stay in this promise chain which we've started here. 67 67
And therefore, now here, we can again use then. 68 68
You could also use async await, 69 69
but in useEffect you shouldn't make this function async, 70 70
instead you would have to wrap this into a new function 71 71
which you immediately execute, just as a side note. 72 72
But here, I will use good old then catch, 73 73
and we'll reach the function in this then block 74 74
after askAsync finished. 75 75
Now again, here we'll get the status. 76 76
Because of course, just because we're asking 77 77
does not mean that we're getting the permission. 78 78
So the user might still deny the permission, 79 79
and in this case there is nothing we can do. 80 80
So now we wanna check this status 81 81
and find out if it's still not granted. 82 82
And if that is the case, then well, as I said, 83 83
there's nothing we can do. 84 84
So here we can just return. 85 85
We might show a message to the user, 86 86
we might an alert that we now can't send any notifications. 87 87
But in the end, there is nothing we can do here. 88 88
If the status is granted though, then we're good. 89 89
Now either way, we can definitely schedule a notification, 90 90
because the worst case scenario 91 91
is that we don't have permissions 92 92
and therefore nothing happens. 93 93
But now that we're asking, the best case scenario 94 94
is that we did get the permission 95 95
and therefore this here will succeed. 96 96
Now actually, I have to step in here. 97 97
This code has an error. 98 98
We don't get the status string like this here, 99 99
instead we get the statusObj, 100 100
which then has a status property. 101 101
And the same is the case here. 102 102
We just didn't get an error before, 103 103
because even though our code technically didn't work, 104 104
we did request their permission still. 105 105
But now this properly checks whether we have the permission. 106 106
And in addition, we also have to adjust it 107 107
in one other place. 108 108
Here, if we already did have the permission, 109 109
we of course will get granted as a response here, hopefully. 110 110
And we'll not ask again. 111 111
That means that in the next then block, 112 112
we have a problem though. 113 113
Because there we now expect to get our statusObj, 114 114
but since we didn't ask again because we didn't need to, 115 115
we'll not get that object here. 116 116
So, accessing status will fail and we'll think 117 117
that we didn't get the permission, even though we have it. 118 118
So here we should return statusObj in that first then block 119 119
so that in the next then block it is available 120 120
for the cases where we already had permission. 121 121
This is a change you should implement 122 122
to ensure that your application properly detects 123 123
its notifications permission status 124 124
and does not think it doesn't have permissions, 125 125
when it actually has them. 126 126
So let's now save this. 127 127
And make sure to restart that on your iPhone simulator. 128 128
And now, right at the start, 129 129
I'm asked whether I wanna grant this permission, 130 130
because of that code we just added. 131 131
And if we now click allow here, 132 132
and trigger the notification and close the application, 133 133
let's see if that now works here on iOS. 134 134
Let's wait for the 10 seconds to go by. 135 135
And here's our first local notification sent by our app. 136 136
And now we got that working on both iOS and Android. 137 137
And just as on Android, if we click on a notification here, 138 138
we can open our app. 139 139
So that's working. 140 140
Now, one thing that did not work or where nothing happened, 141 141
was when the app is in the foreground. 142 142
Now in some cases that is the behavior you want. 143 143
In some cases, if the app is open, 144 144
not showing the notification might be absolutely fine. 145 145
But if I come back to the reminder app example, 146 146
just because it's in the foreground 147 147
might not mean that you don't want to let the user know 148 148
that some task is due. 149 149
So therefore, let me next show you 150 150
how you can handle notifications 151 151
if the app is in the foreground.