0

I am using the following code to send an SMS from my app;

void App::sendSms(const QString &messageText, const QStringList &phoneNumbers) {
bb::pim::account::AccountService accountService;
bb::pim::message::MessageService messageService;

QList<Account> accountListy = accountService.accounts(bb::pim::account::Service::Messages,"sms-mms");

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



QList<bb::pim::message::MessageContact> participants;
foreach(const QString &phoneNumber, phoneNumbers) {
    bb::pim::message::MessageContact recipient = bb::pim::message::MessageContact(
        -1, bb::pim::message::MessageContact::To,
        phoneNumber, phoneNumber);
    participants.append(recipient);
}

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

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

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

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

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

bb::pim::message::Message message = *builder;

messageService.send(smsAccountId, message);

delete builder;
delete conversationBuilder;

}

However everytime it sends a new SMS, it creates a new thread in the Text Messages UI. I was wondering if there was a way to add the new message to the thread that already exists for the number it is going to send to?

Thanks!

Jake Evans
  • 978
  • 5
  • 13
  • 33
  • try having a messages view from which you select the message. the selection should trigger some `onSelected` slot with a `conversation_id` you can use to append messages to it. this is right off the dome but it sounds right. research and let me know – Igbanam Nov 28 '13 at 01:40

1 Answers1

2

The aspect of your code which causes this bug is

// at top of file
using namespace bb::pim::messages;

ConversationBuilder *conversationBuilder = ConversationBuilder::create();
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);

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

This piece—following its preceeding lines, would create a new conversation for the participants regardless of previous existing conversations in the Hub.

To work around this, the BlackBerry Cascades PIM MessageService provides a MessageSearchFilter with which you can use to filter the conversations by any SearchFilterCriteria. Use it thus…

//Setup a filter
MessageFilter filter;
//Set our filter to filter conversations with id of the contact
filter.insert(MessageFilter::ContactId, contactId);
//Run filter
filterdConvosKeys = messageService.conversationKeys(smsAccountId, filter);

ConversationKey conversation_id;
//Vars for conversation builder
conversationBuilder->accountId(smsAccountId);
conversationBuilder->participants(participants);

//Get conversation ID for existing conversation, else create new
if (filterdConvosKeys.count() > 1) {
    // go through all conversations for this contact and choose
    // the conversation in which this contact is the sole participant
else if (filterdConvosKeys.count() == 1) {
    conversation_id = filterdConvosKeys[0].toAscii();
} else {
    conversation_id =  messageService.save(smsAccountId, conversation);
}

Edit

Despite what the original Source says, I find it a bit buggy. If you use it exactly as it says, you will always end up with a new conversation if there is no conversation with the contact as the only participant. I tried doing a search in the BlackBerry Hub on my STL100-3 with the phone number of a contact, I ended up with many messages which were in the same conversation. But this means there is a possibility that many conversations are being returned if you filter by MessageFilter::Participants. It is better to filter using MessageFilter::ContactId.

p.s: I namespaced the code blocks so bb::pim::messages:: is not repeated.

Igbanam
  • 5,904
  • 5
  • 44
  • 68
  • Oy! I stumbled back on this answer, read through and wanted to +1 it. Then I realized I was the one who answered it ( ._.) – Igbanam Mar 27 '14 at 09:45