Notify Attendees in Realtime - Android
When doing livestream, you may want to broadcast message to all the viewers at once.
Let us see, how we can use PubSub to implement this functionality. If you are not familiar with the PubSub mechanism, you can follow this guide.
Notifying Attendees
- We will be publish the message that the sender typed in the
EditText
field to the topicNOTIFY_ATTENDEES
.
- Kotlin
- Java
class SpeakerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//...
findViewById(R.id.btnSend).setOnClickListener(view -> sendMessage());
}
private fun sendMessage() {
// get message from EditText
val message: String = etmessage.getText().toString()
if (!TextUtils.isEmpty(message)) {
val publishOptions = PubSubPublishOptions()
publishOptions.setPersist(true)
// Sending the Message using the publish method
meeting!!.pubSub.publish("NOTIFY_ATTENDEES", message, publishOptions)
// Clearing the message input
etmessage.setText("")
} else {
Toast.makeText(
this@SpeakerActivity, "Please Enter Message",
Toast.LENGTH_SHORT
).show()
}
}
}
public class SpeakerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
findViewById(R.id.btnSend).setOnClickListener(view -> sendMessage());
}
private void sendMessage()
{
// get message from EditText
String message = etmessage.getText().toString();
if (!message.equals("")) {
PubSubPublishOptions publishOptions = new PubSubPublishOptions();
publishOptions.setPersist(true);
// Sending the Message using the publish method
meeting.pubSub.publish("NOTIFY_ATTENDEES", message, publishOptions);
// Clearing the message input
etmessage.setText("");
} else {
Toast.makeText(SpeakerActivity.this, "Please Enter Message",
Toast.LENGTH_SHORT).show();
}
}
}
- Now let us show an alert to all the viewers displaying the message posted by the speaker.
- Kotlin
- Java
class ViewerActivity : AppCompatActivity() {
// PubSubMessageListener
var pubSubMessageListener =
PubSubMessageListener { message ->
if(meeting!!.localparticipant.mode == "VIEWER"){
Toast.makeText(
this@ViewerActivity, message.message,
Toast.LENGTH_SHORT
).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
//...
// Subscribe for 'NOTIFY_ATTENDEES' topic
val pubSubMessageList = meeting!!.pubSub.subscribe("NOTIFY_ATTENDEES", pubSubMessageListener)
for (message in pubSubMessageList) {
// Persisted messages
if(meeting!!.localparticipant.mode == "VIEWER"){
Toast.makeText(
this@ViewerActivity, message.message,
Toast.LENGTH_SHORT
).show()
}
}
}
}
public class ViewerActivity extends AppCompatActivity {
// PubSubMessageListener
private PubSubMessageListener pubSubMessageListener = new PubSubMessageListener() {
@Override
public void onMessageReceived(PubSubMessage message) {
if(meeting.getLocalParticipant().getMode() == "VIEWER"){
Toast.makeText(
ViewerActivity.this, message.getMessage(),
Toast.LENGTH_SHORT
).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//..
// Subscribe for 'NOTIFY_ATTENDEES' topic
List<PubSubMessage> pubSubMessageList = meeting.pubSub.subscribe("NOTIFY_ATTENDEES", pubSubMessageListener);
for(PubSubMessage message : pubSubMessageList){
// Persisted messages
if(meeting.getLocalParticipant().getMode() == "VIEWER"){
Toast.makeText(
ViewerActivity.this, message.getMessage(),
Toast.LENGTH_SHORT
).show();
}
}
}
}
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord