使用QT 完成NOTIFICATION 客户端的开发
文章信息
Contents |
介绍
本文介绍如何使用QT NOTIFICATION API完成一个NOTIFICATION客户端,下面我们将以具体的代码来看实现步骤
实现步骤
设置应用ID和服务ID
为了成功认证,在程序中必须设定服务ID和应用ID
如上述代码所示,example.com是公共服务"example.com"的ID, 这个服务可以用于从开发者控制台发送通知到客户端。注意使用这个公共服务时,你不能使用通知ID 来指定通知 接受者,因为这个服务是公共的,在开发者控制台的任何人都可以使用它。为了使用这个公共服务,你只能使用 JID(用户名和应用ID)去说明通知的接受者。为了使用通知ID去发送通知,你必须注册你自己的服务,并且按照下述代码所示替代service_id 和 application id
加载插件并且连接信号
notification API被实现成一个插件,为了使用这些API,我们必须使用QPluginLoader 机制加载插件
//Load the plugin using QPluginLoader
// ONE_PLUGIN_ABSOLUTE_PATH macro is added to the project when CONFIG+=ovinotifications is added in project's pro file.
QPluginLoader *loader = new QPluginLoader(ONE_PLUGIN_ABSOLUTE_PATH);
QObject *serviceObject = loader->instance();
if (serviceObject)
{
success = true;
// Store the service interface for later usage
iNotificationInterface = qobject_cast<OviNotificationInterface *>(
serviceObject);
// Connext signals to slots.
connect(serviceObject,SIGNAL(stateChanged(QObject*)),
this,SLOT(stateChanged(QObject*)));
connect(serviceObject,SIGNAL(received(QObject*)),
this,SLOT(received(QObject*)));
connect(serviceObject,SIGNAL(notificationInformation(QObject*)),
this,SLOT(notificationInfo(QObject*)));
connect(serviceObject,SIGNAL(version(QString)),
this,SLOT(versionResponse(QString)));
}
注册应用
iNotificationInterface->registerApplication(application_id);
// In the stateChanged slot
// State of the application has changed
OviNotificationState* state = static_cast<OviNotificationState*>(aState);
// Print out the session state on the screen
switch (state->sessionState())
{
case OviNotificationState::EStateOffline:
{
iTextConsole->append("Application is offline.");
break;
}
case OviNotificationState::EStateOnline:
{
iTextConsole->append("Application is online.");
break;
}
case OviNotificationState::EStateConnecting:
{
iTextConsole->append("Application is connecting.");
break;
}
default:
{
break;
}
}
获取NOTIFICATION ID
推荐使用通知ID来指定接收者。通知ID由客户端申请,然后发送给服务端。 注意:发送通知ID给服务发生在通知服务之外,通知API并不提供机制去发送通知ID给服务,为了获得通知ID,第一步必须连接notificationInformation 信号
接着调用getNotificationInformation去获得通知ID
iNotificationInterface->getNotificationInformation(service_id);
When the Notification ID is fetched, the signal notificationInformation is emitted, and the corresponding slot is connected to the signal, as shown below: 当获取到通知ID, notificationInformation 信号被发射,响应的槽函数实现如下:
// In the notificationInfo slot
// Cast QObject to OviNotificationInfo to access the methods
OviNotificationInfo* info = static_cast<OviNotificationInfo*>(aData);
// Show the requested notification id on the screen in message box
QMessageBox messageBox;
messageBox.setText("Notification Id");
messageBox.setInformativeText(info->notificationId());
messageBox.exec();
delete info;
接收通知
为了接收通知,必须连接接收信号到响应的槽函数,然后使用OviNotificationMessag和OviNotificationPayload 去读取通知
// In the received slot
OviNotificationMessage* notification = static_cast<OviNotificationMessage*>(aNotification);
// Show the received notification in the screen
OviNotificationPayload* payload = static_cast<OviNotificationPayload*>(notification->payload());
iTextConsole->append("Application " + notification->senderInformation()
+ "of the service "+notification->from()+ " service sent: \n"
+ "'" + payload->dataString() + "'");


(no comments yet)