Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

SMS Receiver

From Wiki
Jump to: navigation, search

The CSMSReceiver example illustrates how you can wait incoming SMS message and extract information from them as soon as they arrive into the inbox. After constructing the iSession inside the ConstructL()-function, the HandleSessionEventL()-function will be called by the OS to notify your class on CMsvSession events.

Article Metadata
Code ExampleArticle
Created: symbianyucca (30 Mar 2007)
Last edited: hamishwillee (14 Jun 2013)

As shown in the example, inside this function only EMsvEntriesCreated and EMsvEntriesMoved are checked which after the code checks if the event happened for the inbox, which after the HandleEntryL()-function is used to handle individual SMS messages. You could use all functions defined for the CBaseMtm to extract information of the message (and you could also cast the CBaseMtm to CSmsClientMtm for SMS specific functionality), with this example only the body of the message is forwarded to the callback class.

SMS_Receiver.cpp

CSMSReceiver* CSMSReceiver::NewL(MSMSRecCallBack& aObserver)
{
CSMSReceiver* self = new(ELeave)CSMSReceiver(aObserver);
self->ConstructL();
return self;
}
 
CSMSReceiver::CSMSReceiver(MSMSRecCallBack& aObserver):iObserver(aObserver)
{
}
 
CSMSReceiver::~CSMSReceiver()
{
delete iMtmRegistry;
iMtmRegistry = NULL;
 
delete iSession;
iSession = NULL;
}
 
void CSMSReceiver::ConstructL(void)
{
iSession = CMsvSession::OpenSyncL(*this);
iMtmRegistry = CClientMtmRegistry::NewL(*iSession);
}
 
 
void CSMSReceiver::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
{
switch (aEvent)
{
case EMsvEntriesChanged:
{
 
}
break;
case EMsvEntriesCreated:
case EMsvEntriesMoved:
{
TMsvId* entryId;
entryId = static_cast<TMsvId*>(aArg2); // entry id from the session event
 
if ( *entryId == KMsvGlobalInBoxIndexEntryId ) // new entry has been created in Inbox folder
{
TMsvSelectionOrdering sort;
sort.SetShowInvisibleEntries(EFalse); // we dont want to handle the invisible entries
// Take a handle to the Inbox entry
CMsvEntry* parentEntry = CMsvEntry::NewL(*iSession, KMsvGlobalInBoxIndexEntryId, sort);
CleanupStack::PushL(parentEntry);
 
CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);
if(entries)
{
//Process each created entry, one at a time.
for(TInt i = 0; i < entries->Count(); i++)
{
HandleEntryL(entries->At(i));
}
}
 
CleanupStack::PopAndDestroy(1); // parentEntry
}
}
break;
case EMsvCloseSession:
iSession->CloseMessageServer();
break;
default:
// All other events are ignored
break;
}
}
 
void CSMSReceiver::HandleEntryL(TMsvId& aEntId)
{
CMsvEntry* entry = iSession->GetEntryL(aEntId);
CleanupStack::PushL(entry);
 
if(entry->Entry().iMtm == KUidMsgTypeSMS)
{
CBaseMtm* SmsMtm = iMtmRegistry->NewMtmL(KUidMsgTypeSMS);
if(SmsMtm)
{
CleanupStack::Pop(1);//entry
CleanupStack::PushL(SmsMtm);
SmsMtm->SetCurrentEntryL(entry);
SmsMtm->LoadMessageL();
 
HBufC* BodyBuffer = HBufC::NewLC(SmsMtm->Body().DocumentLength());
TPtr BodyPoint(BodyBuffer->Des());
 
SmsMtm->Body().Extract(BodyPoint,0,SmsMtm->Body().DocumentLength());
 
iObserver.GotSMSMessageL(*BodyBuffer);
 
CleanupStack::PopAndDestroy(2);//SmsMtm,BodyBuffer
}
else
{
CleanupStack::PopAndDestroy(1);//entry
}
}
else
{
CleanupStack::PopAndDestroy(1);//entry
}
}

SMS_Receiver.h

class MSMSRecCallBack 
{
public:
virtual void GotSMSMessageL(const TDesC& aMessage) = 0;
};
 
 
class CSMSReceiver : public CBase, MMsvSessionObserver
{
 
public:
static CSMSReceiver* NewL(MSMSRecCallBack& aObserver);
~CSMSReceiver();
protected:
CSMSReceiver(MSMSRecCallBack& aObserver);
void ConstructL(void);
void HandleSessionEventL(TMsvSessionEvent aEvent, TAny *aArg1, TAny *aArg2, TAny *aArg3);
private:
void HandleEntryL(TMsvId& aEntId);
private:
MSMSRecCallBack& iObserver;
CMsvSession* iSession;
CClientMtmRegistry* iMtmRegistry;
};

If you are looking for a full example application for this functionality, you could have a look into an example for monitoring and deleting log entries for SMS messages, especially for Delivery reports is also illustrated in File:SMS DeliveryReport Deleting.zip Example.

Related Links:

This page was last modified on 14 June 2013, at 06:29.
376 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×