You Are Here:

Community: Wiki

This page was last modified 11:37, 31 May 2009.

SMS Operations

From Forum Nokia Wiki

This article illustrates various operations performed on SMS.


Contents

Prerequisite

  • Download SmsHandler.zip as follows:
  • 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.
  • Include 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 your 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 illustrates 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()
{
        HBufC* SMSContent = HBufC::NewLC(400);
	HBufC8* SMSContent8 = HBufC8::NewLC(400);
 
	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);
 
		CMsvStore* inboxStore= entry->ReadStoreL();
		CleanupStack::PushL(inboxStore);
		if (inboxStore->HasBodyTextL())
		{
                        TMsvEntry entry1 =  entry->Entry();
			TBufC<50> aText(entry1.iDetails);//Gives you phone Number or Contact Name if Contact is present 			TBuf16<20> msg;
			msg.Copy(aText);
                     
                         // If you want to get the Recipient Number, when iDetails gives you Contact's Name.
                        iSmsMtm->LoadMessageL();
	                CSmsHeader& smsHeader = iSmsMtm->SmsHeader();
	                const CArrayPtrFlat<CSmsNumber>&  array= smsHeader.Recipients();
	                CSmsNumber* smsNumber = array.At(0);
	                TPtrC RecipentNumber = smsNumber->Address();
                     // RecipentNumber will containt the recipent Number
	             
 
	
			CRichText& richText= iSmsMtm->Body();
			inboxStore->RestoreBodyTextL(richText);
			const TInt length = richText.DocumentLength();
 
			SMSContent->Des().Copy(richText.Read(0,length)); // Gives you actual content(Body) of SMS
			richText.Reset();
 
			SMSContent8->Des().Copy(SMSContent->Des());
 
			WriteToFile(SMSContent8->Des()); // Write SMS Body in the SMSBody.txt file
		}
		else
		{
		// no text in SMS 
		}
		CleanupStack::PopAndDestroy(2,entry);
	}
	CleanupStack::PopAndDestroy(4,SMSContent);
}


//Note that "SMSBody.txt" used in the following code must be created beforehand as we are using Open function from RFile API
void CSmsHandler::WriteToFile(const TPtrC8& aSMSContent8)
{
	_LIT(KFileSpec,"\\SMSBody.txt");//File, in which SMS Body will be stored	
	TInt pos=0;
	RFs fs;
	fs.Connect();
	RFile file;
	TInt err=file.Open(fs,KFileSpec,EFileWrite);
	if(err==KErrNone)
	{
		file.Seek(ESeekEnd,pos);
		file.Write(aSMSContent8);
		file.Close();
	}
	fs.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 the following case in the HandleSessionEventL() of SmsHandler.cpp
  • So when you send message from your application, it will be deleted from "Sent Item" folder.
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.SetSorting(EMsvSortByDateReverse);
		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 Links:

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditFurlTechnocratiMagnoliaTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fSendingE5fSMSE5fwithE5fE52SendAsX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxSendingE20SMSE20withE20E52SendAsE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfnTypeZCommunityContentQ qdcZtypeQUqfnTypeZE52esourceQ qdcZtypeQUqfnTypeZWebpageQ qdcZtypeQUqfnTypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZmessagingQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZsmsQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZE52esourceQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxmessagingX qfnZuserE5ftagQSxsmsX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfnTypeZCommunityContentQ qrdfZtypeQUqfnTypeZE52esourceQ qrdfZtypeQUqfnTypeZWebpageQ qrdfZtypeQUqfnTypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X