Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 123
Registered: ‎09-18-2013
My Device: Dev Alpha B
My Carrier: BSNL INDIA
Accepted Solution

Send an SMS from my app

Hi all,

I want to send an SMS from my app. I don't want to use the composer while using the invocation method. So I want to send an SMS through my custom interface, without using invocation ofcourse. How can I do that?

Highlighted
Developer
Posts: 28
Registered: ‎11-07-2013
My Device: 9790
My Carrier: VietNam

Re: Send an SMS from my app

i think you need it.

 

void ApplicationUI::sendSMS(QString phoneNumber, QString messageText) {
if (phoneNumber.length() == 0) {
logToConsole("Blank is invalid mobile phone number");
return;
}
bb:Smiley Tongueim::account::AccountService accountService;
QList<Account> accountList = accountService.accounts(Service::Messages, "sms-mms");

bb:Smiley Tongueim::account::AccountKey smsAccountId = 0;
if (!accountList.isEmpty()) {
smsAccountId = accountList.first().id();
qDebug() << "SMS-MMS account ID:" << smsAccountId;
}
else {
qWarning() << "Could not find SMS account";
return;
}

bb:Smiley Tongueim::message::MessageService messageService;
QList<MessageContact> participants;
bb:Smiley Tongueim::message::MessageContact recipient = bb:Smiley Tongueim::message::MessageContact(-1, bb:Smiley Tongueim::message::MessageContact::To, phoneNumber, phoneNumber);
participants.append(recipient);

bb:Smiley Tongueim::message::ConversationBuilder *conversationBuilder = bb:Smiley Tongueim::message::ConversationBuilder::create();
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);

bb:Smiley Tongueim::message::Conversation conversation = *conversationBuilder;
bb:Smiley Tongueim::message::ConversationKey conversationId = messageService.save(smsAccountId, conversation);

bb:Smiley Tongueim::message::MessageBuilder *builder = bb:Smiley Tongueim::message::MessageBuilder::create(smsAccountId);
builder->conversationId(conversationId);

builder->addAttachment(bb:Smiley Tongueim::message::Attachment("text/plain", "", messageText.toUtf8()));

foreach(const bb:Smiley Tongueim::message::MessageContact recipient, participants)
{
builder->addRecipient(recipient);
}

bb:Smiley Tongueim::message::Message message = *builder;

messageService.send(smsAccountId, message);

delete builder;
delete conversationBuilder;

}

Developer
Posts: 123
Registered: ‎09-18-2013
My Device: Dev Alpha B
My Carrier: BSNL INDIA

Re: Send an SMS from my app

Hi, thanks. The code works. I was wondering though, if we initialize a pointer using a builder, do we need to delete it after use?
Developer
Posts: 28
Registered: ‎11-07-2013
My Device: 9790
My Carrier: VietNam

Re: Send an SMS from my app

[ Edited ]
void Service::sendSMS(QString phoneNumber, QString messageText) {
     if (phoneNumber.length() == 0) {
          return;
     }
     
     bb::pim::message::SmsTransport smsTransport;

     bb::pim::message::MessageBuilder *builder =                    smsTransport.createMessageBuilder();

     builder->addAttachment(bb::pim::message::Attachment("text/plain", "",messageText.toUtf8()));

     bb::pim::message::MessageContact contact(-1,      bb::pim::message::MessageContact::To, phoneNumber, phoneNumber);
builder->addRecipient(contact);

     bb::pim::message::Message message = *builder;
     smsTransport.send(16020, message);

     delete builder;

}

 

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Send an SMS from my app

@AricLam

 

Please use the 'Insert Code' button when including code in your posting, this is the clipboard icon with a 'C' in it. This way your code will be easier to read.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 28
Registered: ‎11-07-2013
My Device: 9790
My Carrier: VietNam

Re: Send an SMS from my app

sorry @BBSJdev

i'm new member. i don't something. i user it for next. Thank @BBSJdev
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Send an SMS from my app

No problem, as a new member you might not know that you can also go back and edit your posts just choose the Options menu in the top-right of the posting.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 28
Registered: ‎11-07-2013
My Device: 9790
My Carrier: VietNam

Re: Send an SMS from my app

I do, but can't  #2. thanks

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Send an SMS from my app

Try it now.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 28
Registered: ‎11-07-2013
My Device: 9790
My Carrier: VietNam

Re: Send an SMS from my app

Not function "Edit Reply". 

Capture.PNG