Use our iOS sample app to see how Google Cloud Messaging works, or add Google Cloud Messaging to your existing app.
Get the project
Start with the sample using the following command from a terminal:
pod try Google
Select the option for
GcmExample.xcodeproj from the prompt.
Get a configuration file
To use the sample, you need to provide a valid APNs certificate and some additional information to get a configuration file and finish setting up your project. If you don't already have an APNs certificate, see Provisioning APNs SSL Certificates. When prompted, provide the Bundle ID associated with your APNs certificate.
After you complete the configuration process, take note of the API server key on the
download page, you will need it later. Download the
file to add to your project.
Add the configuration file to your project
GoogleService-Info.plist file you just
downloaded into the root of your Xcode project and add it to all targets.
Run the sample
Now you're ready to build the sample app and run it from Xcode.
Select the GcmExample target then start the sample application on an attached device by clicking the run button for the workspace. You must use a physical device for this application to work. The iOS simulator does not support remote notifications.
The app registers with the Google Cloud Messaging service and prints the registration token to the Xcode debug console when finished.
Next, run the GcmServerDemo target. In the dialog, insert the provided API server key, as well as the registration token that the GcmExample app has printed in the Xcode debug console.
Once you have provided credentials to the dialog, you can select Send notification to send a notification to your iOS device, or Display cURL command to print a cURL command that you can use to send the same request to the GCM connection server. You can also select Send to topic to send a message to a topic instead of a direct message to the device. You don't need to add the registration token to use this option, the server key is enough.
If the GCMExample application is running in the foreground, you can see the content of the notification printed to the Xcode debug console; if the GCMExample app is in the background, you will receive an APNS notification.
How it works
Simply get the registration token using the GMS Instance ID API, and post the notification.
To receive the notification, implement
application:didReceiveRemoteNotification: for iOS < 8.0),
GCMService:appDidReceiveMessage:message to acknowledge the reception of the message to the GCM server.
If you want to dive into the details, take a look at our iOS client implementation guide.
Did you have a good experience? Run into trouble? Let us know!