Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 6
Registered: ‎03-09-2010
My Device: Bold 9700
My Carrier: MTC touch

SMS Pushregistry / How to invoke my app using sms and read it

Hello!

 

-We need to develop an application that listens to a certain port (using pushregistry API)

-Once the SMS is sent from our platform/sms server (inlcuding in the sms header thecorrepondent port number) to the BB device; the message should first trigger the application that is previously installed on it and then launch it.

-Once launched; the application should read the content of this message and display it on the screen.

 

We are able to do it in nokia java devices;

Could you please send us the source code that performs the above : end-2-end.

 

Your help and support are highly appreciated;

Thank you in advance;

B/R

Marioxas

New Contributor
Posts: 6
Registered: ‎03-09-2010
My Device: Bold 9700
My Carrier: MTC touch

Re: SMS Pushregistry / How to invoke my app using sms and read it

Can anyone help pleaseeeeeeeeeeeeeeee??

BlackBerry Development Advisor
Posts: 15,814
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: SMS Pushregistry / How to invoke my app using sms and read it

There are some examples in the developers knowledge base that can help with this.

 

If you are creating a BlackBerry CLDC application you can listen for SMS messages as described here:

 

What is - Different ways to listen for SMS messages

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is...

 

If you are creating a MIDlet, here are some examples on the use of the PushRegistry.

 

How To - Create an auto-start MIDlet using the PushRegistry

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/How_To_-_Create_an_auto-start_MIDlet_us...

 

How To - Register a MIDlet with the PushRegistry

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/How_To_-_Register_a_MIDlet_with_the_Pus...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
New Contributor
Posts: 6
Registered: ‎03-09-2010
My Device: Bold 9700
My Carrier: MTC touch

Re: SMS Pushregistry / How to invoke my app using sms and read it

Dear ,

 

Thank you for your support;

Actually we tested many code for receiving incoming sms messages...
All are working fine; even if the application is already opened or closed.

 

In both cases; sms sent to a ceratin port is being received; and displayed on the screen.
The problem is that if u close the application; and you run it again (without receiving any new sms); it is giving us an error:
SMS port already open...

 Here is the code...
Can u please help us prevent this error...

Thank you in advance;

 

import java.io.IOException;
import java.util.Random;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.PushRegistry;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;

public class SMSPushRegistryDemo extends MIDlet {
   
    private static final int SMS_PORT = 9986;
    private static final String SMS_SERVER_URL = "sms://:" + SMS_PORT;
   
    private final Display _display;
    private final Form _mainScreen;
    private final Random _random;
   
    private final Command _sendSMSCommand;
    private final Command _unregisterCommand;
   
    private boolean _isInitialized;
    private MessageConnection _smsServerConnection;
    private final MyMessageListener _smsServerConnectionListener;
   
    private String _lastSendToPhoneNumber;
    private  Connection connection;
   
   
    public SMSPushRegistryDemo() {
        this._display = Display.getDisplay(this);
        this._smsServerConnectionListener = new MyMessageListener();
        this._random = new Random();
       
        this._sendSMSCommand = new Command("Send Test SMS", Command.SCREEN, 1);
        this._unregisterCommand = new Command("Unregister from PushRegistry", Command.SCREEN, 2);
        this._mainScreen = new Form("SMSPushRegistryDemo");
        this._mainScreen.addCommand(this._sendSMSCommand);
        this._mainScreen.addCommand(this._unregisterCommand);
        this._mainScreen.setCommandListener(new MyMainScreenCommandListener());
    }
   
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        try {
           
            connection.close();
            connection = null;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (this._smsServerConnection != null) {
            try {
                this._smsServerConnection.close();
                _smsServerConnection=null;
            } catch (Exception e) {
                // oh well
            }
        }
    }
   
    public void handleReceivedSMSMessage(MessageConnection connection) {
        Message message;
        try {
            message = connection.receive();
        } catch (Exception e) {
            this.logMessage("ERROR: MessageConnection.receive() threw " + e);
            return;
        }
       
        TextMessage textMessage;
        if (message instanceof TextMessage) {
            textMessage = (TextMessage) message;
        } else {
            this.logMessage("ERROR: Received SMS from " + message.getAddress()
            + " is not a TextMessage");
            return;
        }
       
        String address = textMessage.getAddress();
        String payload = textMessage.getPayloadText();
        this.logMessage("SMS Received from " + address + ": " + payload);
    }
   
    private void initialize() {
        boolean registerSuccessful;
        boolean alreadyRegistered;
        try {
            alreadyRegistered = !registerWithPushRegistry();
            registerSuccessful = true;
        } catch (Exception e) {
            this.logInitError("unable to register SMS connection with PushRegistry: " + e);
            registerSuccessful = false;
            alreadyRegistered = false;
        }
        if (registerSuccessful) {
            if (alreadyRegistered) {
                this.logMessage("Already registered with PushRegistry");
            } else {
                this.logMessage("Successfully registered with PushRegistry");
            }
        }
       
        MessageConnection smsServerConnection;
        try {
            smsServerConnection = openSMSServerConnection();
        } catch (Exception e) {
            this.logInitError("unable to open SMS server connection: " + e);
            smsServerConnection = null;
        }
        this._smsServerConnection = smsServerConnection;
       
        if (smsServerConnection != null) {
            this.logMessage("SMS server connection opened successfully on port " + SMS_PORT);
            try {
                smsServerConnection.setMessageListener(this._smsServerConnectionListener);
            } catch (Exception e) {
                this.logInitError("unable to set listener on MessageConnection: " + e);
            }
           
            this.receiveSMSMessagesOnStartup();
        }
    }
   
    private  MessageConnection openSMSServerConnection() throws IOException {
       connection = (MessageConnection) Connector.open(SMS_SERVER_URL,Connector.READ);
       
       // connection = Connector.open(SMS_SERVER_URL);
        if (connection instanceof MessageConnection) {
            return (MessageConnection) connection;
        } else if (connection == null) {
            throw new NullPointerException("Connector.open(\"" + SMS_SERVER_URL
                    + "\") returned null");
        } else {
            try {
                connection.close();
            } catch (Exception e) {
            }
            throw new ClassCastException("Connector.open(\"" + SMS_SERVER_URL
                    + "\") returned an instance of " + connection.getClass().getName()
                    + ", but expected " + MessageConnection.class.getName());
        }
    }
   
   
    private void logInitError(String message) {
        message = "ERROR: " + message;
        this.logMessage(message);
        Alert alert = new Alert("Initialization Error", message, null, AlertType.ERROR);
        alert.setTimeout(Alert.FOREVER);
        this._display.setCurrent(alert);
    }
   
    public void logMessage(String message) {
        this._mainScreen.append(message);
    }
   
    protected void pauseApp() {
    }
   
    private void receiveSMSMessagesOnStartup() {
        String[] availableURLs = PushRegistry.listConnections(false);
        int availableURLsLength = (availableURLs == null) ? 0 : availableURLs.length;
        for (int i = 0; i < availableURLsLength; i++) {
            if (SMS_SERVER_URL.equals(availableURLs[i])) {
                this._smsServerConnectionListener.notifyIncomingMessage(this._smsServerConnection);
            }
        }
    }
   
    public void sendTestSMS(String phoneNumber) {
        this._lastSendToPhoneNumber = phoneNumber;
       
        String url = "sms://" + phoneNumber + ":" + SMS_PORT;
        Connection con;
        try {
            con = Connector.open(url);
        } catch (Exception e) {
            this.logMessage("ERROR: Connector.open(\"" + url + "\") threw " + e);
            return;
        }
       
        try {
            int id = this._random.nextInt(10000);
            this.logMessage("Sending test SMS with ID " + id + " to " + url);
            MessageConnection mcon = (MessageConnection) con;
            TextMessage message = (TextMessage) mcon.newMessage(MessageConnection.TEXT_MESSAGE);
            message.setAddress(url);
            message.setPayloadText("Test SMS (" + id + ")");
            mcon.send(message);
            this.logMessage("SMS " + id + " sent successfully");
        } catch (Exception e) {
            this.logMessage("ERROR: unable to send SMS message to " + url + ": " + e);
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                // oh well
            }
        }
    }
   
    protected void startApp() throws MIDletStateChangeException {
        if (!this._isInitialized) {
            this._display.setCurrent(this._mainScreen);
            try {
                this.initialize();
            } finally {
                this._isInitialized = true;
            }
        }
    }
   
   
    private static boolean registerWithPushRegistry() throws IOException, ClassNotFoundException {
        String[] registeredURLs = PushRegistry.listConnections(false);
        int registeredURLsLength = (registeredURLs == null) ? 0 : registeredURLs.length;
       
        boolean isSMSURLRegistered = false;
        for (int i = 0; i < registeredURLsLength; i++) {
            if (SMS_SERVER_URL.equals(registeredURLs[i])) {
                isSMSURLRegistered = true;
                break;
            }
        }
       
        if (isSMSURLRegistered) {
            return false;
        } else {
            String name = SMSPushRegistryDemo.class.getName();
            PushRegistry.registerConnection(SMS_SERVER_URL, name, "*");
            return true;
        }
    }
   
    private static void unregisterFromPushRegistry() {
        PushRegistry.unregisterConnection(SMS_SERVER_URL);
    }
   
    private class MyMainScreenCommandListener implements CommandListener {
        public void commandAction(Command command, Displayable displayable) {
            if (command == SMSPushRegistryDemo.this._sendSMSCommand) {
                SendSMSScreen sendSMSScreen =
                        new SendSMSScreen(SMSPushRegistryDemo.this._lastSendToPhoneNumber);
                SMSPushRegistryDemo.this._display.setCurrent(sendSMSScreen);
            } else if (command == SMSPushRegistryDemo.this._unregisterCommand) {
                try {
                    unregisterFromPushRegistry();
                } catch (Exception e) {
                    SMSPushRegistryDemo.this
                            .logMessage("ERROR: unable to unregister from PushRegistry: " + e);
                }
                SMSPushRegistryDemo.this.logMessage("Successfully unregistered from PushRegistry");
            }
        }
    }
   
    private class MyMessageListener implements MessageListener {
        public void notifyIncomingMessage(final MessageConnection connection) {
            new Thread() {
                public void run() {
                    SMSPushRegistryDemo.this.handleReceivedSMSMessage(connection);
                }
            }.start();
        }
    }
   
    private class SendSMSScreen extends TextBox {
       
        private Command _sendCommand;
        private Command _cancelCommand;
       
        public SendSMSScreen(String initialPhoneNumber) {
            super("Enter phone number to which SMS will be sent", initialPhoneNumber, 99,
                    TextField.PHONENUMBER);
           
            this._sendCommand = new Command("Send SMS", Command.SCREEN, 1);
            this._cancelCommand = new Command("Cancel", Command.SCREEN, 2);
            this.addCommand(this._sendCommand);
            this.addCommand(this._cancelCommand);
            this.setCommandListener(new MyCommandListener());
        }
       
        private void popSelf() {
            SMSPushRegistryDemo.this._display.setCurrent(SMSPushRegistryDemo.this._mainScreen);
        }
       
        private void processCommand(Command command) {
            if (command == this._sendCommand) {
                String phoneNumber = this.getString();
                if (phoneNumber != null) {
                    phoneNumber = phoneNumber.trim();
                }
               
                if (phoneNumber == null || phoneNumber.length() == 0) {
                    Alert alert =
                            new Alert("ERROR", "A phone number must be specified", null,
                            AlertType.ERROR);
                    SMSPushRegistryDemo.this._display.setCurrent(alert);
                    return;
                }
               
                this.popSelf();
               
                final String destinationPhoneNumber = phoneNumber;
                new Thread() {
                    public void run() {
                        SMSPushRegistryDemo.this.sendTestSMS(destinationPhoneNumber);
                    }
                }.start();
            } else if (command == this._cancelCommand) {
                this.popSelf();
            }
        }
       
        private class MyCommandListener implements CommandListener {
            public void commandAction(Command command, Displayable displayable) {
                SendSMSScreen.this.processCommand(command);
            }
        }
    }
   
}

Highlighted
New Contributor
Posts: 7
Registered: ‎05-08-2012
My Device: blackberry 9930 simulator
My Carrier: blackberry

Re: SMS Pushregistry / How to invoke my app using sms and read it

how to use this midlet code in blackberry     idont now to to add this code add in pushScreen(newSMSPushRegistryDemo)

 

is in valid any one help???