so you probably already know that
Firebase Cloud messaging not only sends
messages to Android devices but it can
also route notifications through APNs
Apple's push notification service to
talk to iOS devices but I think it's
worth explaining how it does this partly
because it's kind of interesting but
more importantly because it can give you
an understanding of what to look for if
things go wrong let's explore further on
this episode of
firecasts so two things are needed for
any service to communicate with APNs
first you need a certificate which
basically allows you to communicate with
the APNs servers in the first place
these days I know you can also use
something known as an APNs off key which
is a little more convenient to work with
if you're writing your own communication
code but I'm going to focus on the
certificate for now because this is what
Firebase Cloud messaging currently uses
uh second you'll need to tell APNs what
device you want to send a notification
to and for that you need a device token
it's essentially a way to uniquely
identify this user's device using an
identifier that only works with your app
so this device token is something that's
typically requested from APNs by the
device and then sent down to your server
since Firebase Cloud messaging is taking
the role of the server here it needs
these two things the certificate and the
device token so one of the first things
you do when you set up Firebase Cloud
messaging is to upload your APNs
certificate in the form of a p12 file to
the Firebase console and this allows the
fcm server to talk to APNs then when
your app starts up your user's device
will request a device token from APNs it
it will then send that token down to
Firebase Cloud messaging and then
Firebase Cloud messaging will create and
send back a copy of its own device token
in exchange so at this point fcm has
everything it needs to communicate to
your device using APNs and now you're
ready to send off your request through
Firebase Cloud messaging so just like
with APNs you need two things to send a
notification request through fcm a way
to communicate with the service and a
way to identify your target device so
first off instead of a certificate
you're going to use an API key which you
can think of as the secret password you
need to talk to the fcm server on behalf
of your app and you can get this from
the Firebase console next assuming you
want to talk to individual devices
you'll also need the fcm token which
you'll get from your client these two
pieces of information are then packaged
up into a notification that you send off
to Firebase Cloud messaging now Firebase
will look at whatever entry it has for
the device with a matching fcm device
token and it will see that it's
associated with an APNs token so it says
Ah I probably need to Route this message
through APNs to talk to this device so
what Firebase will do is it will create
a new APNs version of this notification
using the certificate and the token that
it has stored on its side this new
notification will then get sent to APNs
which will then notify our iOS device
and your notification will finally show
up hooray so I know that's kind of a lot
to process right but if there are two
things I want you to take away from this
explanation it's this first most of this
work is being done behind the scenes for
you most of the time you don't need to
know or care about any of it second is
that by the time we get to here this is
just a regular old APNs notification as
far as your app is concerned this
notification looks like any other
notification it might have received from
Apple so let's stop and think about the
work our device had to do during this
process we had to one ask APNs for a
device token two send that APNs token
off to fcm and then three receive back
the fcm device token well getting an
APNs device token is accomplished by
calling register for remote
notifications the way you normally would
as for these other steps you don't
really need to do any work to make this
happen as long as your app has Firebase
enabled we do most of the work for you
thanks to some fancy method swizzling if
you're new to the concept of method
swizzling it's basically a way
developers can add on functionality to
pre-existing methods in iOS by calling a
custom version of that method which
usually then calls the original method
when it's done now often developers use
these to customize methods that are part
of the standard iOS library but in our
case we're using them on top of any
delegate methods you might have written
so in our Swizzle did register for
remote notifications call we receive the
APNs token and send it off to Firebase
Cloud messaging in exchange for an fcm
device token uh by the way I've actually
been lying to you a little bit this
token is really known as an instance ID
token which in theory could be used to
identify your device for services other
than just Firebase Cloud messaging and
that's why you'll see it created by the
instance ID library in our code and why
it's sometimes called your registration
token in the docs but for the purposes
of understanding fcm I think you're
better off just calling this an fcm
device token because this is basically
what it is I should also point out that
we Swizzle some of the methods for
receiving notifications as well and what
we're primarily doing here is making
sure that we're recording analytics
information about these
notifications this is how we can give
you some of those nice hey 30% of these
notifications were open reports that you
see in the notifications panel and if
you're uncomfortable with the idea of
having somebody else Swizzle your
methods you're more than welcome to turn
this off you'll just have to add in the
code manually to do the work that we're
doing and we've got documentation for
that finally one last note with these
swizzled methods is that we don't
automatically call anything that would
bring up that standard iOS dialogue
asking the user if it can show any user
visible notifications we know that this
is something you probably don't want to
call until you're pretty sure your users
are actually going to say yes so this
method will still be up to you to call
at the moment that makes the most sense
for your app of course now that I've
told you that this is nothing more than
a plain old APNs notification and you
don't need to do anything special to use
Firebase Cloud messaging you might
notice this section in our doation where
we ask you to explicitly connect to
Firebase Cloud messaging so what's that
all about well you see in addition to
being able to communicate to your device
through APNs you can also connect
through Firebase Cloud messaging to
receive notifications directly through
that service but there are some
restrictions here first and foremost
this only works if your app is in the
foreground second this only works for
data only messages nothing with an alert
message badge sound anything like that
it also doesn't work with content
available messages those will always go
through through APNs in fact in APNs
land I don't think you're allowed to
send a silent notification that doesn't
include content available so this type
of message is only valid through
Firebase Cloud messaging as as far as I
can tell so if you send this type of
notification and your app isn't in the
foreground and hasn't explicitly
connected to fcm it won't receive this
message until you reconnect so of course
the question you might have is why would
you ever want to do this so
communicating directly through Firebase
Cloud messaging does have some
advantages for one thing fcm tends to be
more aggressive about getting these data
messages to their target device uh for
another there's just one fewer hop for
your message to take which is always
nice and also your device could actually
send back Upstream messages through fcm
to let you know for instance that this
message was received but because they
only work in the foreground their use
case is kind of limited to apps that are
generally okay with not receiving these
notifications in the background one good
example YouTube their IOS app uses the
fcm connection to deliver room chat for
its YouTube live events since the app
really only cares about getting this
Avalanche of chat messages when it's in
the foreground it's a pretty good use
case for Firebase Cloud messaging but if
you don't think your app is going to
need these types of messages I might
recommend not connecting to the fcm
service directly at least until you've
gotten the rest of Firebase Cloud
messaging working it's just one extra
factor to deal with when you're trying
to track down issues with your APNs
implementation so there you go that's
how fcm works on iOS devices in an
overly simplified nutshell hopefully
this gives you a little more insight
into what's happening behind the scenes
and uh if you found this video helpful
be sure to subscribe and we will see you
soon on another episode of
firecasts thank you very much for
watching want some more exciting
Firebase content well check out these
videos
here jazz
hands