Hey, gang!
Did you know you can send notifications
to iOS devices using Google Cloud Messaging?
Well, you can.
Why would you ever want to do that?
Maybe that's a better question.
Let's find out the answer on this episode of Route 85.
[MUSIC PLAYING]
So notifications, they are a great way for you
to engage with your users.
They let your customers know you have important, new information
for them.
And when used responsibly, they can be a great way
to keep users coming back to your app.
But they're not super fun to implement.
There's a lot of steps required to set up notifications
in the first place.
You need logic on both the client and the server.
And if you're developing a cross-platform mobile app--
and most of you are these days-- you
have to do this for Android and for iOS.
And I'm not just talking about two sets of client logic
either.
It turns out sending notifications to iOS
and Android devices requires different logic on the server,
too.
See if you've done any notifications work in the past,
you're probably used to talking to APNS-- that's the Apple Push
Notification Service-- to deliver notifications
to iOS devices and to GCM-- that's
Google Cloud Messaging-- to deliver notifications
to Android devices.
And while sending notifications through these two services
is similar, they each have slightly different features,
use different protocols, accept different message payloads,
and return different responses, all of which
means that you've got to keep track of what kind of device
each of your users has and use two completely different code
paths to send a notification.
Or do you?
Well, no.
No, you don't.
You see one pretty great feature about Google Cloud Messaging
that a lot of people don't know about
is that GCM can relay to APNS any notifications you
want to send to an iOS device.
Now granted, you'll need to do some setup work, like upload
your APNS certificate to GCM and make
sure your client sends its device token to the GCM
service.
But once you've done that, you can
use GCM to send all of your notifications
no matter what platform your target device is.
And GCM will deliver your notifications
to the correct device using the appropriate service.
Well, this means that you don't need
to care about what device your user has anymore.
You just have to write and maintain one code path.
And as we all know, less code means less room for mistakes.
But it's not just about using less code.
By using GCM to handle your messaging for you,
you can take advantage of some of the other nice features
that GCM offers to developers, like topics.
Topics allow your app to subscribe to notifications
about any particular topic that you or your users want to.
For example, let's say you've got a weather app.
And I, as a loyal weather fan, want
to be notified whenever there's extreme weather happening
in my zip code.
Well, in the old way of doing this,
you'd probably need to set up a database where
you keep track of each one of your users
and their devices and their zip codes
and do this whole select users where blah, blah, blah, query.
Then, loop through the results and send notifications
to each device that you get back from this database query.
But with topics, none of that's necessary.
Instead, your app simply tells GCM
that you're interested in subscribing to, say,
the WEATHER_94043 topic.
Then, next time there's rain in California--
for us that counts as extreme weather--
Oh my gosh!
There's something coming down from the sky.
I don't know if it's water or if it's acid.
I can't go out.
I don't know how to drive anymore.
Yeah, that seems about right.
So, yeah, with topics, your server simply
tells GCM to send notifications to all devices subscribed
to the WEATHER_94043 topic.
And I will get notified along with all other devices
subscribed to that topic.
So there's no database required.
Go ahead and throw it out.
Oh, as long as you weren't using it for anything else, I guess.
I probably should have mentioned that earlier.
GCM has other useful features, too,
like upstream messaging, which allows your app to communicate
to your server through GCM.
This can be helpful in cases where
you might want some lightweight communication from your clients
to your server, but don't feel like dealing
with the hassle of setting up and maintaining
a full blown server open to the entire world.
Or read receipts, where in some, but not all situations,
you can be notified that a user has received
your message, something you can't normally
do through APNS alone.
Oh, and in case you're wondering,
all this is free, as in please send us $0.
And it's using much of the same infrastructure
that Google uses for its own apps.
So it'll probably scale for yours.
So there's a lot to learn when it comes to notifications,
and I encourage you to get started here
with our Google Cloud Messaging Documentation for iOS.
We also have a couple of sample applications for you
to look at.
There's Friendly Ping, our cross-platform chat app powered
entirely through Google Cloud Messaging,
as well as the GCMPlayground, which
lets you easily experiment with sending calls
through the GCM service.
And keep watching Route 85.
Maybe you'll see another Google Cloud Messaging
video pop up in the future.
If only we had some way of letting you
know when that happened.
Well, I'm stumped.
[MUSIC PLAYING]