Simple BBM Integration in Cascades Featured

Did I say simple? I meant very simple. In the following sample app I started with the standard empty cascades project and added a total of ~55 lines of code to the applicationui.cpp and applicationui.app files. In addition the UI is very plain as to not take away from the core purpose of a sample application which is to demonstrate how to do something, right?

Anatomy

The UI is composed of 2 Containers in a Page with two Buttons, a TextField and one Divider.

The inviteUserToDownloadViaBBM() function and updatePersonalMessage() functions are defined in the applicationui.cpp file, declared in the applicationui.hpp file and called from the main.qml file.

Files we will be editing:

  • applicationui.cpp
  • applicationui.hpp
  • main.qml
  • *YourApp*.pro
  • bar-descriptor.xml

There are some other things we add in the applicationui.cpp/.hpp files as well, so lets dive in.

Let's Do This

1Starting with a new standard empty project, open up the applicationui.cpp file from the /src directory. First, add #include <bb/system/SystemDialog> to the top where the other includes are and add using namespace bb::system; just beneath the existing using namespace bb::cascades;

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

2Just below the code shown above, add the following code:

qml->setContextProperty("_app", this);

3Next, look for app->setScene(root); (around line 25-30) and add the follow block of code:

	m_context = new bb::platform::bbm::Context(
			//UUID was generated at random for this sample
			//BE SURE TO USE YOUR OWN UNIQUE UUID. You can gerneate one here: http://www.guidgenerator.com/
			QUuid("15e4be70-fd36-11e2-b778-0800200c9a66")); 
	if (m_context->registrationState()
			!= bb::platform::bbm::RegistrationState::Allowed) {
		connect(m_context,
				SIGNAL(registrationStateUpdated (bb::platform::bbm::RegistrationState::Type)),
				this,
				SLOT(registrationStateUpdated (bb::platform::bbm::RegistrationState::Type)));
		m_context->requestRegisterApplication();
	}

4Now, we need a few functions so scroll to the very bottom on your applicationui.cpp file and add the following three functions:

void ApplicationUI::inviteUserToDownloadViaBBM() {
	if (m_context->registrationState()
			== bb::platform::bbm::RegistrationState::Allowed) {
		m_messageService->sendDownloadInvitation();
	} else {
		SystemDialog *bbmDialog = new SystemDialog("OK");
		bbmDialog->setTitle("BBM Connection Error");
		bbmDialog->setBody(
				"BBM is not currently connected. Please setup / sign-in to BBM to remove this message.");
		connect(bbmDialog, SIGNAL(finished(bb::system::SystemUiResult::Type)),
				this, SLOT(dialogFinished(bb::system::SystemUiResult::Type)));
		bbmDialog->show();
		return;
	}
}
void ApplicationUI::updatePersonalMessage(const QString &message) {
	if (m_context->registrationState()
			== bb::platform::bbm::RegistrationState::Allowed) {
		m_userProfile->requestUpdatePersonalMessage(message);
	} else {
		SystemDialog *bbmDialog = new SystemDialog("OK");
		bbmDialog->setTitle("BBM Connection Error");
		bbmDialog->setBody(
				"BBM is not currently connected. Please setup / sign-in to BBM to remove this message.");
		connect(bbmDialog, SIGNAL(finished(bb::system::SystemUiResult::Type)),
				this, SLOT(dialogFinished(bb::system::SystemUiResult::Type)));
		bbmDialog->show();
		return;
	}
}
void ApplicationUI::registrationStateUpdated(
		bb::platform::bbm::RegistrationState::Type state) {
	if (state == bb::platform::bbm::RegistrationState::Allowed) {
		m_messageService = new bb::platform::bbm::MessageService(m_context,
				this);
		m_userProfile = new bb::platform::bbm::UserProfile(m_context, this);
	} else if (state == bb::platform::bbm::RegistrationState::Unregistered) {
		m_context->requestRegisterApplication();
	}
}

5That's it for the applicationui.cpp file and now we move on to the applicationui.hpp file. After opening the applicationui.hpp file, add the following to include:

#include <bb/platform/bbm/Context>
#include <bb/platform/bbm/MessageService>
#include <bb/platform/bbm/UserProfile>

6Now, we need to declare some of the functions and whatnot that we added in the applicationui.cpp file, so below is the application.hpp file in its entirety.

Newly added code is in RED

 #ifndef ApplicationUI_HPP_
#define ApplicationUI_HPP_
#include <QObject>
#include <bb/platform/bbm/Context> #include <bb/platform/bbm/MessageService> #include <bb/platform/bbm/UserProfile> namespace bb { namespace cascades { class Application; class LocaleHandler; } } class QTranslator; /*! * @brief Application object * * */ class ApplicationUI: public QObject { Q_OBJECT public: ApplicationUI(bb::cascades::Application *app); virtual ~ApplicationUI() { }Q_INVOKABLE void inviteUserToDownloadViaBBM(); Q_INVOKABLE void updatePersonalMessage(const QString &message); private slots: void onSystemLanguageChanged(); private: QTranslator* m_pTranslator; bb::cascades::LocaleHandler* m_pLocaleHandler;bb::platform::bbm::UserProfile * m_userProfile; bb::platform::bbm::Context *m_context; bb::platform::bbm::MessageService *m_messageService; Q_SLOT void registrationStateUpdated( bb::platform::bbm::RegistrationState::Type state); }; #endif /* ApplicationUI_HPP_ */

7Before we get to the main.qml file, now would be a good time to do the following:

  • add LIBS += -lbbplatformbbm -lbbsystem to your app's .pro file just before the CONFIG line
  • make sure to request BBM permissions by checking the  box next to BlackBerry Messenger in the Application tab of the bar-descriptor.xml

The QML

Your QML can look however you want it to, and the BBM functions can be called from any signal from which you want, but for the purposes of this sample, my QML is as follows with the BBM specific functions again in RED:

import bb.cascades 1.0
Page {
    Container {
        layout: DockLayout {
        
        }
        verticalAlignment: VerticalAlignment.Fill
        horizontalAlignment: HorizontalAlignment.Fill
        //Todo: fill me with QML
        Container {
            verticalAlignment: VerticalAlignment.Center
            horizontalAlignment: HorizontalAlignment.Center
            leftPadding: 15.0
            rightPadding: 15.0
            Button {
                text: "Invite Others to Download"
                onClicked: {
                    _app.inviteUserToDownloadViaBBM()
                }
                horizontalAlignment: HorizontalAlignment.Center
            }
            Divider {
            
            }
            TextField {
                id: bbmStatusText
                input {
                    onSubmitted: {
                        if (text != "") {
                            _app.updatePersonalMessage(bbmStatusText.text)
                        }
                    }
                }
                onTextChanging: {
                    if (bbmStatusText.text != "") {
                        updateMessage.enabled = true
                    } else {
                        updateMessage.enabled = false
                    }
                }
            }
            Button {
                id: updateMessage
                text: "Update BBM Status"
                enabled: false
                onClicked: {
                    _app.updatePersonalMessage(bbmStatusText.text)
                }
                horizontalAlignment: HorizontalAlignment.Center
            }
        }
    }
}

 So in my quick and plain QML, I have one button that will allow you to invite other users to download the app (this will only work once the app is downloaded from BlackBerry World). I also have a TextField and a Button that will request to update the user's BBM Personal Message with the test entered into the TextField. I have the function tied to two signals so it will run if the user presses enter after inputting some text, and also if they press the Button. Additionally, I have the Button disabled until the user enters some text into the field.

That's it!

The full project source in ZIP format is attached to this post as well as a precompiled and signed .bar file so you can see the app in action before you get started.

Happy Coding!

Leave your comments

Post comment as a guest

0
  • No comments found
Subscribe to the official OSBB BBM Channel!

osbbchannelQR

C00013E89

Back to top