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
Zvjer
Posts: 10
Registered: ‎01-18-2013
My Device: Z10LE
My Carrier: VIPnet Croatia
Accepted Solution

Sending SMS example code

Hello all!

 

I have found this documentation that might help me send an ordinary SMS from my app:

https://developer.blackberry.com/cascades/reference/bb__pim__message__messageservice.html

but some example code is not available there which would prove very helpful.

 

My app only needs to SMS a few alphanumeric characters given from a variable in cascades, so that's all I need at this moment. One code example please.

 

Thanks a lot!

Please use plain text.
BlackBerry Development Advisor
PBernhardt
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: Sending SMS example code

SMS actually uses the same API as other messages (like email). The key difference is that you want to pick the SMS account specifically, and you probably want to build is as part of a conversation.

 

Try something like this:

                MessageService messageService;
                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. Put the right phone number in yourself
                int contactKey   = -1;
                MessageContact recipient = MessageContact(contactKey, MessageContact::To,"5555555555", "5555555555");

                //Create a conversation  because sms/mms chats most of the time is a conversation
                ConversationBuilder* conversationBuilder = ConversationBuilder::create();
                conversationBuilder->accountId(accountId);
                QList<MessageContact> participants;

                participants.append(recipient);

                conversationBuilder->participants(participants);

                Conversation conversation = *conversationBuilder;
                ConversationKey conversationId = messageService.save(accountId, conversation);

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

                //Sending SMS/MMS
                MessageKey key = messageService.send(accountId, message);
                qDebug() << "+++++++ Message sent" << endl;

 

 

 

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Please use plain text.
New Developer
Zvjer
Posts: 10
Registered: ‎01-18-2013
My Device: Z10LE
My Carrier: VIPnet Croatia

Re: Sending SMS example code

Thanks a lot for this Paul!
Please use plain text.
New Developer
Zvjer
Posts: 10
Registered: ‎01-18-2013
My Device: Z10LE
My Carrier: VIPnet Croatia

Re: Sending SMS example code

Paul, I invited You to BBM via Your QR avatar.Could You test my app for me? Only sms sending, because Alpha device cannot send SMS and I'm working very blindly by not being able to look at console.log while (You?) send sms. Please accept me on bbm :smileyhappy:

Please use plain text.
Contributor
paulgee31
Posts: 24
Registered: ‎06-14-2011
My Device: Bold 9870
My Carrier: O2

Re: Sending SMS example code

When checking incoming SMS does the mimeType of the message determine whether the SMS is binary or ASCII?

Please use plain text.
New Developer
Zvjer
Posts: 10
Registered: ‎01-18-2013
My Device: Z10LE
My Carrier: VIPnet Croatia

Re: Sending SMS example code

Paul hi again! I tried Your code and waited for this latest dev-alpha update to test it but it does not work. I cannot be sure if dev-alpha has a problem sending SMS via this SMS-sending code, but I believe something is not right with the code although I can call it with following command from QML and it executes without errors. But it does not send any message! Here is the QML call:

 

zgparking.sendSMS(label1.text + label2.text + label3.text, "my_test_number");

 ...so the first QML parameter is supposed to be SMS body, and second one is ofcourse destination number which is currently my phone number.

 

#include "ZGParking2.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;

ZGParking2::ZGParking2(bb::cascades::Application *app)
: QObject(app)
{
    // persistant store data
    QCoreApplication::setOrganizationName("JerkoCilas");
    QCoreApplication::setApplicationName("ZGParking");

    // 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("zgparking", this);

    // qDebug() << "+++++++ Launch..." << endl;

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

QString ZGParking2::getValueFor(const QString &objectName, const QString &defaultValue)
{
    QSettings settings;

    // If no value has been saved, return the default value.
    if (settings.value(objectName).isNull()) {
        return defaultValue;
    }

    // Otherwise, return the value stored in the settings object.
    return settings.value(objectName).toString();
}

void ZGParking2::saveValueFor(const QString &objectName, const QString &inputValue)
{
    // A new value is saved to the application settings object.
    QSettings settings;
    settings.setValue(objectName, QVariant(inputValue));
}

void ZGParking2::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. Put the right phone number in yourself
	int contactKey = -1;
	bb::pim::message::MessageContact recipient = bb::pim::message::MessageContact(contactKey, bb::pim::message::MessageContact::To, contactNumber, contactNumber);

	//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));
	bb::pim::message::Message message;
	message = *messageBuilder;

	//Sending SMS/MMS
	messageService.send(accountId, message);

	showSuccessMessage();
}

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

	SystemToast *toast = new SystemToast(this);

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

 What do You think? Can You see why this would not send a message? If You believe it may be an error in SDK I can send You my whole app-code for You to test it, I don't mind as long as we get it working. Thanks a lot!

Please use plain text.
New Developer
Zvjer
Posts: 10
Registered: ‎01-18-2013
My Device: Z10LE
My Carrier: VIPnet Croatia

Re: Sending SMS example code

OK, no need to check anything. I got it working! Thanks again!
Please use plain text.
Developer
StevenKader
Posts: 635
Registered: ‎02-03-2010
My Device: Z10 LE
My Carrier: Rogers

Re: Sending SMS example code

Hi, can you tell us how you got it working.  Thanks.


Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing
Please use plain text.
New Developer
Zvjer
Posts: 10
Registered: ‎01-18-2013
My Device: Z10LE
My Carrier: VIPnet Croatia

Re: Sending SMS example code

I will give you my full working code example when I catch some free time. I already planned it, but now I'm on my way to work. Note that Pauls code is good, I just changed it a bit.
Please use plain text.
Contributor
kuroberry
Posts: 30
Registered: ‎02-05-2013
My Device: Dev Alpha B
My Carrier: maxis

Re: Sending SMS example code

Hi, could you kindly provide the working sample code. Thanks

Please use plain text.