You Are Here:

Community: Wiki

This page was last modified on 8 September 2009, at 16:57.

SMS Receiver

From Forum Nokia Wiki

Reviewer Approved   

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:

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 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fMessageE5fhandlingE5finE5fJavaE5fPlatfromE2cE5fMicroE5fE45ditionE5fE28JavaE5fME45E29X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZmessagingQ qfnZtopicQUqfnTopicZsmsQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZuserE5ftagQSxmessagingX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx3E2E0000X