Search code examples
javasmslib

SMSlib receiving duplicate messages


I am trying to receive sms on computer using D-Link USB Modem. I have find out solution of my problem on this link But now issue i am facing is that i am receiving same message 3 times, like this

New Inbound message detected from Gateway: 923145663675 Hello
New Inbound message detected from Gateway: 923145663675 Hello
New Inbound message detected from Gateway: 923145663675 Hello

Also if program remains open for long time then these above given lines will be printed on screen again and again I did search on google a lot and some where a suggestion i found to remove unused notification, i have done that but still duplicate messages are being received. Code is given below

public void doIt() throws Exception{                
   InboundNotification inboundNotification = new InboundNotification();

   try{

     SerialModemGateway gateway = new SerialModemGateway("modem.com4", "COM7", 921600, "", "");

     gateway.setProtocol(Protocols.PDU); 
     gateway.setInbound(true); 
     gateway.setSimPin("0000"); 
     Service.getInstance().setInboundMessageNotification(inboundNotification);
     Service.getInstance().addGateway(gateway);
     Service.getInstance().startService();

     System.out.println("Now Sleeping - Hit <enter> to stop service.");
     System.in.read();
     System.in.read();
  }catch (Exception e){
      e.printStackTrace();
  }finally{
      Service.getInstance().stopService();
  }
}

public class InboundNotification implements IInboundMessageNotification{
      public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg){

        if (msgType == MessageTypes.INBOUND) {                     
              System.out.println("New Inbound message detected from Gateway: " + msg.getOriginator() + " " + msg.getText());

                try {
                    gateway.deleteMessage(msg);
                } catch (GatewayException ex) {
                    Logger.getLogger(ReadMessages.class.getName()).log(Level.SEVERE, null, ex); 
                }

        }
    }
}

Solution

  • Once I was also facing same problem, whereas my requirements were to keep the record of all incoming messages. So, I create table in database and inserted all incoming messages in database with complete details, for example sender number, message content also date and time. And in database I made unique records with the combination of date, time and message content.

    Now, if I will receive duplicate message, then for sure date, time and message content will be same. And after that when these values will be inserted in database, duplicate values will not be inserted in database. As a result we will have unique data.

    But this might not works for you, because in actual fact you do not receive message multiple times, you just receive one message and later on notification shown to you multiple times. Because if you will receive message multiple times then date and time must be different all the time. But in my case these were same always. So, first you should try to delete all message from inbox and then maybe you need to flush receiving port.