You Are Here:

Community: Wiki

This page was last modified on 11 November 2009, at 06:50.

SMS Operations

From Forum Nokia Wiki

Reviewer Approved   

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, and capabilities required.
//Libraries included for SMS support-
LIBRARY msgs.lib smcm.lib gsmu.lib mtur.lib
CAPABILITY ReadUserData WriteUserData NetworkServices



  • 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& header = iSmsMtm->SmsHeader();
TPtrC from = header.FromAddress(); // This will give you actual phone number irrespective of the name of contact
 
// To read phone number from Sent Item folder, see the Note below
 
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: To read phone number from the message in the Sent Item folder, use the following code snippet.

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
//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 message from Outbox folder ( Online Mode )

  • Sometimes a need arises to delete message from the outbox folder so that it cannot be scheduled to be sent.
  • Add the following case in the HandleSessionEventL() of SmsHandler.cpp
  • So when you send message from your application, it will be deleted from "Outbox" folder.
  • A phone number and the message contents can be retrieved before deleting the message which is shown in the following code snippet.
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 == KMsvGlobalOutBoxIndexEntryId)
{
TMsvSelectionOrdering sort;
sort.SetSorting(EMsvSortByDateReverse);
sort.SetShowInvisibleEntries(ETrue); // we want to handle also the invisible entries
 
CMsvEntry* parentEntry = CMsvEntry::NewL(*iSession,
KMsvGlobalOutBoxIndexEntryId, 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)
{
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
iSmsMtm->LoadMessageL();
 
CSmsHeader& smsHeader = iSmsMtm->SmsHeader();
const CArrayPtrFlat<CSmsNumber>& array= smsHeader.Recipients();
CSmsNumber* smsNumber = array.At(0);
TPtrC recipentNumber = smsNumber->Address(); // Gives actual phone number, instead of giving mapped name from phonebook
 
CRichText& richText= iSmsMtm->Body();
TInt length = richText.DocumentLength();
HBufC* SMSContent = HBufC::NewLC(length);
SMSContent->Des().Copy(richText.Read(0,length)); // Gives you actual content(Body) of SMS
richText.Reset();
// Use SMSContent in your function
CleanupStack::PopAndDestroy(SMSContent);
parentEntry->DeleteL(entries->At(i));
break;
}
}
CleanupStack::PopAndDestroy(entries);
CleanupStack::PopAndDestroy(parentEntry);
}
break;
}

Note: As the message is already deleted before it gets scheduled for actual sending, the code will leave in the ScheduleL() function. To prevent showing the error code on the actual device, use TRAP_IGNORE macro around InvokeAsyncFunctionL call as shown in the following code.

TRAP_IGNORE(iOperation = iSmsMtm->InvokeAsyncFunctionL( ESmsMtmCommandScheduleCopy,
*selection, dummyParams, iStatus ));


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 < msgCount; i++)
{
TMsvId entryID = entries->At(i);
iSmsMtm->SwitchCurrentEntryL(entryID);
 
CMsvEntry* entry= iSession->GetEntryL((*entries)[i]);
CleanupStack::PushL(entry);
 
entry->DeleteL(entryID);
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:

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
User Rating: qfnZuserE5FratingQNx4E2E6667X
RDF Facets: qfnZuserE5FtagQSxsmsE20operationgE20goodE20refX