Simples Assim

How to Trigger Notifications in BlackBerry OS 3.6+

leave a comment »

Application

package br.eti.fernandoribeiro.sample.ui;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.notification.NotificationsConstants;
import net.rim.device.api.notification.NotificationsManager;
import net.rim.device.api.ui.UiApplication;

import br.eti.fernandoribeiro.sample.notifications.SampleConsequence;
import br.eti.fernandoribeiro.sample.util.Constants;
import br.eti.fernandoribeiro.sample.util.SampleResource;

public final class SampleApplication extends UiApplication {
  private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(SampleResource.BUNDLE_ID, SampleResource.BUNDLE_NAME);

  public static void main(final String[] args) {
    NotificationsManager.registerSource(Constants.SOURCE_SAMPLE, BUNDLE.getString(SampleResource.APPLICATION_TITLE), NotificationsConstants.DEFAULT_LEVEL);

    NotificationsManager.registerConsequence(Constants.CONSEQUENCE_SAMPLE, new SampleConsequence());
    ...
    NotificationsManager.triggerImmediateEvent(Constants.SOURCE_SAMPLE, Constants.EVENT_SAMPLE, null, null); // May be anywhere in the app
    ...
  }

  ...
}

Consequence

package br.eti.fernandoribeiro.notifications;

import net.rim.device.api.notification.Consequence;
import net.rim.device.api.synchronization.SyncConverter;
import net.rim.device.api.synchronization.SyncObject;
import net.rim.device.api.system.LED;
import net.rim.device.api.util.DataBuffer;

import br.eti.fernandoribeiro.util.Constants;

public final class SampleConsequence implements Consequence, SyncConverter {

  private static final class Configuration implements SyncObject {

    public int getUID() {
      return 0;
    }

  }

  private static final Configuration CONFIG = new Configuration();

  public SyncObject convert(final DataBuffer data, final int version, final int UID) {
    return new Configuration();
  }

  public boolean convert(final SyncObject object, final DataBuffer buffer, final int version) {
    return (object instanceof Configuration);
  }

  public Object newConfiguration(final long consequenceID, final long sourceID, final byte profileIndex, final int level, final Object context) {
    return CONFIG;
  }

  public void startNotification(final long consequenceID, final long sourceID, final long eventID, final Object configuration, final Object context) {

    if ((Constants.CONSEQUENCE_SAMPLE == consequenceID) && (Constants.SOURCE_SAMPLE == sourceID) && (Constants.EVENT_SAMPLE == eventID))
      LED.setState(LED.STATE_BLINKING);

  }

  public void stopNotification(final long consequenceID, final long sourceID, final long eventID, final Object configuration, final Object context) {

    if ((Constants.CONSEQUENCE_SAMPLE == consequenceID) && (Constants.SOURCE_SAMPLE == sourceID) && (Constants.EVENT_SAMPLE == eventID))
      LED.setState(LED.STATE_OFF);

  }

}

Constants (Recommended)

package br.eti.fernandoribeiro.sample.util;

public interface Constants {
  long CONSEQUENCE_SAMPLE = 0xcdb9b62f62f4dccdL; // Global

  long EVENT_SAMPLE = 0x35b82a6314580fffL; // Global

  long SOURCE_SAMPLE = 0L; // Application-specific
}

http://c2.com/ppr/wiki/JavaIdioms/InterfacesForDefiningConstants.html

http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/notification/NotificationsManager.html

http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/notification/Consequence.html

Advertisement

Written by Fernando Ribeiro

July 18, 2010 at 10:34 pm

Posted in Software

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 781 other followers