Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Registering for Notifications on Android with GCM

Ready to send your first notification? Of course you're not! That's why you're here. But don't worry, we'll get you registered so you can start sending notifications faster than a collision-free hash table lookup (OK, maybe not that fast).

A Notifications application involves two components:

  • A client (Android) app that registers for and receives notifications
  • A server app that creates bindings and sends notifications

The aim of this guide is to show you how these two components work together when you register for notifications. Let's get to it!


Client: Registering a Device with GCM

Before working with Twilio Notifications, we'll need to register our device with GCM. It's best to implement registration in a dedicated service (e.g RegistrationIntentService) and not an activity since it can take a while to receive the registration token. This service is typically invoked from either a login flow, or onTokenRefresh (since GCM may assign a new token at anytime).


        The onTokenRefresh callback in MyInstanceIDListenerService will fire if the registration token changes. Make sure you've properly configured your Android app for notifications or else the registration won't work.

        Client + Server: Creating a Binding


        Once we've registered our device with GCM, it's time to create a Binding. A “Binding” represents a unique device that can receive a notification. It associates a unique device ID (provided by Android) with an identity and an optional set of tags that you define for your application. When you want to send a notification, Twilio will send your notification only to Bindings that match the parameters you specify (more on that later).

        クライアントAppは、サーバーがバインディングの作成時に使用する必要性が考えられるデバイスIDおよび追加情報を含むリクエストを送信すべきです。 これはBindingResourceクラスで行われます。


              Since the response to getToken is synchronous and we're already in a dedicated service, it's safe to do the HTTP request from our intent service. In this example, we'll use the retrofit2 library but any HTTP client will do.


                    The BindingRegistration class is a simple Plain Old Java Object (POJO) that the retrofit2 API will translate to JSON for us. It wraps the parameters we need to send to the server.


                          We'll also need an intent-filter to help declare the capabilities of MyInstanceIDService.



                                In our server app, we’ll receive the POST request. It’ll contain the following four parameters.

                                name description
                                Identity The Identity to which this Binding belongs. Identity is defined by your application and can have multiple endpoints.
                                Endpoint The identifier of the device to which this registration belongs. Endpoints are also defined by your application.
                                BindingType The type of the Binding determining the transport technology to use. Allowed values: apn, gcm.
                                住所 The address obtained from the vendor. For APNS it is the device token. For GCM it is the registration token.

                                We'll use index.js in our server app and these four parameters to send an API request to Twilio. We will use the next generation Twilio Node Helper Library. Then, we'll send a success message or an error back to the mobile app. We implement this logic in the /register endpoint, but this is not required. You can come up with your own API, perhaps integrate it into a login or a session initialization flow.


                                      Now that we've registered the client app with GCM and created a Binding, it's time to send some notifications! Check out our Sending Notifications guide for more information.

                                      Next: Sending Notifications »

                                      Rate this page:


                                      誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。