This Quickstart demonstrates how to send Push Notifications to an Android application using Notify and Twilio Functions.
Note: Google Cloud Messaging (GCM) is deprecated, and we no longer maintain it. Please use Firebase Cloud Messaging (FCM) to send Android Push Notifications.
In the console, create a Notify service. Make note of the SID! You will use this later when you start writing code further down.
We need to do is grab all the necessary information from our Twilio account. Here's what we'll need
|Service Instance SID||A service instance where all the data for our application is stored and scoped. You can create one in the console.|
You will also need to create a push credential on the Twilio Console, and then configure it on your Notify service. You can upload your credentials here. If you do not yet have one, you can provision one following this guide.
Functions is Twilio's serverless code execution environment that lets you write Node.js code that can respond to Twilio webhooks or to regular HTTP POST or GET requests.
You can also always write your own server code using any of Twilio's supported server side languages (Node.js, Python, Ruby, C#, Java, or PHP) - check out how to register bindings or how to send a notification in those languages.
The sample mobile app is already set up to communicate with Twilio Functions to register a device for notifications. You just need to create two Functions in your account from a template, and then specify the URL for one of those Twilio Functions in the source code to the app.
To get started with this, create a new Twilio Function on the Twilio Console's Manage Functions page. Choose the Twilio Notify Quickstart template from the list of templates.
After choosing the Twilio Notify Quickstart template, you'll be asked for one piece of configuration information - the Notification Service instance SID that you created at the beginning of this quickstart.
After entering that SID as the value, and clicking create, you will see two new Twilio Functions on your Functions page.
The first function is Register Binding - available at the /register-binding path. The quickstart mobile app sends an identity and the device token that it gets from the Firebase Cloud Messaging service to this function to create a binding. Notify uses these bindings to map the device that someone is using to their identity when you send a notification.
The second function is Send Notification - this is available at the /send-notification path. The mobile app does not use this function - instead you can use it to send notifications to yourself on the quickstart app.
If you select either function, you can see the Node.js source code, if you are curious to see how these are implemented.
Lastly, copy the URL for the register binding function - you will need that for the Android app. Each account has a different subdomain for Twilio Functions - for instance, yours might be something like this:
Now that you have the Twilio Functions created, it's time to set up the Android app!
To get going quickly, we provide an Android sample app built as a stand-alone Android Studio project written in Java:
Open Android Studio and import the project by double clicking on it's build.gradle file.
If not done yet, follow this guide to configure Android Push Notifications for the Twilio Notify sample Android app.
Next, go to Firebase Console to generate a 'google-services.json' file for your app.
Open your project
Go to the settings
Download google-services.json file and save it to the
/app folder. This file configures your app to receive notifications via FCM.
Compile the project and it will build your gradle dependencies automatically.
Next, in the
notifyapi/TwilioFunctionsAPI.java file on this line: private final static String BASE_SERVER_URL = "YOUR_TWILIO_FUNCTIONS_URL"; replace the string with the address of your server.
Next we need to create a Binding between user Identity and the device running the app. User Identity can be any unique identifier you choose, like GUID. You can find out more about Identity and Bindings in the Binding resource reference API
In the app enter Identity you choose and click the button Create binding.
This action creates a Binding which uniquely identifies a user on a certain device, running your application.
Now that we have the Binding, we are ready to send a Push Notification to an Android app running on a device or on the emulator.
To send a notification, you can use the
Twilio Notify Quickstart (Send Notification) Twilio Function you just created.
You can either send an HTTP GET or an HTTP POST request to the function. Typically, you would send a POST request to the function from your application (as you are performing an action, not retrieving information). If you don't have an easy way to POST a request (like Postman or curl), using GET is fine for testing your quickstart app.
For instance, you might make a request like:
Use the identity you used in the app. Because you registered a binding with Twilio, the server will send your device the 'Hello' message as a notification.
You're all set! From here, you can start building your own application.