Adding a chat to Android application is made easy by Azure Communication Services. Luckily, the requirement to build user interface can be fulfilled by ChatKit.

At end of this post, you will be familiar with:

Build Android Sample Chat Application

Chat Admin Chat Join

Create Azure Communication Service Resource

Generate Communication Service User Token

  • Clone access-tokens-quickstart
  • cd to access-tokens-quickstart
  • Execute ‘npm install`
  • Enter connection string copied previously to const connectionString
  • Change scope to chat
// This code demonstrates how to fetch your connection string
    // from an environment variable.

    const connectionString = "endpoint=https://************.azure.com/;accesskey=**********==";

    // Instantiate the identity client
    const identityClient = new CommunicationIdentityClient(connectionString);

    // This code demonstrates how to fetch your endpoint and access key
    // from an environment variable.
    /*const endpoint = process.env["COMMUNICATION_SERVICES_ENDPOINT"];
    const accessKey = process.env["COMMUNICATION_SERVICES_ACCESSKEY"];
    const tokenCredential = new AzureKeyCredential(accessKey);
    // Instantiate the identity client
    const identityClient = new CommunicationIdentityClient(endpoint, tokenCredential);*/

    // Authenticate with managed identity
   // const endpoint = "endpoint=https://communication-chat.communication.azure.com/;accesskey=Th0P8iQR2PPHJXQw2OFwBt0qmciVOMKexoClvpM1HI2iOJE+F5QKgnZR/we9X10avgJSlFrt4PNy6jnwBfLmMQ==";
  //  const tokenCredential = new DefaultAzureCredential();
   // const identityClient = new CommunicationIdentityClient(endpoint, tokenCredential);

    // Create an identity
    let identityResponse = await identityClient.createUser();
    console.log(`\nCreated an identity with ID: ${identityResponse.communicationUserId}`);

    // Issue an access token with the "voip" scope for an identity
    let tokenResponse = await identityClient.getToken(identityResponse, ["chat"]);
    const { token, expiresOn } = tokenResponse;
    console.log(`\nIssued an access token with 'voip' scope that expires at ${expiresOn}:`);
    console.log(token);

    //Refresh access tokens
    // Value of identityResponse represents the Azure Communication Services identity stored during identity creation and then used to issue the tokens being refreshed
  //  let refreshedTokenResponse = await identityClient.getToken(identityResponse, ["chat"]);

    // Revoke access tokens
   // await identityClient.revokeTokens(identityResponse);
   // console.log(`\nSuccessfully revoked all access tokens for identity with ID: ${identityResponse.communicationUserId}`);

    // Delete an identity
  //  await identityClient.deleteUser(identityResponse);
   // console.log(`\nDeleted the identity with ID: ${identityResponse.communicationUserId}`);

Having an issue? Check if below video can help.

Start Chat

  • Enter required information obtained by executing node .\issue-access-token.js
  • On Start Chat button click, you will see a ThreadId generated in logs. ChatThreadID -: 19:Pk6_myvAX.....U1@thread.v2

Join Chat

  • Enter required information, input threadId from Run AzureChatAdmin
  • Click Join Chat

Chat