Namespaces
Variants
Actions

使用QML完成notification客户端的开发

Jump to: navigation, search
文章信息

代码示例
文章
liuting 在 22 Feb 2012 创建
最后由 hamishwillee 在 26 Jul 2012 编辑

Contents

介绍

在上篇文章中我们介绍了如何使用QT 开发NOTIFICATION 客户端,在本文中我们将介绍如何使用QML开发NOTIFICATION客户端,下面我们来看下具体实现代码

实现步骤

设置应用ID和服务ID

为了成功认证,在程序中必须设定服务ID和应用ID

const QString application_id = "com.example";
const QString service_id = "example.com";

如上述代码所示,example.com是公共服务"example.com"的ID, 这个服务可以用于从开发者控制台发送通知到客户端。注意使用这个公共服务时,你不能使用通知ID 来指定通知接受者,因为这个服务是公共的,在开发者控制台的任何人都可以使用它。为了使用这个公共服务,你只能使用 JID(用户名和应用ID)去说明通知的接受者。为了使用通知ID去发送通知,你必须注册你自己的服务,并且按照下述代码所示替代service_id 和 application id

const QString application_id = "mynotifexample.com";
const QString service_id = "com. mynotifexample ";

声明 NOTIFICATTION API QML类型

The Notifications Client API is implemented as a Qt Plugin and its QML binding needs to be loaded by the QML script. The following code shows how to declare the Notifications API QML type and make the Notifications API reachable from QML code. notification API 被实现成为一个插件,为了在QML中使用这些API,我们必须在QML中声明,代码如下

import com.nokia.OviNotifications 1.0

注册应用

// Emitted after component "startup" has completed. Registration occurs.
Component.onCompleted: {
notifications.registerApplication(applicationId);
}

管理应用状态

应用程序有如下三种状态: 1.EStateOffline 2.EStateConnecting 3.EStateOnline

/ State of the application has changed. 
OviNotificationSession
{
id : notifications
onStateChanged : {
 
// Printing status on screen
// aState is an instance of type OviNotificationState.
switch(aState.sessionState)
{
case OviNotificationState.EStateOffline:
{
messages.insert(0, {"msg": "onStateChanged", "description": "offline"})
break;
}
case OviNotificationState.EStateConnecting:
{
messages.insert(0, {"msg": "onStateChanged", "description": "connecting"})
break;
}
 
case OviNotificationState.EStateOnline:
{
messages.insert(0, {"msg": "onStateChanged", "description": "online"})
break;
}
 
default:
break;
}
 
messages.insert(0, {"msg": "Error", "description": aState.sessionError});
 
}

获得通知ID

推荐使用通知ID来指定接收者。通知ID由客户端申请,然后发送给服务端。注意:发送通知ID给服务发生在通知服务之外,通知API并不提供机制去发送通知ID给服务

// connecting to the notificationInformation signal
onNotificationInformation : {
notificationId=aData.notificationId;
messages.insert(0, {"msg": "onNotificationInformation", "description": notificationId})
}

获得通知内容

为了接收通知,必须响应相应的信号,然后使用OviNotificationMessag和OviNotificationPayload 去读取通知

onReceived : {
var from="From: "+aNotification.senderInformation
var timestamp="Type: "+aNotification.timestamp
var paytmp=aNotification.payload;
var notification ="Payload: "+paytmp.dataString
var encoding ="Encoding: "+paytmp.encoding
var type ="Type: "+paytmp.type
messages.insert(0, {"msg": "onReceived", "description": notification})
}

代码下载

Media:Notificationqmlexample.zip

相关链接

This page was last modified on 26 July 2012, at 09:45.
246 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved