# Push Notifications on Android for Conversations

Your end users can get push notifications when another participant in a 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. You need to set up your Android app to use push notifications if you have not done so already. You also need to share an FCM API key with Twilio so that push notifications can be sent to your application.

## 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 notifications will be disabled until you explicitly enable them. Follow [this guide](/docs/conversations/push-notification-configuration) 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 a web interface for generating this file that you can find in the [Firebase Console](https://console.firebase.google.com/).

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

![Firebase console showing options to create or import a project with recent projects listed.](https://docs-resources.prod.twilio.com/3692b6c3762064521e113d66953c22a3ada75d8b9fcfe0802aa6e0d6a08b0db0.gif)

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 you will need it in a later step.

## 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](https://firebase.google.com/docs/android/setup) 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](https://firebase.google.com/docs/cloud-messaging/android/client) 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](https://firebase.google.com/docs/cloud-messaging/android/client#sample-register). 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 you have your app configured to receive push notifications, upload your API Key by creating a Credential resource. Visit the [Push Credentials Creation](https://console.twilio.com/us1/account/keys-credentials/push-credentials?frameUrl=/console/project/credentials/push-credentials) page to generate a FCM credential SID using the API key. You can also get to the Credentials page by clicking on the **Account** dropdown in the top left corner of the Twilio Console and then clicking on **Credentials** from the dropdown Account menu. Once on the Credentials page, click the **Push Credentials** tab.

On the Push Credentials Page, create a new Push Credential. Give the credential a name and make sure the credential's type is "FCM Push Credentials". Under "FCM Secret", paste your API Key from the end of Step 2. Then, click **Create**.

The next screen you see after creating the credential includes the new push credential's SID. Keep that credential SID handy for the next step.

## Step 6: Pass the Push Credential Sid in your Access Token

For this step, you will modify your server application to add the push credential SID from the previous step into your server's [Access Token generation](/docs/conversations/create-tokens).

Your Access Token needs to include the Push Credential SID that you got in Step 5. See below for examples in each Twilio server-side SDK of how to generate an Access Token with a ChatGrant that contains a Push Credential SID.

Creating an Access Token (Chat) with Push Credentials

```js
const AccessToken = require('twilio').jwt.AccessToken;
const ChatGrant = AccessToken.ChatGrant;

// Used when generating any kind of tokens
// To set up environmental variables, see http://twil.io/secure
const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID;
const twilioApiKey = process.env.TWILIO_API_KEY;
const twilioApiSecret = process.env.TWILIO_API_SECRET;

// Used specifically for creating Chat tokens
const serviceSid = process.env.TWILIO_CHAT_SERVICE_SID;
const pushCredentialSid = process.env.TWILIO_PUSH_CREDENTIAL_SID;
const identity = 'user@example.com';

// Create a "grant" which enables a client to use Chat as a given user,
// on a given device
const chatGrant = new ChatGrant({
  serviceSid: serviceSid,
  push_credential_sid: pushCredentialSid
});

// Create an access token which we will sign and return to the client,
// containing the grant we just created
const token = new AccessToken(
  twilioAccountSid,
  twilioApiKey,
  twilioApiSecret,
  {identity: identity}
);

token.addGrant(chatGrant);

// Serialize the token to a JWT string
console.log(token.toJwt());
```

```py
import os
from twilio.jwt.access_token import AccessToken
from twilio.jwt.access_token.grants import ChatGrant

# required for all twilio access tokens
# To set up environmental variables, see http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY']
api_secret = os.environ['TWILIO_API_KEY_SECRET']

# required for Chat grants
service_sid = 'ISxxxxxxxxxxxx'
push_credential_sid = 'CRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
identity = 'user@example.com'

# Create access token with credentials
token = AccessToken(account_sid, api_key, api_secret, identity=identity)

# Create an Chat grant and add to token
chat_grant = ChatGrant(service_sid=service_sid, push_credential_sid=push_credential_sid)
token.add_grant(chat_grant)

# Return token info as JSON
print(token.to_jwt())
```

```cs
using System;
using System.Collections.Generic;
using Twilio.Jwt.AccessToken;

class Example
{
    static void Main(string[] args)
    {
        // These values are necessary for any access token
        // To set up environmental variables, see http://twil.io/secure
        const string twilioAccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
        const string twilioApiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY");
        const string twilioApiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET");

        // These are specific to Chat
        const string serviceSid = Environment.GetEnvironmentVariable("TWILIO_SERVICE_SID");
        const string pushCredentialSid = Environment.GetEnvironmentVariable("TWILIO_PUSH_CREDENTIAL_SID");
        const string identity = "user@example.com";

        // Create an Chat grant for this token

        var grant = new ChatGrant
        {
          ServiceSid = serviceSid,
          PushCredentialSid = pushCredentialSid
        };

        var grants = new HashSet<IGrant>
        {
            { grant }
        };

        // Create an Access Token generator
        var token = new Token(
            twilioAccountSid,
            twilioApiKey,
            twilioApiSecret,
            identity,
            grants: grants);

        Console.WriteLine(token.ToJwt());
    }
}
```

```java
import com.twilio.jwt.accesstoken.AccessToken;
import com.twilio.jwt.accesstoken.ChatGrant;

public class Example {
  public static void main(String[] args) {
    // Get your Account SID from https://twilio.com/console
    // To set up environment variables, see http://twil.io/secure
    // Required for all types of tokens
    String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
    String twilioApiKey = System.getenv("TWILIO_API_KEY");
    String twilioApiSecret = System.getenv("TWILIO_API_SECRET");

    String serviceSid = System.getenv("TWILIO_SERVICE_SID");
    String pushCredentialSid = System.getenv("TWILIO_PUSH_CREDENTIAL_SID");
    String identity = "user@example.com";

    ChatGrant grant = new ChatGrant();
    grant.setServiceSid(serviceSid);
    grant.setPushCredentialSid(pushCredentialSid);

    AccessToken token = new AccessToken.Builder(twilioAccountSid, twilioApiKey, twilioApiSecret)
        .identity(identity).grant(grant).build();

    System.out.println(token.toJwt());
  }
}
```

```go
package main

import (
	"fmt"
	"os"

	"github.com/twilio/twilio-go/client/jwt"
)

func main() {
	// Get your Account SID from https://twilio.com/console
	// To set up environment variables, see http://twil.io/secure
	// Required for all types of tokens
	var twilioAccountSid string = os.Getenv("TWILIO_ACCOUNT_SID")
	var twilioApiKey string = os.Getenv("TWILIO_API_KEY")
	var twilioApiSecret string = os.Getenv("TWILIO_API_SECRET")

	params := jwt.AccessTokenParams{
		AccountSid:    twilioAccountSid,
		SigningKeySid: twilioApiKey,
		Secret:        twilioApiSecret,
		Identity:      "user@example.com",
	}

	jwtToken := jwt.CreateAccessToken(params)
	chatGrant := &jwt.ChatGrant{
		ServiceSid:        "ISxxxxxxxxxxxx",
		PushCredentialSid: "CRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
	}

	jwtToken.AddGrant(chatGrant)
	token, err := jwtToken.ToJwt()

	if err != nil {
		error := fmt.Errorf("error: %q", err)
		fmt.Println(error.Error())
	}

	fmt.Println(token)
}
```

```php
<?php
// Get the PHP helper library from https://twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\ChatGrant;

// Required for all Twilio access tokens
// To set up environmental variables, see http://twil.io/secure
$twilioAccountSid = getenv('TWILIO_ACCOUNT_SID');
$twilioApiKey = getenv('TWILIO_API_KEY');
$twilioApiSecret = getenv('TWILIO_API_KEY_SECRET');

// Required for Chat grant
$serviceSid = 'ISxxxxxxxxxxxx';
$pushCredentialSid = 'CRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// choose a random username for the connecting user
$identity = "john_doe";

// Create access token, which we will serialize and send to the client
$token = new AccessToken(
    $twilioAccountSid,
    $twilioApiKey,
    $twilioApiSecret,
    3600,
    $identity
);

// Create Chat grant
$chatGrant = new ChatGrant();
$chatGrant->setServiceSid($serviceSid);
$chatGrant->setPushCredentialSid($pushCredentialSid);

// Add grant to token
$token->addGrant($chatGrant);

// render token to string
echo $token->toJWT();
```

```rb
require 'twilio-ruby'

# Required for any Twilio Access Token
# To set up environmental variables, see http://twil.io/secure
account_sid = ENV['TWILIO_ACCOUNT_SID']
api_key = ENV['TWILIO_API_KEY']
api_secret = ENV['TWILIO_API_KEY_SECRET']

# Required for Chat
service_sid = 'ISxxxxxxxxxxxx'
push_credential_sid = 'CRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
identity = 'user@example.com'

# Create Chat grant for our token
grant = Twilio::JWT::AccessToken::ChatGrant.new
grant.service_sid = service_sid
grant.push_credential_sid = push_credential_sid

# Create an Access Token
token = Twilio::JWT::AccessToken.new(
  account_sid,
  api_key,
  api_secret,
  [grant],
  identity: identity
)

# Generate the token
puts token.to_jwt
```

## 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](https://media.twiliocdn.com/sdk/android/conversations/latest/docs/) for details.

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