Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Defining service listeners

Define a channel listener

The following code sample assumes that you are creating this class as part of an application that requires a channel listener.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.bbm.platform.*;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
  2. Create the framework for the channel listener and define each callback method's signature. The following code sample displays a message that indicates what event has occured.
    class MyChannelListener extends BBMPlatformChannelListener
    {
        public void invitationAccepted(BBMPlatformConnection connection, BBMPlatformContact contact)
        {
            Dialog.inform(contact.getDisplayName() + " has accepted your invitation.");
        }
    
        public void invitationDeclined(BBMPlatformConnection connection, String contactDisplayName) 
        {
            Dialog.inform(contactDisplayName + " has declined your invitation.");
        }
       
        public void invitationFailed(BBMPlatformConnection connection, String inviteeName, int reasonCode) 
        {
            Dialog.inform(inviteeName + " has failed to receive your invitation. Error: " + reasonCode);
        }
    
    
        public void contactLeft(BBMPlatformConnection connection, BBMPlatformContact contact) 
        {
            Dialog.inform(contact.getDisplayName() + " has left the application.");
        }
    
        //NOTE: The message parameter is received as a data array. This method converts message to a String before displaying.
        public void dataReceived(BBMPlatformConnection connection, BBMPlatformContact fromContact, BBMPlatformData message)
        {
            Dialog.inform(fromContact.getDisplayName() + ": " + new String(message.getData()));
        }
    
        //NOTE: param is a custom parameter defined by the application and passed in from the sender of the invitation
        public void invitationReceived(BBMPlatformConnection connection, BBMPlatformContact contact, String param)
        {
            Dialog.inform(contact.getDisplayName() + ": " + param);
        }
    
        public void invitationsSent(BBMPlatformConnection connection, int count)
        {
            Dialog.inform("I've sent " + count + " invitations.");
        }
    
        public void joinFailed(BBMPlatformConnection connection, BBMPlatformContact inviter, int reasonCode)
        {
            Dialog.inform("The user has failed to join the session. Error: " + reasonCode);
        }
       
    }

Define a messaging service listener

The following code sample assumes that you are creating this class as part of an application that requires a messaging service listener.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.bbm.platform.*;
    import net.rim.blackberry.api.bbm.platform.service.*;
  2. Create the framework for the messaging service listener, defining each callback method's signature. Declare a member variable for the channel listener associated with this messaging service.
    class MyMessagingServiceListener extends MessagingServiceListener
    {
         public void channelCreated(BBMPlatformChannel channel, InvitationMenuItem menuItem)
         {
             //add any code for the channel's initialization here
         }
    
         public BBMPlatformChannelListener(BBMPlatformChannel channel) 
         {
             return _channelListener;
         }
    }

Define a context change listener

The following example assumes that you are creating a private inner class as part of an application that requires a context change listener.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.bbm.platform.*;
    import net.rim.device.api.ui.component.*;
  2. Create the framework for the context change service listener, defining the accessChanged() method. The accessErrorCode parameter is one of the fields from the BBMPlatformContext class (for example, ACCESS_BLOCKED_BY_IT_POLICY). The following code sample displays dialog boxes that informs the user what change has occurred.
    class MyBBMPlatformContextListener extends BBMPlatformContextListener
    {
        public void accessChanged(boolean isAccessAllowed, int accessErrorCode)
        {
            if(isAccessAllowed) 
            {
                Dialog.inform("Access to the platform context is now allowed.");
            }
            else
            {
                Dialog.inform("Access to the platform context is now disallowed because 
                              of error: " + accessErrorCode);
            }
        }
        
        public void appInvoked(int reason, Object param)
        {
                /* code for handling different contexts for invocation:
                 *  
                 * <reason> is a constant in BBMPlatformApplication prepended 
                 * with INVOKE, which tells you why this app was invoked and by 
                 * whom 
                 * <param> is an object representing a custom paramater with 
                 * more information about the invocation context
                 */ 
        }
    }

Define a session listener

The following example assumes that you are creating this class as part of an application that requires a session listener.
  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.*;
  2. Create the framework for the session listener, defining each callback method's signature, and implement the method as required. The following code sample displays a dialog that informs the user of the change that has occured.
    class MySessionListener extends BBMPlatformSessionListener 
    {
        public void invitationAccepted(BBMPlatformConnection connection, BBMPlatformContact contact) 
        {
            Dialog.inform(contact.getDisplayName() + " has accepted the session invitation ");   
        }
        
        public void invitationDeclined(BBMPlatformConnection connection, String contactDisplayName)
        {
            Dialog.inform(contactDisplayName + " has declined the session invitation "));    
        }
            
        public void joinFailed(BBMPlatformConnection connection, BBMPlatformContact inviter, int reasonCode) 
        {
            Dialog.inform(inviter.getDisplayName() + " has rejected the session join request due to the reason: " + Integer.toString(reasonCode));
        }
            
        public void invitationFailed(BBMPlatformConnection connection, String inviteeName, int reasonCode) 
        {
            Dialog.inform(inviteeName + "cannot accept the channel invite due to the reason: " + Integer.toString(reasonCode));
        }
     
        public void contactsJoined(BBMPlatformSession session, BBMPlatformContactList contactList) 
        {
            if (contactList != null) 
            {
                Enumeration contacts = contactList.getAll();
                while (contacts.hasMoreElements()) 
                {
                    BBMPlatformContact contact = (BBMPlatformContact)contacts.nextElement();
                    Dialog.inform(contact.getDisplayName() + " has joined the session.");                            
                }               
            }             
        }
                
        public void contactsRemoved(BBMPlatformSession session, BBMPlatformContact removedBy, BBMPlatformContactList contactList) 
        {
            if (contactList != null) 
            {
                Enumeration contacts = contactList.getAll();
                while (contacts.hasMoreElements()) 
                {
                    BBMPlatformContact contact = (BBMPlatformContact)contacts.nextElement();
                    Dialog.inform(contact.getDisplayName() + " has been removed from the session by " + removedBy.getDisplayName());                            
                }               
            }
        }
        
        public void contactLeft(BBMPlatformConnection connection, BBMPlatformContact contact)
        {
            Dialog.inform(contact.getDisplayName() + " has left the session "); 
        }
            
        public void dataReceived(BBMPlatformConnection connection, BBMPlatformContact fromContact, BBMPlatformData data)
        { 
            Dialog.inform("session msg from:" + fromContact.getDisplayName() + " : " + data.getDataAsString()));                 
        }
            
        public void broadcastDataReceived(BBMPlatformSession session, BBMPlatformContact sender, BBMPlatformData data) 
        {
            Dialog.inform("broadcasting msg from:" + sender.getDisplayName() + " > " + data.getDataAsString()));    
        }
        
        public void invitationReceived(BBMPlatformConnection connection, BBMPlatformContact contact, String param)
        {
            Dialog.inform(contact.getDisplayName() + " is inviting me to join the session ")); 
        }
    
        public void invitationsSent(BBMPlatformConnection connection, int count) 
        {
            Dialog.inform(count + " session invitation(s) sent"));
        }
            
        public void sessionEnded(BBMPlatformContact contact) 
        {
            Dialog.inform(contact.getDisplayName() + " ends the session");
        }
           
        public void joinRequestReceived(BBMPlatformConnection connection, BBMPlatformIncomingJoinRequest request, String param)
        {
            _incomingJoinRequest = request;
            Dialog.inform("Incoming Session Join request received from " + request.getRequester().getDisplayName() + ". Please Accept or Decline",FOCUSABLE);                    
        } 
        
        public void joinRequestCancelled(BBMPlatformConnection connection, BBMPlatformIncomingJoinRequest request, int reasonCode)
        {
            Dialog.inform("The join request has been cancelled by " + request.getRequester().getDisplayName() + " for reason: " + reasonCode,FOCUSABLE);                    
        }  
    }