Instructor: Thankfully, handling push notifications 2 2
is made easy with Expo. 3 3
And there are various steps which we already implemented, 4 4
which we will also need to implement 5 5
if we are looking to handle push notifications. 6 6
For example, we also need to ask for permission, 7 7
if we want to get push notifications. 8 8
So that permission, 9 9
which we're getting here for receiving notifications, 10 10
that will in the end enabled 11 11
both local and push notifications you could say. 12 12
So that is code we absolutely need on iOS only, 13 13
but they are we need it. 14 14
We will also need our handlers 15 15
for handling incoming notifications when the app 16 16
is in foreground and when the app is in background, 17 17
because actually push notifications 18 18
will still cause a local notification once they arrive. 19 19
So for handling the message, once it's on our device, 20 20
our code is exactly the same. 21 21
So this does not change. 22 22
But what will change, 23 23
of course, is how we schedule a notification. 24 24
We're not going to do this with this code anymore. 25 25
Instead, I first of all wanna start with showing 26 26
you how a message could be sent 27 27
from totally outside of the device. 28 28
And then as a second step, 29 29
I will show you how our app here, 30 30
could send a notification to itself, 31 31
but to itself or running on a different device 32 32
on a different user. 33 33
That would be the scenario 34 34
you have in a chat app for example. 35 35
That your app wants to send a notification to, well, 36 36
the same app, but the same app on a different device, 37 37
the device of the user to whom you just send a chat message. 38 38
That will be the second step. 39 39
First of all, I'll show you how to send 40 40
a notification totally from outside the app. 41 41
But for that one important step is missing. 42 42
And that's that identification with Google's 43 43
and Apple's push servers, because as I mentioned, 44 44
we will need to do that. 45 45
And those servers will then give us this token, this ID, 46 46
which we ultimately need to send push notifications. 47 47
And that's therefore code we have to add here. 48 48
Besides asking for permission here, 49 49
if we plan on receiving push notifications, 50 50
we also need to sign this app installation up 51 51
with those official push servers. 52 52
So this app running on a device of our user, 53 53
needs to register itself with those official push servers. 54 54
And for that, we need to add some code. 55 55
Now we will need to make some changes here, 56 56
to our permissions though 57 57
or not to the permissions we'll still need those. 58 58
But after we have those permissions 59 59
will now need to get that token, that ID. 60 60
We'll need to sign our app up 61 61
with those official push notification servers 62 62
and get that ID which then allows us 63 63
to use the ID to push notifications 64 64
to this app installation on this device. 65 65
Now, thankfully, Expo makes that super, super easy. 66 66
It makes it very simple. 67 67
Because the team behind Expo, 68 68
basically did not just give us 69 69
all these nice JavaScript functions 70 70
and API's for building React Native apps 71 71
in a convenient way, 72 72
they also have their own backend server, 73 73
to which you can talk through some functions, 74 74
where they have all the logic 75 75
for signing up an app with those official push servers 76 76
and getting that permission. 77 77
Actually, to be precise, 78 78
Expo does not sign our app up with those services. 79 79
Instead, Expo signed itself up with those services. 80 80
And it allows us to use its sign up you could say. 81 81
So Expo, the team and the software 82 82
does all the heavy lifting behind the scenes for us, 83 83
so that we don't manually need to sign up our app, 84 84
but we can leverage Expo's existing server 85 85
to push notifications through Expo's subscription 86 86
with those official servers 87 87
to our different app installations, users and devices. 88 88
That is how you can think about that. 89 89
And that will make implementing 90 90
push notifications with Expo very, very easy. 91 91
I can tell you that if you would need to sign up manually, 92 92
that would be a lot of steps, a lot of work. 93 93
And Expo is handling this for you in a very, 94 94
very elegant way. 95 95
Expo really makes it easy to deliver 96 96
push notifications to your applications. 97 97
What do you need to do? 98 98
Well, after we got our permissions here, 99 99
in the next then block, 100 100
we want to tell Expo and their own server 101 101
which they have for us, 102 102
to sign our app up with those official push servers. 103 103
We'll need to tell Expo to basically sign 104 104
our app up so to say. 105 105
Behind the scenes Expo will do all of that, 106 106
behind the scenes Expo will sign up, 107 107
and then our app will leverage Expo 108 108
to deliver push notifications. 109 109
Now for that, we need to ensure that we only reach 110 110
this then block if we do have an active permission. 111 111
Therefore here, if the permission was not granted, 112 112
we don't really wanna return, 113 113
because then the next then block would still be triggered, 114 114
even though we don't have permission. 115 115
Instead, here I wanna throw a new error. 116 116
Permission not granted. 117 117
With this we'll ensure that this code 118 118
only runs if we do have permissions. 119 119
You will see I now get a warning 120 120
that permissions were not granted, 121 121
even though we did grant permissions before here. 122 122
The reason for that simply can be seen if we console log, 123 123
this status object here. 124 124
If we do that, we see indeed, 125 125
I do have a status of granted here. 126 126
But before that we have a status of undetermined. 127 127
Which simply means it's still figuring out 128 128
whether we have permissions or not. 129 129
That is okay because ultimately 130 130
it should see that we do have them. 131 131
So we can simply get rid of the warning 132 132
by adding a catch block here. 133 133
And then here for the moment, I'll not return anything. 134 134
But with that we no longer have an unhandled 135 135
promise rejection and therefore this warning goes away. 136 136
Again, getting the permission should work. 137 137
Now with those permissions out of the way, 138 138
let's see how we can get this push token 139 139
which we need to deliver messages.