Menu

Expand
Rate this page:

Push Notifications on Android

Your end users can get push notifications when another participant in the conversation sends a message, joins the conversation, or leaves the conversation. You can configure which of these events send push notifications, as well as the message template used and any sound that plays.

Twilio uses the Firebase Cloud Messaging (FCM) service to send push notifications, and you will need to set your Android app up to use push notifications if you have not done so already. You'll also need to share an API key for FCM with Twilio so that push notifications can be sent to your application.

Let's get started!

Step 1 - Enable push notifications for your Service instance

IMPORTANT: The default enabled flag for new Service instances for all Push Notifications is false. This means that Push will be disabled until you explicitly enable it. Follow this guide to do so.

Step 2 - Create a Configuration File

The Firebase Cloud Messaging (FCM) library looks for a file named google-services.json in your Android app to identify push configuration details. Google provides an easy-to-use web interface for generating this file that you can find in the Firebase Console.

Copy the google-services.json file you download in the step below into the app/ directory of your Android Studio project.

Config File Generation

Once you've entered your app credentials, you can download the generated file to your desktop. Save the API Key that is displayed on the last page, as we're going to use it later on.

Step 3 - Set up your Android app with Firebase

As the version numbers for the Firebase libraries are always changing, please refer to the Add Firebase to your Android project documentation guide for setup instructions. You can add Firebase manually to Gradle, or use the Firebase Assistant in the Android Studio IDE.

Step 4 - Add Firebase Cloud Messaging to your Android application

Adding Firebase Cloud Messaging is described in the Set up a Firebase Cloud Messaging client app on Android guide on the Firebase site. Be sure to add the com.google.firebase:firebase-messaging library to your dependencies.

Be sure to follow the steps to modify the app's AndroidManifest.xml file, and add the Java or Kotlin code to Access the device token. You will need to send that device token to Twilio, which we describe in a later step of this guide.

As a quick check at this point, you can send a push notification through Firebase Cloud Messaging to your app using the Firebase Web Console. Verify that you have Firebase Cloud Messaging working correctly with your server, and that you can retrieve a device token, before proceeding with the Twilio integration steps in this guide.

Step 5 - Upload your API Key to Twilio

Now that we have our app configured to receive push notifications let's upload our API Key by creating a Credential resource. Visit the Credentials Creation page to generate a credential SID using the API key.

API Key

Keep that credential SID handy for the next step.

Step 6 - Pass the API Credential Sid in your Access Token

For this step, you will be modifying your server application to add the credential SID from the previous step.

Your Chat Android client Access Token needs to include the correct credential_sid - the one you created in Step 5 above. Each of the Twilio Helper Libraries enables you to add the push_credential_sid. Please see the relevant documentation for your preferred Helper Library for the details. Here is an example using the Node.js Twilio helper Library:

var chatGrant = new ChatGrant({
    serviceSid: ChatServiceSid,
    pushCredentialSid: FCM_Credential_Sid,
});

Step 7 - Use the Registration API in the Twilio ConversationsClient

You will need to call the ConversationsClient API methods -registerFCMToken and unregisterFCMToken to send the individual Android device's FCM token to Twilio, so that Twilio can send push notifications to the right device. See the Twilio Conversations Android SDK documentation for details.

Nice! That's all we need to make sure the Conversations Client can use Firebase Cloud Messaging to send push notifications!

Rate this page:

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 by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.

        
        
        

        Thank you for your feedback!

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

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more