Host a public session

Before you begin: Make sure that you have completed the task, Register your application with the BlackBerry Messenger platform, and that the class that displays the screen passes in a reference to the BBMPlatformContext object associated with your application into the screen's constructor.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.bbm.platform.*;
    import net.rim.blackberry.api.bbm.platform.service.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.MainScreen;
  2. Define a session listener. The code sample in step 5 assumes that you named the listener class, MySessionListener.
  3. Create a class that extends MainScreen. In the constructor for this screen class, pass in the BBMPlatformContext object associated with your application.
    public class MyBBMInviteScreen extends MainScreen
    {
        public MyBBMInviteScreen(BBMPlatformContext platformContext) 
        {
    
        }
    }
  4. In the constructor, retrieve a reference to the messaging service for this application.
    public class MyBBMInviteScreen extends MainScreen
    {
        public MyBBMInviteScreen(BBMPlatformContext platformContext) 
        {
            MessagingService messagingService = platformContext.getMessagingService();
        }
    }
  5. Invoke MessagingService.createSession() to create a new BBMPlatformSession instance. Pass in an instance of the listener you defined in step 2 as a parameter.
    public class MyBBMInviteScreen extends MainScreen
    {
        public MyBBMInviteScreen(BBMPlatformContext platformContext) 
        {
            MessagingService messagingService = platformContext.getMessagingService();
            BBMPlatformSession mySession = messagingService.createSession(new 
                 MySessionListener());
        }
    }
  6. Invoke BBMPlatformSession.setPublic() to set this session as public so that contacts who are not in your BlackBerry Messenger contact list can join. A confirmation dialog is presented to the user when this method is invoked.
    public class MyBBMInviteScreen extends MainScreen
    {
        public MyBBMInviteScreen(BBMPlatformContext platformContext) 
        {
            MessagingService messagingService = platformContext.getMessagingService();
            BBMPlatformSession mySession = messagingService.createSession(new 
             MySessionListener());
    
            boolean isHosting = mySession.setPublic();
    
             if (isHosting)
                 {
                    // add code to send PIN and PPID to your web service
                 }
        }
    }
    After you confirm that you want to host the session, you must send your web service your PIN and PPID. You can now receive join requests from users looking to join your session even if they are not one of your BlackBerry Messenger contacts. The joinRequestReceived() method in your BBMPlatformSessionListener implementation is invoked when a BBMPlatformIncomingJoinRequest object is received by your application. You can accept or decline this request by invoking BBMPlatformIncomingJoinRequest.accept() or BBMPlatformIncomingJoinRequest.decline(), respectively.

Code sample: Hosting a public session

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

This code sample also assumes that you have completed the task, Define a session listener and 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.*;
import net.rim.device.api.ui.container.MainScreen;

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

        if (isHosting) 
			{
			 	// add code to send PIN and PPID to your web service
			}	
		
    }
}
Back To Top

Was this information helpful? Send us your comments.