Android Quickstart GCM
In this guide, we will get you up and running quickly with a sample application you can build on as you learn more about Notifications. Sounds like a plan? Then let's get cracking!
Table Of Contents
Gather account information
The first thing we need to do is grab all the necessary information from our Twilio account. To set up your back-end for Notifications, you will need four values from your account:
Config Value | Description |
---|---|
Credential SID | Identifier for your GCM push credentials stored at Twilio - you can upload your credentials here. If you do not yet have one, you can provision one following this guide. |
Service Instance SID | A service instance where all the data for our application is stored and scoped. You can create one in the console. Make sure you configure it with your Credential SID from above. |
Account SID | Used to authenticate REST API requests - find it in the console here. |
Auth Token | Used to authenticate REST API requests - like the Account SID, find it in the console here. |
Set up the server app
When using the Notifications service, you'll need to be able to register devices to receive notifications, and then send notifications to those devices. To get you going quickly, we've created a Node.js server that accomplishes these tasks. You will be communicating with this server-side application from your Android application to register a device for notifications.
You can browse the source code on GitHub as well. Want to use a server written in another language? Try out our PHP, Python, Ruby, or Java quick start notification servers.
Unzip the app you just downloaded, and navigate to that folder in a Terminal window on
your Mac. You'll need to install Node.js if you haven't already, you can do that
here. Now, we just need to configure and run
the app. In the terminal, edit the config.json
file to include the four configuration
parameters we gathered from above.
Now we'll want to install our dependencies:
npm install
Once we've done that we're ready to start the server - again in your terminal, run:
npm start
To confirm the server is running, visit http://localhost:3000 in a web browser. You should see a brief message indicating the server is running.
Now that our server is set up, let's get the starter Android app up and running.
Download and explore the mobile app
To get going quickly, there is a starter app built as a stand-alone Android Studio project written in Java. Download it now:
The application is built using the Gradle automation system and is available on GitHub. Open Android Studio and import the project by double clicking on it's build.gradle file.
Use the Google Services tool to generate a 'google-services.json' file for your app. If you have your Android app already setup, select that, if you are using the app downloaded above use the following values.
App name: testapp Android package name: com.testapp
This file configures your app to receive notifications via GCM. Then, hit the
compile button and it will build your gradle dependencies automatically. Next,
in the RegistrationIntentService
file on this line:
private static final String host = "myserver.com";
replace myserver.com with the address of your server. Do not include "http://" in the address, the app adds that automatically.
Once that's done, your app should be fully configured and ready to rock.
Important Note About Running on a Device vs a Simulator
Push Notifications work on both the Android simulator and device. To test on a
device, you'll need to replace the URL in RegistrationIntentService
with a public URL for your server. Consider checking out
ngrok to expose your local server on the public
Internet.
Once the app loads, you can enter an Identity (e.g. Bob) and the identifier of the specific endpoint of that Identity (e.g. Bob's Android phone). Then click "Create Binding" to register your device-specific address with Twilio. Creating a Binding lets our service know where to send notifications. It also associates the device-specific address with the Identity (e.g. Bob) so that you can send notifications referencing that Identity. Awesome, your device is now registered for push notifications.
Now you can send your first notification. Run the notify.js
script in the server app
you just downloaded, passing in the identity for the device you just registered.
node notify.js YOUR_IDENTITY
You can find your notification in the Notification Center (just swipe down from the top of the screen on your Android device or simulator).
You're all set! From here, you can start building your own application. For more information on the Notification API, check out our REST API docs.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.