TODD KERPELMAN: So in a previous video,
you learned that you can use GCM to send notifications
through APNs to iOS devices.
But to many developers, APNs is a bit of a black box.
And come to think of it, so is GCM.
Put them together and you've got a black rectangle, I guess.
Well personally, I find having a little background
on how things work underneath the hood
can help out a lot when stuff goes wrong.
So let's shine a light on half of this big old rectangle
on this episode of "Route 85."
[MUSIC PLAYING]
So APNs is a service that sends notifications to iOS devices,
whether the target app is in the foreground
or in the background.
So how does it work?
Well for starters, you need two important configuration bits
to make this happen.
First off-- the device token.
You can basically think of this as the address
of the device you want to talk to you,
but one that's unique for your app.
It's also not really the address of the device.
You know how when rock stars check into a hotel,
they use a fake name?
Well, the device token is kind of like having that fake name.
It lets the hotel manager know that, hey, it's cool for you
to visit your rock star friend, because you know the alias
that they're checked in under.
Is that Matt and Kim?
It kind of looks like them, right?
Yeah.
So when your app gets permission from your user
to receive notifications, iOS talks to APNs and requests
a device token-- basically a fake rock star name-- which
is then given to your app.
Now if you were just building an iOS app using nothing but APNs,
you'd need to pass this token back to your server somehow.
We'll get into what GCM does at a later point.
When your server passes this device token back to APNs,
it's your way of both identifying
the device you want to talk to, as well as proving to APNs
that you're allowed to notify that device in the first place.
The next bit you need is the APNs certificate.
This basically allows your server
to talk to APNs securely.
And hang on, we've got some massively over-simplified
explanations coming your way.
One common way for two devices to talk to each other securely
is to use public and private key encryption.
Alice encrypts messages using Bob's public key, which
only he can decode on his side using his private key,
and then vice versa.
So let's imagine your server is Alice in this scenario.
And if you want to talk back and forth to APNs,
you're going to need your own private key and APNs'
public key.
In some cases, like the openssl tool
that you can run from the command line,
you pass these two keys in separately.
But for many third party tools, including GCM, these two keys
are combined together, along with some other information,
into a p12 file that we colloquially refer to
as the APNs certificate.
So with all that background, let's look
at what happens when we send off a notification via APNs
using, say, a little server that you might have.
For starters, we'll use that certificate
we just talked about, containing your private key and APNs'
public key, so that you can talk securely to the APNs service.
Yes, I know APNs service is redundant.
Shut up.
Now when you send off a notification,
you're going to need to include things
like the notification message, badge count,
or any associated data.
And you're going to send all of that
along with the device token.
Now when APNs receives that token,
it's going to do some verification.
It'll make sure it's a valid token, it hasn't been revoked,
and that it actually belongs to your app.
At this point, if everything looks good,
it will go ahead and schedule the delivery
of that notification at some point in the future.
When does it actually deliver it?
Honestly, that's kind of up to Apple.
And they're not super explicit about the logic they use.
There are a lot of different factors involved though.
But you can influence this by labeling your messages
as high priority, which has a priority
value of 10 here, or a normal priority, with a value of 5.
A high priority message would be for something
where the user might get upset if they don't
receive their message on time.
A chat message or a calendar reminder
is a good example here.
A normal priority message is for something less time critical.
APNs will often group these messages
and send them all at once as a way
to conserve the user's battery.
And they may even get throttled and not send at all.
But still, it's a good way of respecting your user's battery.
So if it's not an urgent notification,
consider sending it normal priority.
Now, what happens when your device
receives the notification?
It depends a lot on what's going on with your app at the time.
If your app is in the foreground,
iOS will simply hand the notification off to your app.
It won't show any kind of display at the system level.
That's completely left up for your app to deal with.
If your app is in the background,
iOS will commonly show a notification-- usually
as a banner up on top here, but it depends on your settings.
And it will probably be listed in the Notifications panel
as well.
If the user selects that notification,
your app will come back into the foreground.
And at that point, your app has a chance
to handle the notification.
Now, there is one other attribute of notifications
you should be aware of.
And that is the content-available flag.
The purpose of this flag is to tell iOS
that this would be a good time for your app
to do a little background work.
For example, you might want to tell your news app that you
have some breaking news available,
and your app should go and download and refresh
the day's top headlines.
Now, these notifications always need
to be sent as normal priority.
And developers typically send these as silent notifications.
That is, they're usually sent without
an associated alert message or sound,
so the user doesn't notice them.
When iOS gets these kind of content-available messages,
it will pass the notification along to your app.
And your app will have up to 30 seconds--
but it might be a little less, depending on certain factors--
to do whatever background work it needs to do, and then let
iOS know that it's finished.
Now, how you actually handle these notifications or what
methods you use are all very good topics
that I'm going to leave to another video.
Honestly, you're troopers for sticking out with me this far.
So there you go.
That's APNs in an overly-simplified nutshell.
But wait, this is a video series on GCM, right?
How does GCM fit in this whole scenario?
Well, we will take a look at the second half
of our black rectangle in a future video.
In the meantime, feel free to check out our documentation.
And we will see you soon on "Route 85."
[MUSIC PLAYING]