In this guide, we'll go through all the necessary steps to get your Android push credentials configured with Twilio Notify. The end result is a properly configured Android app and a Twilio Notify Push Credential which you can then use to send notifications to your Android app. Let's get started!
Google has replaced Google Cloud Messaging (GCM) with Firebase Cloud Messaging (FCM). To use push notifications for your Android apps, you will need to create a project on the Firebase Console:
The Firebase Cloud Messaging (FCM) library requires a file called
google-services.json in your Android project's app directory to link your app with Firebase services. The Firebase console has an easy way to generate this file, but you could also use Android Studio's Firebase integration. We'll use the web based approach in this guide.
After you create a Firebase project, you can add Firebase to your Android app:
Clicking the middle link will bring up this dialog:
After filling in your Android app's package name, clicking the Add App button will automatically download the generated
You will need to put that file into the
app folder of your Android project, or Gradle won't be able to build your application after you add the Firebase libraries.
The next screen in the Firebase dialog will also explain where to put the file you just downloaded.
Android Studio uses Gradle to parse your credentials from the
google-services.json file. Your app has 2
build.gradle files, a project-level one and an app-level one.
Add this line to your project-level
build.gradle in the dependencies section:
To fully integrate your application with Firebase Cloud Messaging, you'll need to create two application-specific Android Services, and then add them to your
AndroidManifest.xml file. Check out the quickstart app for an example of each of these services.
The first service to add is a messaging service that responds to Firebase messaging events. This service must extend
com.google.firebase.messaging.FirebaseMessagingService from the Firebase Cloud Messaging library.
<service android:name="com.example.ExampleMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
Once you've done that, create a service that extends
com.google.firebase.iid.FirebaseInstanceIdService, to handle registration token refreshes
<service android:name="com.example.ExampleInstanceIDListenerService" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID" /> </intent-filter> </service>
Now that we have our app configured to receive push notifications, let's upload our FCM Secret to create a Credential that our Notify Service can use.
In Firebase, FCM Secret is called Server Key and you can find it in your Firebase console under your Project Settings and Cloud Messaging tab.
Once you have that, you will need to create a credential on the Add a Push Credential page on the Twilio Console using FCM Secret:
From the drop down, choose FCM and paste in the secret - you can also give this credential a friendly name, such as FirebasePush.
Twilio allows you to build multiple applications in a single account. To separate those applications you need to create Service instances that hold all the data and configuration for your application. On the Notify Services page of the Twilio Console, click the red plus button to create a new Notify Service:
After naming your Notify Service (for instance, with the same name as your app), you can configure the service to use one or more messaging credentials, such as the FCM Credential we created in Step 5:
Now that you have your application configured for Firebase Cloud Messaging, you are ready to write some code and send some push notifications with Twilio. Visit the Android Quickstart guide for the next step!