Send a message to all participants in a session

Before you begin: Send a session invitation to a contact to populate your session's contact list with contacts.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.bbm.platform.*;
    import net.rim.device.api.ui.component.*;
  2. In the if statement that you created when you completed the Send a session invitation to a contact task, invoke BBMPlatformSession.getContactList() to retrieve an instance of BBMPlatformContactList. The returned contact list contains all the contacts that have accepted your invitation.
    if(mySession != null)
    {
        BBMPlatformContactList contacts = mySession.getContactList();
    }
  3. Create an instance of the BBMPlatformData class (a wrapper class for your data message) and String object (the body of your message). The following code sample uses the BBMPlatformData constructor that defines a content type (the first parameter of the constructor), the data itself (the second parameter), and an application defined ID (the third parameter). You can define the content type and ID using any String and int value respectively that your application requires for parsing. You can also send a message without those values if they are not needed.
    String myMsg = "Test data";
    BBMPlatformData data = new BBMPlatformData("Message", myMsg.getBytes(), 0);
  4. Invoke BBMPlatformSession.broadcastData(), inherited from its parent class, to send the message. The following code sample displays a dialog box confirming that the message has been sent.
    String myMsg = "Test data";
    BBMPlatformData data = new BBMPlatformData("Message", myMsg.getBytes(), 0);
         
    mySession.broadcastData(data, contacts);

Code sample: Sending a message to all participants in a session

The following code sample assumes that the class that displayed the MyBBMScreen has passed a reference to the application's associated BBMPlatformContext object into the MyBBMScreen constructor.

This code sample also assumes that you have completed the task, Define a session listener, and that you named the session listener class, MySessionListener.

import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.service.*;
import net.rim.device.api.ui.component.*;

public class MyBBMScreen extends MainScreen
{
    public MyBBMScreen(BBMPlatformContext platformContext)
    {
        MessagingService messagingService = platformContext.getMessagingService();
       
        BBMPlatformSession mySession = messagingService.createSession(new MySessionListener());

        if (mySession != null) 
        {
            channel.sendInvitation("Lets play a game", "Poker App", null, 0);

            BBMPlatformContactList contacts = channel.getContactList();

            String myMsg = "Test data";
            BBMPlatformData data = new BBMPlatformData("Message", myMsg.getBytes(), 0);

            mySession.sendData(data, contacts);
        }
    }
}
Back To Top

Was this information helpful? Send us your comments.