0

Is there a way to extract the content of a newly received message and place it in a string variable when the MessageReceived event is triggered?

Dustin Catap
  • 411
  • 5
  • 11

1 Answers1

0

it's so simple just add method like this:

private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
    var obj = e.IndicationObject;
    if (obj is MemoryLocation)
    {
        var loc = (MemoryLocation)obj;
        var msg = string.Format("New message received in storage \"{0}\", index {1}.",
                                loc.Storage, loc.Index);
        MessageBox.Show(msg);

        DecodedShortMessage[] messages = CommSetting.comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
        foreach (DecodedShortMessage message in messages)
        {
            DisplayMessage(message.Data);
        }
        return;
    }
}

private void DisplayMessage(SmsPdu pdu)
{
    if (pdu is SmsDeliverPdu)
    {
        SmsDeliverPdu data = (SmsDeliverPdu)pdu;
        var phoneNumber = data.OriginatingAddress; 
        var msg = data.UserDataText;
        var date = string.Format("{0:dd/MM/yyyy}", data.SCTimestamp.ToDateTime());
        var time = string.Format("{0:HH:mm:ss}", data.SCTimestamp.ToDateTime());

        //read message in listBox1
        listBox1.Items.Add(string.Format("{0}, {1}, {2}, {3}", date, time, phoneNumber, msg));
    }
}

but don't forget register this event when connection is open:

comm.MessageReceived += new MessageReceivedEventHandler(comm_MessageReceived);

i hope this help :D

aminvincent
  • 553
  • 1
  • 12
  • 43
  • Tried to implement this.. What is "CommSetting" here? It is showing me error –  Dec 10 '18 at 08:45
  • `CommSetting` is class for setup connection to gsm comm library. go to this link and download sample project [link](http://www.scampers.org/steve/sms/libraries.htm) – aminvincent Dec 11 '18 at 09:10
  • `class CommSetting { public static string Comm_Port = ""; public static Int64 Comm_BaudRate = 0; public static Int64 Comm_TimeOut = 0; public static GsmCommMain comm; public CommSetting() { // // TODO: Add constructor logic here // } }` – aminvincent Dec 11 '18 at 09:12