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
New Developer
Posts: 11
Registered: ‎01-18-2013
My Device: Z10LE
My Carrier: VIPnet Croatia

Re: Sending SMS example code

Yes! Below You will find my full cpp file pasted as a code, but if You need more I have created a fully working example app which sends SMS and I also wrote a full tutorial for it on my blog here: http://www.zvjer.com/blog/?p=501

 

// Default empty project template
#include "SMSexample.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include <bb/system/SystemToast>

#include <bb/pim/message/ConversationBuilder>
#include <bb/pim/message/MessageBuilder>
#include <bb/pim/message/MessageService>
#include <bb/pim/message/MessageContact>
#include <bb/pim/account/AccountService>

#include <bb/cascades/Page>
#include <QSettings>

using namespace bb::cascades;
using namespace bb::pim::account;

SMSexample::SMSexample(bb::cascades::Application *app)
: QObject(app)
{
    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("_smsexample", this);

    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    // set created root object as a scene
    app->setScene(root);
}

// Following lines are for SMS sending (all of them)
void SMSexample::sendSMS(QString regID, QString contactNumber) {
	bb::pim::message::MessageService messageService;
	bb::pim::account::AccountService accountService;

	//Get the SMS/MMS account
	QList<Account> accountList = accountService.accounts(Service::Messages,"sms-mms");
	AccountKey accountId = accountList.first().id();

	// Create a contact to whom you want to send sms/mms.
	int contactKey = -1;
	bb::pim::message::MessageContact recipient = bb::pim::message::MessageContact(contactKey,bb::pim::message::MessageContact::To,contactNumber,contactNumber);

	//Create a conversation  because sms/mms chats most of the time is a conversation
    bb::pim::message::ConversationBuilder* conversationBuilder = bb::pim::message::ConversationBuilder::create();
    conversationBuilder->accountId(accountId);
    QList<bb::pim::message::MessageContact>participants;
    participants.append(recipient);
    conversationBuilder->participants(participants);
    bb::pim::message::Conversation conversation = *conversationBuilder;
    bb::pim::message::ConversationKey conversationId = messageService.save(accountId,conversation);

	//Create a message Builder for sms/mms
	bb::pim::message::MessageBuilder* messageBuilder = bb::pim::message::MessageBuilder::create(accountId);
	messageBuilder->addRecipient(recipient);
	// SMS API's handle body as an attachment.
	QString body = regID;
	messageBuilder->addAttachment(bb::pim::message::Attachment("text/plain", "body.txt", body));
	messageBuilder->conversationId(conversationId);
	bb::pim::message::Message message;
	message = *messageBuilder;

	//Sending SMS/MMS
	//messageService.send(accountId, message);
	bb::pim::message::MessageKey key = messageService.send(accountId, message);

	showSuccessMessage();
}

void SMSexample::showSuccessMessage() {
	using namespace bb::system;

	SystemToast *toast = new SystemToast(this);

    toast->setBody("SMS sending success!");
    toast->show();
}

 

Contributor
Posts: 30
Registered: ‎02-05-2013
My Device: Dev Alpha B
My Carrier: maxis

Re: Sending SMS example code

Thanks, really appreciate it.

New Developer
Posts: 11
Registered: ‎01-18-2013
My Device: Z10LE
My Carrier: VIPnet Croatia

Re: Sending SMS example code

let me know how it works for you, if you want.
Contributor
Posts: 30
Registered: ‎02-05-2013
My Device: Dev Alpha B
My Carrier: maxis

Re: Sending SMS example code

It works great thanks. I didn't put LIBS += -lbbpim inside .pro before.

New Contributor
Posts: 7
Registered: ‎12-20-2011
My Device: all
My Carrier: a lot's of them

Re: Sending SMS example code

Hi,

 

Thanks for this piece of code, but is there any way to retrieve the status of the SMS like the interface 'DatagramStatusListener' (http://docs.blackberry.com/ko-kr/developers/deliverables/6022/net/rim/device/api/io/DatagramStatusLi... was doing with the "old" BlackBerry ?

 

Being able to tell if the SMS has been successfuly sent, or not (and in this case why) is a pre-requisite for my project.

The only related class I was able to find in the Cascade documentation is the class 'SmsTransportSendResult' (http://developer.blackberry.com/cascades/reference/bb__pim__message__smstransportsendresult.html) but it doesn't match my need.

 

Thanks !

Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Sending SMS example code

Hello 

 

can we set profile picture or name while sending the sms?

 

Contributor
Posts: 13
Registered: ‎10-05-2012
My Device: BB 10 dev Alpha
My Carrier: BSNL

Re: Sending SMS example code

Thanks for sharing code it is working but I have little problem when you sending message each time you  create conversation and that  not good for SMS every SMS will put in new conversation is there any  way to find existing conversation and send SMS along with that conversion. 

 

Thanks in advance!!!!!!!!

Developer
Posts: 310
Registered: ‎04-08-2013
My Device: Z10
My Carrier: Virgin Canada

Re: Sending SMS example code

Thanks for the code, it worked like a charm!
New Developer
Posts: 22
Registered: ‎09-09-2014
My Device: BB 9900 Bold
My Carrier: T-Mobile Czech republic

Re: Sending SMS example code

Hello, I try to create send-SMS part of my app with using code examples above. I have only one problem:

 

on line

 

QList <accounts> AccountList = accountService.accounts(Service::Messages,"sms-mms");

 

compiler reports errors:

 

../src/quotesapp.cpp:112:12: error: 'accounts' was not declared in this scope
../src/quotesapp.cpp:112:20: error: template argument 1 is invalid
../src/quotesapp.cpp:112:34: error: invalid type in declaration before '=' token

 

In what scope is type <accounts. declared please ?

 

Thank you for any advice.

 

Thank you.

 

 

Developer
Posts: 310
Registered: ‎04-08-2013
My Device: Z10
My Carrier: Virgin Canada

Re: Sending SMS example code

[ Edited ]

You've made a typo, it should be :


QList<Account> ...