Send a message to a contact
Before you begin: Complete the task,
Register your
application with the BlackBerry Messenger platform. Verify that the class
that displays the screen in the following code sample passes in a reference to
the
BBMPlatformContext object associated with the application.
-
Import the required classes and interfaces.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.io.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
import net.rim.blackberry.api.bbm.platform.service.*;
import net.rim.device.api.ui.component.*;
-
Create a class that extends
MainScreen. In the constructor for this screen
class, pass in the application's associated
BBMPlatformContext.
public class MyBBMScreen extends MainScreen
{
public MyBBMScreen(BBMPlatformContext platformContext)
{
}
}
-
Declare an instance of
BBMPlatformChannel as a member variable of the
MyBBMScreen class.
public class MyBBMScreen extends MainScreen
{
BBMPlatformChannel _channel;
public MyBBMScreen(BBMPlatformContext platformContext)
{
}
}
-
In the
MyBBMScreen constructor, retrieve an instance of the
messaging service.
public MyBBMScreen(BBMPlatformContext platformContext)
{
MessagingService myMessagingService = platformContext.getMessagingService();
}
-
Create a channel passing in an instance of a
BBMPlatformChannelListener that you will define in
step 9.
public MyBBMScreen(BBMPlatformContext platformContext)
{
MessagingService myMessagingService = platformContext.getMessagingService();
_channel = messagingService.createChannel(_channelListener);
}
-
Invite a contact to your application. In the parameters for
sendInvitation(), you can specify a message to be
delivered when the contact receives the invitation, a parameter that will be
passed to the application's main method as
args[1], and an expiry time. The following code
sample passes in the value 0 for expiry time (that is, the invitation never
expires). A Contact picker UI component opens for the user to choose a contact
to invite.
public MyBBMScreen(BBMPlatformContext platformContext)
{
MessagingService messagingService = platformContext.getMessagingService();
_channel = messagingService.createChannel(_channelListener);
_channel.sendInvitation("Let's play a game", "Chess App", 0);
}
-
In the
MyBBMScreen class, define a helper method,
onContactJoined(), which the
BBMPlatformChannelListener.contactJoined() callback
method invokes. Pass in a reference to the contact who has just joined.
public class MyBBMScreen extends MainScreen
{
BBMPlatformChannel _channel;
public MyBBMScreen(BBMPlatformContext platformContext)
{
//code from steps 5 to 7
}
private onContactJoined(BBMPlatformContact contact)
{
}
}
-
In the
onContactJoined() method, create a message, wrap it
in a
BBMPlatformData object, and, in a try-catch block,
send the message. In the following code sample, the message is sent to all
contacts who have joined the channel, informing that a participant has left the game.
private void onContactLeft(BBMPlatformContact contact)
{
String goodbyeMsg = contact.getDisplayName() + " has left the game.";
BBMPlatformData data = new BBMPlatformData(goodbyeMsg);
try {
_channel.sendData(data, _channel.getContactList());
}
catch (BBMPlatformException e)
{
// Error handler code
}
}
-
In the
MyBBMScreen class, define the channel listener,
_channelListener, as a private inner class. In the
contactJoined() method, invoke the
onContactJoined() method passing in the
BBMPlatformContact that just joined.
public class MyBBMScreen extends MainScreen
{
public MyBBMScreen(BBMPlatformContext platformContext)
{
//code from steps 4 to 6
}
private void onContactJoined(BBMPlatformContact contact)
{
//code from step 8
}
private BBMPlatformChannelListener _channelListener = new BBMPlatformChannelListener()
{
public void contactsInvited(BBMPlatformConnection conn, BBMPlatformContactList
contactList)
{
}
public void contactsJoined(BBMPlatformConnection conn, BBMPlatformContactList
contacts, String cookie, int type)
{
}
public void contactDeclined(BBMPlatformConnection conn,
BBMPlatformContact contact)
{
}
public void contactLeft(BBMPlatformConnection conn, BBMPlatformContact contact)
{
onContactLeft(contact);
}
public void dataReceived(BBMPlatformConnection conn, BBMPlatformContact sender,
BBMPlatformData data)
{
}
}
};
}
Code sample: Sending a message to a contact
The following code sample assumes that the class that displayed the
MyBBMScreen passed a reference to the application's
associated
BBMPlatformContext object into the
MyBBMScreen constructor.
import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.io.*;
import net.rim.blackberry.api.bbm.platform.profile.*;
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();
_channel = messagingService.createChannel(_channelListener);
_channel.sendInvitation("Let's play a game", "Chess App", 0);
}
private void onContactLeft(BBMPlatformContact contact)
{
String goodbyeMsg = contact.getDisplayName() + " has left the game.";
BBMPlatformData data = new BBMPlatformData(goodbyeMsg);
try
{
_channel.sendData(data, _channel.getContactList());
}
catch (BBMPlatformException e)
{
// Error handler code
}
}
private BBMPlatformChannelListener _channelListener = new BBMPlatformChannelListener()
{
public void contactsInvited(BBMPlatformConnection conn, BBMPlatformContactList contactList )
{
}
public void contactsJoined(BBMPlatformConnection conn, BBMPlatformContactList contacts, String cookie,
int type)
{
}
public void contactDeclined(BBMPlatformConnection conn, BBMPlatformContact contact)
{
}
public void contactLeft(BBMPlatformConnection conn, BBMPlatformContact contact)
{
onContactLeft(contact);
}
public void dataReceived(BBMPlatformConnection conn, BBMPlatformContact sender, BBMPlatformData data)
{
}
};
}
Was this information helpful? Send us your comments.