Thankfully, handling push notifications
is made easy with Expo.
And there are various steps which we already implemented,
which we will also need to implement
if we are looking to handle push notifications.
For example, we also need to ask for permission,
if we want to get push notifications.
So that permission,
which we're getting here for receiving notifications,
that will in the end enabled
both local and push notifications you could say.
So that is code we absolutely need on iOS only,
but they are we need it.
We will also need our handlers
for handling incoming notifications when the app
is in foreground and when the app is in background,
because actually push notifications
will still cause a local notification once they arrive.
So for handling the message, once it's on our device,
our code is exactly the same.
So this does not change.
But what will change,
of course, is how we schedule a notification.
We're not going to do this with this code anymore.
Instead, I first of all wanna start with showing
you how a message could be sent
from totally outside of the device.
And then as a second step,
I will show you how our app here,
could send a notification to itself,
but to itself or running on a different device
on a different user.
That would be the scenario
you have in a chat app for example.
That your app wants to send a notification to, well,
the same app, but the same app on a different device,
the device of the user to whom you just send a chat message.
That will be the second step.
First of all, I'll show you how to send
a notification totally from outside the app.
But for that one important step is missing.
And that's that identification with Google's
and Apple's push servers, because as I mentioned,
we will need to do that.
And those servers will then give us this token, this ID,
which we ultimately need to send push notifications.
And that's therefore code we have to add here.
Besides asking for permission here,
if we plan on receiving push notifications,
we also need to sign this app installation up
with those official push servers.
So this app running on a device of our user,
needs to register itself with those official push servers.
And for that, we need to add some code.
Now we will need to make some changes here,
to our permissions though
or not to the permissions we'll still need those.
But after we have those permissions
will now need to get that token, that ID.
We'll need to sign our app up
with those official push notification servers
and get that ID which then allows us
to use the ID to push notifications
to this app installation on this device.
Now, thankfully, Expo makes that super, super easy.
It makes it very simple.
Because the team behind Expo,
basically did not just give us
all these nice JavaScript functions
and API's for building React Native apps
in a convenient way,
they also have their own backend server,
to which you can talk through some functions,
where they have all the logic
for signing up an app with those official push servers
and getting that permission.
Actually, to be precise,
Expo does not sign our app up with those services.
Instead, Expo signed itself up with those services.
And it allows us to use its sign up you could say.
So Expo, the team and the software
does all the heavy lifting behind the scenes for us,
so that we don't manually need to sign up our app,
but we can leverage Expo's existing server
to push notifications through Expo's subscription
with those official servers
to our different app installations, users and devices.
That is how you can think about that.
And that will make implementing
push notifications with Expo very, very easy.
I can tell you that if you would need to sign up manually,
that would be a lot of steps, a lot of work.
And Expo is handling this for you in a very,
very elegant way.
Expo really makes it easy to deliver
push notifications to your applications.
What do you need to do?
Well, after we got our permissions here,
in the next then block,
we want to tell Expo and their own server
which they have for us,
to sign our app up with those official push servers.
We'll need to tell Expo to basically sign
our app up so to say.
Behind the scenes Expo will do all of that,
behind the scenes Expo will sign up,
and then our app will leverage Expo
to deliver push notifications.
Now for that, we need to ensure that we only reach
this then block if we do have an active permission.
Therefore here, if the permission was not granted,
we don't really wanna return,
because then the next then block would still be triggered,
even though we don't have permission.
Instead, here I wanna throw a new error.
Permission not granted.
With this we'll ensure that this code
only runs if we do have permissions.
You will see I now get a warning
that permissions were not granted,
even though we did grant permissions before here.
The reason for that simply can be seen if we console log,
this status object here.
If we do that, we see indeed,
I do have a status of granted here.
But before that we have a status of undetermined.
Which simply means it's still figuring out
whether we have permissions or not.
That is okay because ultimately
it should see that we do have them.
So we can simply get rid of the warning
by adding a catch block here.
And then here for the moment, I'll not return anything.
But with that we no longer have an unhandled
promise rejection and therefore this warning goes away.
Again, getting the permission should work.
Now with those permissions out of the way,
let's see how we can get this push token
which we need to deliver messages.