01-18-2013 05:37 PM
Hello all!
I have found this documentation that might help me send an ordinary SMS from my app:
https://developer.blackberry.com/cascades/referenc
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!
Solved! Go to Solution.
01-18-2013 05:40 PM
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/pla in","body.txt",body));
messageBuilder->conversationId(conversationId);
Message message;
message = *messageBuilder;
//Sending SMS/MMS
MessageKey key = messageService.send(accountId, message);
qDebug() << "+++++++ Message sent" << endl;
01-18-2013 05:43 PM
01-22-2013 08:48 AM
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 ![]()
01-22-2013 09:38 AM
When checking incoming SMS does the mimeType of the message determine whether the SMS is binary or ASCII?
02-03-2013 12:05 PM
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(th is);
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::At tachment("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!
02-10-2013 10:13 AM
02-10-2013 03:25 PM
Hi, can you tell us how you got it working. Thanks.
02-11-2013 12:45 AM
02-26-2013 03:47 AM
Hi, could you kindly provide the working sample code. Thanks