Search code examples
javasmslib

SMS not received with java smslib library


i try with this, but it is some times not working correctly..i used a while loop for loop the code. can i add some listner for this? any one can give me the correct answer for this? in need to get responce real time

 while (true) {
            msgList = new ArrayList<InboundMessage>();
            Service.getInstance().readMessages(msgList, InboundMessage.MessageClasses.ALL);
            for (InboundMessage im : msgList) {

                if (last < im.getMemIndex()) {
                    ResultSet rs = DB.getConnection().createStatement().executeQuery("Select * From codes where code='" + im.getText() + "'");
                    if (rs.next()) {
                        ResultSet rs2 = DB.getConnection().createStatement().executeQuery("Select * From sms_log where code='" + im.getText() + "' AND tel_no='" + im.getOriginator() + "'");
                        if (rs2.next()) {
                                if (m == null) {
                                    m = new SMSClient(1);
                                }
                                m.sendMessage(im.getOriginator(), "The Code is Already Sent... Thank You!.");

                            System.out.println("The Code is Already Sent... Thank You!.");
                        } else {
                            System.out.println("The Code Verified... Thank You!.");
                            if (m == null) {
                                m = new SMSClient(1);
                            }

                            m.sendMessage(im.getOriginator(), "The Code Verified... Thank You!.");
                            DB.getConnection().createStatement().execute("INSERT INTO sms_log (tel_no,code,status) values('" + im.getOriginator() + "','" + im.getText() + "',1)");

                        }
                    } else {
                        if (m == null) {
                            m = new SMSClient(1);
                        }
                        m.sendMessage(im.getOriginator(), "Invalid Code... Thank You!.");
                        System.out.println("Invalid Code... Thank You!.");
                    }

                }
            }
            Thread.sleep(10000);
            System.out.println("start");

        }

Solution

  • I think IInboundMessageNotification is the interface you are looking for

    public class InboundNotification implements IInboundMessageNotification {
    
        @Override
        public void process(AGateway aGateway, Message.MessageTypes messageTypes, InboundMessage inboundMessage) {
        //add you logic for received messages here 
        }
    }

    Add notification class to smsLib service

    Service.getInstance().setInboundMessageNotification(new InboundNotification())

    From now on, process() method will be called every time your modem receives a message.

    As far as I remember, smslib (version 3.5.x) does not delete received messages so it needs to be done manually

    @Override
    public void process(AGateway aGateway, Message.MessageTypes messageTypes, InboundMessage inboundMessage) {
       try {
             aGateway.deleteMessage(inboundMessage);
           } catch (TimeoutException | GatewayException | InterruptedException | IOException e) {
                e.printStackTrace();
           }
       // your logic here
    }

    otherwise you will keep receiving not deleted messages every time you receive a new one.

    Hope you will find this useful.