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

标签 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;

然而,每次它发送新的 SMS 时,它都会在文本消息 UI 中创建一个新线程。我想知道是否有办法将新消息添加到它要发送到的号码已经存在的线程中?

谢谢!

最佳答案

导致此错误的代码方面是

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

这篇文章——按照其前面的几行,将为 参与者 创建一个新的对话,而不管 Hub 中以前存在的对话。

为了解决这个问题,BlackBerry Cascades PIM MessageService 提供了一个 MessageSearchFilter您可以使用它按任何 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);
}

编辑

尽管原创Source说,我觉得它有点问题。如果你完全按照它说的那样使用它,如果没有与作为唯一参与者的联系人的对话,你将总是以新的对话结束。我尝试在我的 STL100-3 上的 BlackBerry Hub 中使用联系人的电话号码进行搜索,结果我得到了同一个对话中的许多消息。但这意味着如果您按 MessageFilter::Participants 过滤,可能会返回许多对话。最好使用 MessageFilter::ContactId 进行过滤。

p.s: 我为代码块命名空间,所以 bb::pim::messages:: 不会重复。

关于c++ - BB10 - 从级联应用程序发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560779/

相关文章:

c++ - Qt 中的 "no matching function to call"错误

c++ - 使用 For 循环显示 1 个结果

c++ - 为什么在连接调试器/IDE 后我的 STL 代码运行如此缓慢?

qml - 从 C++ 访问动态 QML 对象

qt - 如何设置 QML TableViewStyle 以显示中间椭圆 (ElideMiddle)

javascript - 为什么当循环为空时 For-in 循环返回 'arg'

c++ - 创建一个简单的 ListView Cascades

c++ 到 c#'s "作为”?

c++ - 带有 qt5 (qml) 的无框窗口

android - 在 MonoDroid 中开发 - 迁移到 BlackBerry 10 的前景