Add a Conversation to an Android app
Initialize the OpenWeb SDK and configure the Conversation in your Android app.
When implemented, a user sees the following displayed below your article in your article Activity or Fragment:
- Preview of comments (preConversationFragment) from the conversation
- Text box to enter comments
- Button to show all comments
If a user clicks the button to see more comments, the SDK opens the Activity which displays all comments from the conversation.
If a user clicks on the text box, the creation screen appears to enable the user to type a comment.
Implementation
Use the following steps to initialize the SDK and configure the Conversation.
- In
onCreate()
of the application class, initialize the OpenWeb SDK with yourSpot_ID
.
SpotIm.init(
this,
"SPOT_ID"
)
SpotIm.init(
context,
"SPOT_ID"
);
Or with success callback:
SpotIm.init(
this,
"SPOT_ID",
object: SpotVoidCallback {
override fun onSuccess() {
//do Something ...
}
override fun onFailure(exception: SpotException) {
when(exception) {
is SPServerErrorException -> { } // do something
is SPNoInternetConnectionException -> { } // do something
is SPSdkDisabeledException -> { } // do something
else -> { } // do something
}
}
}
)
SpotIm.init(
context,
"SPOT_ID",
new SpotVoidCallback() {
@Override
public void onSuccess() {
//do Something ...
}
@Override
public void onFailure(@NotNull SpotException exception) {
if (exception instanceof SPNoInternetConnectionException) {
// do something
} else if (exception instanceof SPServerErrorException) {
// do something
} else if (exception instanceof SPSdkDisabeledException) {
// do something
} else {
// do something
}
}
}
);
- In
onCreate()
of an article activity, add the pre-conversation fragment to the Activity.
SpotIm.getPreConversationFragment(CONVERSATION_ID, object :
SpotCallback<Fragment> {
override fun onSuccess(fragment: Fragment) {
//do Something ...
}
override fun onFailure(exception: SpotException) {
//do Something ...
}
})
SpotIm.getPreConversationFragment(CONVERSATION_ID,new SpotCallback<Fragment>() {
@Override
public void onSuccess(Fragment fragment) {
//doSomething...
}
@Override
public void onFailure(SpotException exception) {
//doSomething...
}
});
Add images to comments
We support adding images to comments from the camera or the photo library.
In order to enable this option in the comment creation screen, please add the following permissions to the AndroidManifest.xml
file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
Get a comment counter
To show your users the engagement level of multiple Conversations, you can display the number of user comments that have been posted on each article on your main screen.
The following code example shows how to get and display a comment counters for multiple conversations.
SpotIm.getConversationCounters(listOf(<POST_ID>), object : SpotCallback<Map<String, ConversationCounters>> {
override fun onSuccess(response: Map<String, ConversationCounters>) {
val counters = response[<SPECIFIC_POST_ID>]
counters?.let {
print(it.comments)
print(it.replies)
}
}
override fun onFailure(exception: SpotException) {
// Do something
}
})
SpotIm.getConversationCounters(Arrays.asList(<POST_ID>), new SpotCallback<Map<String, ConversationCounters>>() {
@Override
public void onSuccess(Map<String, ConversationCounters> response) {
ConversationCounters counters = response.get(<SPECIFIC_POST_ID>);
System.out.println(counters.getComments());
System.out.println(counters.getReplies());
}
@Override
public void onFailure(SpotException exception) {
// Do something
}
});
Updated 3 months ago