Quickstart guide to Google Cloud Messaging for iOS

Quickstart guide to Google Cloud Messaging for iOS

There are a lot of moving parts to GCM so hopefully this quickstart guide will be useful to other developers. This post is intended to step through the parts of the process that are not described fully in the Google tutorial.

Benefits

There are plenty of good reasons to choose GCM over raw Apple notifications:

  • You can post notifications to Android and iOS devices with the same system.
  • You can (with a bit of fiddling) use a cross-platform tool like Unity to develop the app with minimal platform-specific code.
  • Google handle the communication with the Apple Push Notification Services servers for you, which is tricky to say the least...

Before we get started...

You will need:

  • An Apple Developer account and access to the certificates page
  • Your iOS app's bundle ID (found in your XCode project target General settings). It's normally something like uk.co.mycompany.myapp.
  • A server to keep track of the device registrations and hit GCM servers to post the notifications

Apple Push Notification Service

Before I explain how GCM works, it's going to help to understand a bit about APNS. When you launch the app it requests the push service from the OS. If this is the first time, and the user has notifications globally enabled, then the OS will ask the user if they want to receive notifications. If they agree, the app will fetch a security token from Apple, and you'll get this in a callback.

This token is what you need to send the notification to the correct device, so in that callback you need to hit your own server and store the device's ID and token.

Now you have a table on your server full of devices and tokens, the final thing you need is certificates from Apple that are specific for your bundle ID. Then you can hit the APNS server to send devices notifications.

GCM

GCM is similar, but crucially it allows you to treat Android and iOS devices in the same way. Google will handle the APNS connection too. You'll need to:

  • Register your app with Google and give them your certificates
  • In the callback from iOS, send the registration token to Google. They will give you a new token back.
  • Register the new token on your server alongside the device ID.

Now your server can hit GCM to send notifications.

Let's do it...

Now I'll take you through these steps in more detail:

Register your app

The tricky bit here is getting the certificates right:

  • Log into your Apple developer account and go to Certificates, Identifiers and Profiles
  • If you don't already have one, create an App ID with the correct (explicit) bundle ID and make sure the Push Notifications service is checked
  • Create two certificates for this app ID - Follow the instructions carefully to sign them with your KeyChain.
    • A development certificate: Apple Push Notification service SSL (Sandbox)
    • A production certificate: Apple Push Notification service SSL (Sandbox & Production)
  • Download both certificates, and open them in KeyChain. Right click on the certificate (not the key inside!) and export them as development.p12 and production.p12
  • Log into you Google developer account and register your app
  • When prompted upload your two .p12 certificates
  • If all went well, download your GoogleService-Info.plist. You will need this in the next step.
  • Make a note of the API key too. The server will use this to authenticate with GCM.

Configure Xcode

Surprisingly, that's the hard bit done, now we just need to write some code. If you don't have CocoaPods you'll need it to import the libraries:

In your project directory:

sudo gem install cocoapods

pod init

Then add the line pod 'Google/CloudMessaging' to the Podfile and:

pod install

From now on open your .xcworkspace file, not your normal .xcodeproject.

  • Drag the GoogleService-Info.plist into your project and add to all targets.
  • In your AppDelegate.m, add the callbacks as per Obtain a Registration Token.
  • In your _registrationHandler block, call through to your server with your device ID and the GCM token.

Your server

Whatever server architecture or framework you are using you will need:

  • A database with a table of device IDs and tokens
  • An endpoint for registration that can be called by the Android or iOS client apps
  • Some code to hit the GCM server with your API key (you got this when you registered the app) and the device token(s).
  • An endpoint to test notifications - ideally a simple web form where you can enter a device ID and a message.

More information

Apple Push Notification Service

Google Cloud Messaging

Setting up a GCM Client App on iOS

Try Google Cloud Messaging for iOS

I hope this helps! Good luck!

Portions of this page are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

James Uren
in iOS Tagged iOS Google Cloud Messaging GCM

Airsource design and develop apps for ourselves and for our clients. We help people like you to turn concepts into reality, taking ideas from initial design, development and ongoing maintenance and support.

Contact us today to find out how we can help you build and maintain your app.