Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 3.0 / 5
(3.0 / 5 - 2 votes cast)

This page was last modified 14:10, 21 April 2008.

SMS Operations

From Forum Nokia Wiki

This article illustrates various operations peformed on SMS.


Contents

Prerequisite

  • Download SmsHandler.zip from here: SmsHander.Zip
  • Extracting SmsHandler.zip will result into SmsHandler.h and SmsHandler.cpp
  • Copy-Paste SmsHandler.h into your project's /inc folder.
  • Copy-Paste SmsHandler.cpp into your project's /src folder.
  • Edit your .mmp file. Add an entry for SmsHandler.cpp in SOURCE directive.
SOURCE	SMSHandler.cpp


  • Edit your .mmp file. Add libraries for SMS handling.


//Libraries included for SMS support-
LIBRARY	msgs.lib smcm.lib gsmu.lib mtur.lib



  • Open your CYrApplicationContainer.h file.
  • Inlcude SmsHandler.h.


#include "SMSHandler.h" //Added for SMS Handling


  • Define an object of SmsHandler class.


private: //data
        .......
	.......
	CSmsHandler* iSmsHandler;



  • Open your CYrApplicationContainer.cpp file.
  • Initialize SmsHanlder object.


CYrApplicationContainer::ConstructL()......
{
.....
.....
    SetRect(aRect);
    ActivateL();
 
    iSmsHandler = CSmsHandler::NewL(); // SmsHandler 
}


Send message

  • Define one function of yr own SendMsg():


void CYrApplicationContainer ::SendMsg()
{   
    TBuf<128> SMSText,PhoneNumber;
    SMSText.Copy(_L("Test Message"));
    PhoneNumber.Copy(_L("999999999")); //Replace Number as per your needs
   
    iSmsHandler->SendL( PhoneNumber, SMSText) ;
}



Read Messaging folders

Inbox

Outbox

Draft

Sent Item

  • Following code snippet illustrate reading messages from Inbox Folder. KMsvGlobalInBoxIndexEntryId is used.
  • To read messages from Outbox Folder, use KMsvGlobalOutBoxIndexEntryId
  • To read messages from Draft Folder, use KMsvDraftEntryId
  • To read messages from Sent Item Folder, use KMsvSentEntryId


void CSmsHandler::ReadInbox()
{
	TBuf16<400> SMSContent; // To store the body part of SMS
 
	TMsvSelectionOrdering sort;
	sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
 
	CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,KMsvGlobalInBoxIndexEntryId,sort); // Reading Messages from Inbox Folder
	CleanupStack::PushL(inboxContext);
 
	CMsvEntrySelection* entries = inboxContext->ChildrenL();
	CleanupStack::PushL( entries );
 
	TInt msgCount= entries->Count();
	for (TInt i=0; i<entries->Count(); i++)
	{
 
		TMsvId entryID = entries->At(i);
		iSmsMtm->SwitchCurrentEntryL(entryID);
 
		CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
		CleanupStack::PushL(entry);
 
		TMsvEntry entry1 =  entry->Entry();
 
 		CMsvStore* inboxStore= entry->ReadStoreL();
		CleanupStack::PushL(inboxStore);
		if (inboxStore->HasBodyTextL())
		{
 
			TBufC<50> aText(entry1.iDetails); // Gives you phone Number
			TBuf16<20> msg;
			msg.Copy(aText);
	
			CRichText& richText= iSmsMtm->Body();
			inboxStore->RestoreBodyTextL(richText);
			const TInt length = richText.DocumentLength();
 
			SMSContent.Copy(richText.Read(0,length)); // Gives you actual content(Body) of SMS
			richText.Reset();
 
			TBuf8<400> SMSContent8;
			SMSContent8.Copy(SMSContent);
 
			WriteToFile(SMSContent8); // Write SMS Body in the SMSBody.txt file
		}
		else
		{
		// no text in SMS 
		}
		CleanupStack::PopAndDestroy(inboxStore);
		CleanupStack::PopAndDestroy(entry);
	}
	CleanupStack::PopAndDestroy(entries);
	CleanupStack::PopAndDestroy(inboxContext);
}


void CSmsHandler::WriteToFile(TBuf8<400> SMSContent8)
{
	_LIT(KFileSpec,"\\SMSBody.txt");//File, in which SMS Body will be stored	
	TInt iPos=0;
	RFs iFs;
	iFs.Connect();
	RFile iFile;
	TInt iErr=iFile.Open(iFs,KFileSpec,EFileWrite);
	if(iErr==KErrNone)
	{
		iFile.Seek(ESeekEnd,iPos);
		iFile.Write(SMSContent8);
		iFile.Close();
	}
	iFs.Close();
	//File closed
}



Read incoming message ( Online Mode )

Retrieve Message Body

Retrieve Phone Number

  • You will find MessageReceivedL() in the SMSHandler.cpp file.
  • Perform following changes to read incoming message body and phone number.


void CSmsHandler::MessageReceivedL( TMsvId aEntryId )
{
	CMsvEntry* serverEntry = iSession->GetEntryL( aEntryId );   // current entry
	CleanupStack::PushL( serverEntry );
	TMsvEntry entry = serverEntry->Entry(); // currently handled message entry
 
	entry.SetNew( ETrue );
	entry.SetUnread( ETrue );
	entry.SetVisible( ETrue );
 
	serverEntry->ChangeL( entry );  // commit changes
 
	//Added to retrieve message body
	const TDesC& descp = entry.iDescription; // iDescription will have only first 32 characters from the message
	TBuf8<40> MessageArrived;
	MessageArrived.Copy(descp);
 
	//Added to retrieve Phone Number of the Sender
	iSmsMtm->SwitchCurrentEntryL(aEntryId);
	iSmsMtm->LoadMessageL();
	CSmsHeader& header = iSmsMtm->SmsHeader();
 
	TPtrC from = header.FromAddress();
	const TDesC& phoneNumber = from;
	
	CleanupStack::PopAndDestroy( serverEntry );
}


Delete message from Sent Item ( Online Mode )

  • Add following case in the HandleSessionEventL() of SmsHandler.cpp
  • So when you send message from your application, it will be deleted from "Sent Item" folder.
  • Before this code would work, the message to be deleted need to have its iMtmData3 set in the ScheduleL()
case EMsvEntriesMoved:
{
	// Entry id is obtained from the session event arguments.
            TMsvId* entryId = STATIC_CAST( TMsvId*, aArg2 );
 
	// We are interested in messages that are moved to Sent Item Folder
	if ( *entryId == KMsvSentEntryId )
            {
		TMsvSelectionOrdering sort;
		sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
 
		CMsvEntry* parentEntry = CMsvEntry::NewL(*iSession, KMsvSentEntryId, sort);
		CleanupStack::PushL(parentEntry);
	
		CMsvEntrySelection* entries = parentEntry->ChildrenL();
		CleanupStack::PushL(entries);
		
		for(TInt i = 0; i < entries->Count(); i++)
		{
			 if( parentEntry->ChildDataL(entries->At(i)).iMtmData3 == KUidMsgTypeSMS.iUid )
			 {
				 parentEntry->DeleteL(entries->At(i));
				 break;
			 }
		}
		CleanupStack::PopAndDestroy( entries );
		CleanupStack::PopAndDestroy( parentEntry );
		}
		break;
	}
}



Delete messages from Messaging Folders

Inbox

Outbox

Draft

Sent Item

  • Following code snippet illustrates deleting messages from Inbox Folder. KMsvGlobalInBoxIndexEntryId is used.
  • To delete messages from Outbox Folder, use KMsvGlobalOutBoxIndexEntryId
  • To delete messages from Draft Folder, use KMsvDraftEntryId
  • To delete messages from Sent Item Folder, use KMsvSentEntryId


void CSmsHandler::DeleteMessages()
{
	TMsvSelectionOrdering sort;
	sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
 
	CMsvEntry* inboxContext=CMsvEntry::NewL(*iSession,KMsvGlobalInBoxIndexEntryId,sort);
	CleanupStack::PushL(inboxContext);
 
	CMsvEntrySelection* entries = inboxContext->ChildrenL();
	CleanupStack::PushL( entries );
 
	TInt msgCount= entries->Count();
	TInt i;
	for (i=0; i<entries->Count(); i++)
	{
		TMsvId entryID = entries->At(i);
		iSmsMtm->SwitchCurrentEntryL(entryID);
 
		CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
		CleanupStack::PushL(entry);
 
		entry->DeleteL(entries->At(i));
		CleanupStack::PopAndDestroy(entry);
	}
	CleanupStack::PopAndDestroy(entries);
	CleanupStack::PopAndDestroy(inboxContext);
}



Disable "Delivery Report"

  • Open SmsHandler.cpp and add SetDeliverReport = EFalse in CreateMsgL()


TBool CSmsHandler::CreateMsgL()
{
.....
.....
	settings->CopyL( iSmsMtm->ServiceSettings() );    // restore settings
    	settings->SetDelivery( ESmsDeliveryImmediately ); // to be delivered immediately
	settings->SetDeliveryReport(EFalse);// Delivery Report Disabled here
	header.SetSmsSettingsL( *settings );              // new settings
....
....
}

Receiving and deleting received delivery reports is illustrated in SMS DeliveryReport Deleting Example.


Send message to multiple reciepients

  • Open SmsHandler.cpp and set multiple numbers in AddAddresseeL() in CreateMsgL()


TBool CSmsHandler::CreateMsgL()
{
........
	// Recipient number is displayed also as the recipient alias.
	entry.iDetails.Set( iRecipientNumber );
 
	// Add addressee.
	TBuf<15> PhoneNumber2;
	PhoneNumber2.Copy(_L("9999999999")); //This is second number on which message will be sent
	iSmsMtm->AddAddresseeL( iRecipientNumber, entry.iDetails );
	iSmsMtm->AddAddresseeL( PhoneNumber2, entry.iDetails );
......
.......
}
 
Related Discussions
Thread Thread Starter Forum Replies Last Post
what is the suitable sdk ? lobna Mobile Java Tools & SDKs 0 2005-03-06 02:54
SMS from emulator passing to server... trimatnokia Mobile Java Networking & Messaging & Security 0 2006-04-24 15:17
Enumerating SMSs rps82 Multimodecards 1 2003-03-18 00:22
how to send sms faster? kepalle General Messaging 12 2007-04-06 07:38
concatenated sms egc_33 Mobile Java Networking & Messaging & Security 1 2007-07-14 15:47
 
Powered by MediaWiki