Application is not notified when new messages arrive

by Retired on ‎02-16-2010 01:27 PM - edited on ‎04-26-2012 09:12 AM by BlackBerry Development Advisor (6,357 Views)



This article applies to BlackBerry® devices based on Java™.





An application can add FolderListener to folders with the INBOX type so the application is notified when new messages arrive on the BlackBerry device. If an application does not receive notification when new messages arrive, it is likely because FolderListener was not added to all the appropriate folders.


For most corporate users, the Inbox folder is stored at the root level of the folder hierarchy. The BlackBerry Device Simulator uses this corporate Inbox as well. For some users, an Inbox can exist at one or more levels lower in the hierarchy. This is often true for users with a BlackBerry Internet Service account.


The Store.list() method only returns folders at the root level of the folder hierarchy. The BlackBerry device user's Inbox is not returned if it is in a subfolder, so the FolderListener is not added. This is shown in the following code lines:



try {
Store s = Session.waitForDefaultSession().getStore();
Folder[] folders = s.list(Folder.INBOX);
Folder inbox = folders[0];
catch (Exception e) {}




Complete either one of the following two possible solutions for this issue:


Solution 1 - Add a FolderListener to the Store


Instead of adding a FolderListener to a specific folder with the INBOX type, you can add a FolderListener to the entire Store. During the implementation of the FolderListener methods, evaluate whether the FolderEvent is for a received (inbound) message.


The following code demonstrates how to do this:



Store store = Session.waitForDefaultSession().getStore();
store.addFolderListener(new FolderListener() {
public void messagesAdded(FolderEvent e) {
if( e.getMessage().isInbound() == true )
//message is a new received message
//perform some task here
//implement other FolderListener methods



Solution 2 - Search recursively for INBOX folders

An alternative approach is to iterate through the folder hierarchy and complete a recursive search to retrieve a reference to all INBOX folders:


  • Invoke Store.list() with no type parameter to return a list of all folders.
  • List all folders at the top level of type INBOX.
  • Iterate through each of the other folders and repeat these steps until you have searched all the available subfolders.

The following sample code demonstrates how to search for INBOX folders and add a FolderListener to each one:



public class folderListenerImpl implements FolderListener{
Store store = Session.waitForDefaultSession().getStore();
Folder[] folders = store.list();
for( int foldercnt = folders.length - 1; foldercnt >= 0; --foldercnt) {
Folder f = folders[foldercnt];
public void recurse(Folder f)
if ( f.getType() == Folder.INBOX )
Folder[] farray = f.list();
for (int fcnt = farray.length - 1; fcnt >= 0; --fcnt)


Note: In the above examples, Session.waitForDefaultSession().getStore() retrieves only the default session, which is associated with the message service configured under Options > Message Services on the BlackBerry device. If this option is set to Desktop, the message service is associated with a BlackBerry Enterprise Server account. If the option is set to Web Client, the message service is associated with a BlackBerry Internet Service account. For users with both BlackBerry Enterprise Server and BlackBerry Internet Service accounts who want to listen for new messages arriving in both of these accounts, retrieve the session for both services by searching through the service books on the BlackBerry device. The following code demonstrates how to retrieve a session for both BlackBerry Enterprise Server and BlackBerry Internet Service services:



ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] srs = sb.getRecords();

for(int cnt = srs.length - 1; cnt >= 0; --cnt) {

//identify the service record associated with a mail message service via a CID of 'CMIME'
if( srs[[cnt].getCid().equals( "CMIME" )) {
ServiceConfiguration sc = new ServiceConfiguration(srs[cnt]);
Store store = Session.getInstance(sc).getStore();

//then search recursively for INBOX folders using either method (1) or (2) above


Note: ServiceBook and ServiceRecord application programming interfaces (APIs) are supported in BlackBerry Java Development (JDE) 4.0 and later.