This approach is for developers who want to build a completely custom UI or run agent interactions in the background. The AgentforceService handles the communication and state, emitting events via Kotlin Flows that your app uses to update its own UI or trigger logic.
Instantiate AgentforceService
Use the AgentforceServiceProvider to create an AgentforceService instance for a specific agent.
1import com.salesforce.android.agentforceservice.AgentforceServiceProvider2import com.salesforce.android.agentforceservice.AgentforceService34// Create the service provider with required dependencies5val serviceProvider = AgentforceServiceProvider(6 configurationLocale = Locale.getDefault(),7 domain = "https://your-domain.my.salesforce.com",8 network = MyAppNetwork(okHttpClient),9 credentialProvider = MyAppCredentialProvider(userSession),10 instrumentationHandler = myInstrumentationHandler, // Optional11 logger = myLogger // Optional12)1314// Get the service for your specific agent15val agentId = "YOUR_AGENT_ID"16val agentforceService: AgentforceService = serviceProvider.getAgentforceService(agentId)
Send Messages
To send a message to the agent programmatically, you can use a method like sendUtterance on your AgentforceConversation object:
1// Example: Sending a message with an optional attachment2conversation.sendUtterance(3 utterance = "Hello, world!",4 attachment = null // or provide an AgentforceAttachment object5)
Receive Messages
To receive messages from the agent, you can use the conversationManager property on the AgentforceConversation object:
1// Example: Using conversationManager to send and receive messages2val responseChannel = conversation.conversationManager.sendMessage(3 inputRepresentation = ConversationInputRepresentation("Hello, world!"),4 agentforceAttachment = null, // Optional attachment5 file = null // Optional file6)