×
Namespaces

Variants
Actions
Revision as of 02:03, 1 April 2009 by savaj (Talk | contribs)

SMS Receiver

From Nokia Developer Wiki
Jump to: navigation, search

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.

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 SMS DeliveryReport Deleting Example.

Related Links:

144 page views in the last 30 days.
×