BB10 - 从级联应用程序发送短信

时间:2013-09-01 17:03:49

标签: c++ qml blackberry-10 blackberry-cascades

我使用以下代码从我的应用发送短信;

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;

}

但是每次发送新短信时,它都会在短信用户界面中创建一个新主题。我想知道是否有办法将新消息添加到已发送给它的数字的线程中?

谢谢!

1 个答案:

答案 0 :(得分:2)

导致此错误的代码方面是

// 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);

这条跟随其前一行的内容将为participants创建一个新的对话,而不管之前现有的对话是什么。

要解决此问题,BlackBerry Cascades PIM MessageService会提供MessageSearchFilter,您可以使用Source来过滤任何SearchFilterCriteria的对话。因此使用它......

//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);
}

修改

尽管最初的{{3}}说了什么,但我觉得它有点儿麻烦。如果您完全按照原样使用它,如果没有与联系人作为唯一参与者的对话,您将始终以新对话结束。我尝试使用联系人的电话号码在我的STL100-3上的BlackBerry Hub中进行搜索,最后我收到许多消息,这些消息都在同一个对话中。但这意味着,如果按MessageFilter::Participants过滤,则可能会返回许多会话。最好使用MessageFilter::ContactId进行过滤。

p.s:我将代码块命名为bb::pim::messages::,因此不会重复。