So now we know how to trigger, display, and handle
local notifications.
This is an important prerequisite
because with push notifications,
we are going to trigger local notifications,
but that's the difference,
the trigger is not coming from inside our app
like it currently does,
keep in mind, that currently in the end,
we trigger our local notification here
with the trigger notification handler
with the schedule notification async method.
That's how we currently trigger the notification.
And that will no longer be the case.
Instead now with push notifications,
the trigger will be outside of our application.
It could be the developer of the app
manually sending push notifications.
It could be server side code
that leads to a push notification being sent.
For example, when a new chat message is stored in a database
and therefore now,
we will need to learn how we can send push notifications
to other devices and not just to our own device.
For that, we need to understand how push notifications work.
We got our app, and typically of course,
it runs on multiple devices for multiple users.
Then, we got some event
that should lead to a push notification to be delivered.
That could be a chat message that's being posted,
but it could also be that we, as the owner of the app,
decide to send a marketing message
or anything like that to our users.
So we want to deliver our message or our notification
to a device,
but this is not how it works.
We can't directly send messages to devices.
That's a security mechanism
because if anyone could just start sending
push notifications to random devices out there,
our phones would get spammed.
So instead, to deliver push notifications to our apps
on our devices,
we have to use official push notification servers.
And both Android, as well as iOS, so Google and Apple,
have their own push notification servers.
And you have to use those to deliver your message
to the devices.
Why?
Because those servers will only deliver messages to devices
and app installations
that identified themselves.
Essentially, your app will have to identify itself
with Google's and Apple's servers,
it will get a unique token,
an ID you could say,
and only that ID can then later be used
to deliver a push notification
through those official push servers
to your app installations,
because then the official servers can verify
that your app opted into getting those push notifications.
That's the security mechanism here.
And therefore, instead of directly sending messages
to devices,
we, in the end, use that event
in conjunction with those official servers
to send our push notification
through those official push servers
to the different devices we want to target.
That's how push notifications work.
So we have that extra security step in between,
which matters.
Now let's see how we can implement that in code
and how, again, Expo helps us a lot with that.