These instructions describe how to authenticate the Service Chat SDK using the provided authentication mechanism within the Salesforce Mobile SDK.
Before You Begin
important: The legacy chat product is scheduled for retirement on February 14, 2026, and is in maintenance mode until then. During this phase, you can continue to use chat, but we no longer recommend that you implement new chat channels. To avoid service interruptions to your customers, migrate to Messaging for In-App and Web before that date. Messaging offers many of the chat features that you love plus asynchronous conversations that can be picked back up at any time. Learn about chat retirement in Help.
Before starting, make sure that you’ve already:
- Installed the Service Chat SDK. See Install the Service SDK for Android.
- Installed the Salesforce Mobile SDK. If you want to install this SDK using maven, add a dependency on
com.salesforce.mobilesdk:SalesforceSDK:<VERSION_NUMBER>to your module'sbuild.gradlefile. - Created a connected app that allows the SDK to authenticate with your Salesforce Experience Cloud site. See Connected Apps. For an overview of Salesforce authentication from a mobile device, see Understand Security and Authentication.
note: When creating a connected app, be sure that it has access to the chatter_api scope. See Scope Parameter Values.
-
If you're using a Salesforce Experience Cloud site, be sure to configure the login endpoint as described in the Salesforce Mobile SDK documentation (Configure the Login Endpoint).
The documentation describes how to use the first server listed in your
servers.xmlfile as the default login location. For example:<servers> <server name="Site Login" url="https://MY_SITE_URL.com"/> </servers> -
Add an
account_typeproperty tostrings.xml. The property must be unique to your app to prevent conflicts with other apps that use the Service Chat SDK or the Salesforce Mobile SDK.<string name="account_type">com.mycompany.myapp</string> -
Implement
AuthenticatedUseras a wrapper to your Mobile SDK user.In Java:
import com.salesforce.androidsdk.accounts.UserAccount; import com.salesforce.android.service.common.http.AuthenticatedUser; public class MobileSdkUser implements AuthenticatedUser { private final String mUserId; public MobileSdkUser (UserAccount userAccount) { mUserId = userAccount.getUserId(); } @Override public String getUserId () { return mUserId; } }In Kotlin:
class MobileSdkUser(userAccount: UserAccount) : AuthenticatedUser { private val mUserId: String init { mUserId = userAccount.getUserId() } override fun getUserId(): String { return mUserId } }note:
AuthenticatedUser.getUserIdmust return the Salesforce user ID (UserAccount.getUserId) for the Case Management message feed to display correctly. -
Implement
AuthTokenProvideras a wrapper to your Mobile SDK authentication system.In Java:
import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.salesforce.android.service.common.http.AuthTokenProvider; import com.salesforce.android.service.common.http.ResponseSummary; import com.salesforce.androidsdk.rest.ClientManager; public class MobileSdkAuthTokenProvider implements AuthTokenProvider { private final ClientManager.AccMgrAuthTokenProvider mTokenProvider; private String mAuthToken; public MobileSdkAuthTokenProvider (ClientManager.AccMgrAuthTokenProvider tokenProvider, String initialToken) { mTokenProvider = tokenProvider; mAuthToken = initialToken; } @Nullable @Override public String getToken () { return mAuthToken; } @Nullable @Override public String getTokenType () { return "Bearer"; } @Override public boolean canRefresh () { return true; } @Override public void refreshToken (@NonNull ResponseSummary responseSummary) { mAuthToken = mTokenProvider.getNewAuthToken(); } }In Kotlin:
class MobileSdkAuthTokenProvider( private val mTokenProvider: ClientManager.AccMgrAuthTokenProvider, private var mAuthToken: String?) : AuthTokenProvider { override fun getToken(): String? { return mAuthToken } override fun getTokenType(): String? { return "Bearer" } override fun canRefresh(): Boolean { return true } override fun refreshToken(responseSummary: ResponseSummary) { mAuthToken = mTokenProvider.getNewAuthToken() } } -
When your app launches, initialize the Salesforce Mobile SDK.
// Initialize the Salesforce Mobile SDK SalesforceSDKManager.initNative(context, null, MainActivity.class);For more information, see the SalesforceSDKManager documentation in the Mobile SDK Developer’s Guide.
-
Write code that allows a user to log in and log out of their org using the Salesforce Mobile SDK. Make sure the user logs in before showing the Service Chat SDK UI.
For example:
public void login (final Activity activity) { SalesforceSDKManager.getInstance() .getClientManager() .getRestClient(activity, new ClientManager.RestClientCallback() { @Override public void authenticatedRestClient (RestClient client) { // Handle authenticated state activity here } }); } public void logout (final Activity activity) { SalesforceSDKManager.getInstance().logout(activity, true); } -
When configuring Knowledge (using
KnowledgeConfiguration) or Case Management (usingCaseConfiguration), use thewithAuthConfigmethod in the builder and pass in your implementation forAuthenticatedUserandAuthTokenProvider.This code sample illustrates how it works with Knowledge:
// Create Knowledge configuration builder KnowledgeConfiguration.Builder builder = KnowledgeConfiguration.builder(siteUrl).offlineResourceConfig(offlineConfig); // Grab a user account from the Salesforce Mobile SDK UserAccount userAccount = SalesforceSDKManager.getInstance().getUserAccountManager().getCurrentUser(); if (userAccount != null) { // Create an authorization token provider from the Salesforce Mobile SDK ClientManager.AccMgrAuthTokenProvider authTokenProvider = new ClientManager.AccMgrAuthTokenProvider( SalesforceSDKManager.getInstance().getClientManager(), userAccount.getInstanceServer(), userAccount.getAuthToken(), userAccount.getRefreshToken()); // Create a wrapper with the Salesforce Mobile SDK token provider AuthTokenProvider provider = new MobileSdkAuthTokenProvider(authTokenProvider, userAccount.getAuthToken()); // Build a config using the token provider and the authenticated user builder.withAuthConfig(provider, new MobileSdkUser(userAccount)); } // And now build a configuration object! KnowledgeConfiguration config = builder.build();This code sample illustrates how it works with Case Management:
// Create Case Management configuration builder CaseConfiguration.Builder builder = new CaseConfiguration.Builder(SITE_URL, CREATE_CASE_ACTION_NAME) // Grab a user account from the Salesforce Mobile SDK UserAccount userAccount = SalesforceSDKManager.getInstance().getUserAccountManager().getCurrentUser(); if (userAccount != null) { // Create an authorization token provider from the Salesforce Mobile SDK ClientManager.AccMgrAuthTokenProvider authTokenProvider = new ClientManager.AccMgrAuthTokenProvider( SalesforceSDKManager.getInstance().getClientManager(), userAccount.getInstanceServer(), userAccount.getAuthToken(), userAccount.getRefreshToken()); // Create a wrapper with the Salesforce Mobile SDK token provider AuthTokenProvider provider = new MobileSdkAuthTokenProvider(authTokenProvider, userAccount.getAuthToken()); // Build a config using the token provider and the authenticated user builder.withAuthConfiguration(provider, new MobileSdkUser(userAccount)) .caseListName(CASE_LIST_NAME); } // Create a core configuration instance CaseConfiguration coreConfiguration = builder.build();
At this point, you can use the features of the Service Chat SDK as an authenticated user.