×
Namespaces

Variants
Actions
(Difference between revisions)

在GPS应用程序中使用Notification API

From Nokia Developer Wiki
Jump to: navigation, search
flycarl (Talk | contribs)
flycarl (Talk | contribs)
Line 5: Line 5:
 
Qt做为跨平台的应用程序和用户界面 开发框架,最近引进了"通知API"(“Notification API”)这个引人注目的功能,用来实现实时推送通知。Notification API发送轻量的通知,优化了带宽和电池的耗用,具有很高的效率。移动应用的实现处在一个革命性的阶段,在这里,由服务端发起向移动客户端推送消息是一个不可避免的要素,现在这个功能在Qt SDK中发布了。Notification API 使得客户端即使在未激活的时候也能立即受到来自 邮件,facebook,twitter,天气更新,股票更新等的通知。 在这篇文章中我给出一个在GPS应用中使用Notification API的例子。
 
Qt做为跨平台的应用程序和用户界面 开发框架,最近引进了"通知API"(“Notification API”)这个引人注目的功能,用来实现实时推送通知。Notification API发送轻量的通知,优化了带宽和电池的耗用,具有很高的效率。移动应用的实现处在一个革命性的阶段,在这里,由服务端发起向移动客户端推送消息是一个不可避免的要素,现在这个功能在Qt SDK中发布了。Notification API 使得客户端即使在未激活的时候也能立即受到来自 邮件,facebook,twitter,天气更新,股票更新等的通知。 在这篇文章中我给出一个在GPS应用中使用Notification API的例子。
  
== Introduction ==
+
==目标==
 +
利用 Notification API 开发一个实时粘附应用程序(sticky application),花几个小时和最少的开发工作。
  
Qt, the cross-platform application and UI framework, has newly introduced ''Notifications API'', a striking feature which helps in adding real-time push notifications. Notifications API is highly efficient as it delivers light-weight notifications optimizing bandwidth and battery usage. Mobile application development is in its revolutionary stage now; server-initiated push messages to the mobile client are an inevitable factor here, which has been employed by Qt in the new beta release.  Notifications API enables clients to obtain instantaneous notifications for email, facebook, twitter, weather updates, stock updates, etc even when the applications are not active. In this article, I hereby elucidate yet another applicability of Notification API using GPS.
+
==问题表述==
  
 +
考虑在用户在某种紧急情况下,需要被追踪,以获得他们的实时位置。可以通过一个使用Notification API的GPS应用来处理。两个注册到服务器的用户,这个应用帮助一个用户被另一个连接到他的用户追踪。这个应用在下面这种情形下有用: 被追踪者不能回复消息,但是希望他/她的位置被跟踪。
  
 +
==实现策略==
  
== Goals and objectives: ==
+
客户端应用获取经纬度数据,从服务器后端接受消息。使用Qt的Notification API发送通知,使用Qt Mobility Location API获取经纬度。
 +
服务器后端提供功能包括: 用户注册,认证,发送消息给请求的用户。服务器使用Notification Serevice API向移动终端发消息。Notification Service API通过 HTTP REST接口 提供对通知服务器的访问。
  
To utilize Notifications API and develop a real-time sticky application within hours with minimal development effort.
+
==为什么用Qt==
 +
使用第三方SMS网关服务器向移动终端发消息 花费不菲。
 +
黑莓手机使用的推送机制 使用的黑莓企业服务 非常昂贵。
 +
尽管苹果的推送服务机制跟Qt比较类似,但是iPhone开发智能在Mac环境下开发,而基于Qt的推送服务开发可以用Qt Quick完成,不指定开发平台,并且开发过程始终那么愉轻松快。
 +
Android也是在开发上不如Qt Quick轻松,且其推送服务的实现不是通用的有效。
  
 +
==资源需求==
 +
-    一个支持Qt SDK 1.1的移动应用程序
 +
-    移动设备有internet连接
 +
-    一台有公共IP的服务器机器
 +
-    一个开发者
  
 +
==应用如何工作==
 +
这个应用程序帮助注册的客户端追踪他们的地理位置,即使在他们不能回复消息的情况下。注册在服务器上的客户端可以访问服务器上连接的其他客户端,当一个客户端要求追踪另一个客户端,服务器通过Notification API给后者手机发信息,后者手机回复自己的经纬度给服务器,然后服务器将这个坐标推送给请求客户端,并将坐标以地标形式显示在前者移动终端的地图上。每个注册用户都有权按他们的意愿启用或禁用跟踪设备。
  
==Problem statement:  ==
+
==应用实现导引==
 
+
实现这个工程十分容易,所有需要的资源,都在Forum Nokia和Qt Nokia网站上提供了。
Situations arise when people need to be traced in order to find out their exact location in emergencies. Such situations can be met by introducing a GPS application using Notifications API which helps to track a person who is registered to the server by another person who is also registered and connected with the former. This helps in circumstances where the former is not in a position to respond, but needs to be traced where he/she is. 
+
'''''客户端'''''
 
+
客户端包含三个模块,即,'''UI, 经纬度获取和通知监听(notification listener)'''
+
'''UI'''可以用Qt Quick轻松创建。Qt SDK自带很多例子,开发者可以从想要的例子中截取或进行相应的开发。
 
+
'''经纬度获取'''代码如下
== Strategy: ==
+
 
+
A client side application is developed which retrieves latitudes/longitudes and receives messages from the back end server. The notifications support can be provided by using Notifications Qt API. QT Mobility Location API can be used to get latitude & longitude.
+
Develop a back end server which enables user registration, authentication, sending message to the requested user. This server sends the message to the mobile by using Notifications Service API. The Notification Service API provides access to the Notification Server through an HTTP REST interface.
+
 
+
 
+
 
+
== Why Qt: ==
+
 
+
Notification implementation using a third party SMS gateway server to send message to mobile is a costly process.
+
 
+
The push mechanism used in Blackberry phones using Blackberry Enterprise Service is highly expensive.
+
 
+
Even though Apple push notification service works similar to Qt, the ease with which development can be done in Qt Quick makes it an all-time favorite. Moreover, it does not need a specific platform for development unlike the iPhone development which can be done only in Mac environment.
+
 
+
Android also lags behind Qt Quick in developmental ease and it is not available universally.
+
 
+
 
+
 
+
== Resource requirement: ==
+
 
+
-      A mobile application which supports Qt SDK 1.1 (Beta)
+
 
+
- Internet connectivity in the mobile
+
 
+
- A public IP server machine
+
 
+
- A developer
+
 
+
 
+
 
+
== How this app works: ==
+
 
+
This application helps registered clients to track their geographical positions even if they are not in a situation to respond to messages. A client who is registered to the server can access other clients who are also linked with the server and who have enabled tracing by the former. When the client requests for tracking of another client, the server sends a message via Notification API to the latter’s phone, and the latter responds by sending the current longitudinal and latitudinal position. This message hits the server again, and the server pushes it to the former who initiated the request. The positions are evoked in a map in the former’s mobile, denoting the exact geographical position of the latter. Any registered user is entitled to enable or disable the tracking facility according to their wish.
+
 
+
 
+
 
+
== A Guide towards implementation of the application: ==
+
 
+
Implementing the project is quite simple since all the needed resources are available in Forum Nokia and Qt Nokia website itself.
+
 
+
'''''Client Application:'''''
+
 
+
The client application consists of three modules viz. '''UI, latitudes and longitudes retrieval and notification listener.'''  
+
 
+
The '''UI''' for the mobile application can be easily created using QT Quick. Many samples are available which comes along with SDK. The developer shall cull out desired samples and develop accordingly.
+
 
+
The '''latitude and longitudes retrieval''' source code is given here
+
 
+
 
<code cpp>
 
<code cpp>
  
Line 137: Line 103:
 
</code>
 
</code>
  
It can be referred in the link: http://www.forum.nokia.com/document/Mobile_Hands-on_Labs/Qt/MobilityLocation/05.html. The file QtMobilityLocation.zip shall be downloaded, opened in Qt SDK 1.1 (Beta), the code is copied and appended to the project.
+
这可以从这个链接获取 http://www.forum.nokia.com/document/Mobile_Hands-on_Labs/Qt/MobilityLocation/05.html.,下载QtMobilityLocation.zip,在Qt SDK 1.1中打开,复制代码添加到工程中。
  
 +
'''通知监听'''的实现细节可以从这个链接的到: https://projects.forum.nokia.com/notificationsapi/files。下载notificationexample.zip,改编需要的数据。 本工程需要的代码片段列在下面
  
 
+
注册&注销通知的部分:
The details of implementation of '''notification listener''' can be obtained from the following link: https://projects.forum.nokia.com/notificationsapi/files. The file notificationexample.zip is downloaded and required data is adapted. The code snippets required for this application are given below
+
 
+
For registering & unregistering the application for the notification
+
 
+
 
<code cpp>
 
<code cpp>
 
void NotificationExample::registerApplication()
 
void NotificationExample::registerApplication()
Line 161: Line 124:
  
 
</code>
 
</code>
 
+
接收通知消息的代码如下。这可以定制使得当接收消息时,应用激活上面描述的经纬度获取部分。
The code for receiving notification message in the application is given below. This can be customized in such a way that on receiving the notification message, the application invokes the latitudinal & longitudinal values as explained above.
+
 
+
 
<code cpp>
 
<code cpp>
 
void NotificationExample::received(QObject* aNotification)
 
void NotificationExample::received(QObject* aNotification)
Line 195: Line 156:
 
</code>
 
</code>
  
'''''Back end server application:'''''
+
'''''服务器后端应用:'''''
 
+
The developer can develop a simple server side application in his choice of platform. The server sends message to mobile via Notifications API. Such sample service applications that use the Notifications REST API to send notifications to client applications can be seen here:
+
 
+
https://projects.forum.nokia.com/notificationsapi/wiki/serviceapisample
+
 
+
  
 +
开发者可以在他选择的平台上开发一个简单的服务器端应用。服务器通过Notification API向移动终端发消息。 参考https://projects.forum.nokia.com/notificationsapi/wiki/serviceapisample
 +
,这是一个使用Notification REST API给客户端发送消息的简单服务。
  
 
----
 
----
 
+
==总结==
== Conclusion: ==
+
在Facebook之前有很多社交网络站点,但他们都没有Facebook成功。这说明引进的时间不是最关键的,观点的唯一性表示才是关键。类似的,上面的使用Qt框架和Notification API开发的应用将会在同类中脱颖而出。
 
+
There were many social networking sites which came into being before Facebook. But none of them were as successful as Facebook. This shows that the time of introduction doesn’t matter, the uniqueness in expression of ideas is what that matters. Similarly, the above application developed using Qt framework and Notifications API shall stretch its wings beyond the horizons.
+
 
+
 
----
 
----
 
 
  
 
== References: ==
 
== References: ==

Revision as of 08:04, 8 May 2011


Contents

介绍

Qt做为跨平台的应用程序和用户界面 开发框架,最近引进了"通知API"(“Notification API”)这个引人注目的功能,用来实现实时推送通知。Notification API发送轻量的通知,优化了带宽和电池的耗用,具有很高的效率。移动应用的实现处在一个革命性的阶段,在这里,由服务端发起向移动客户端推送消息是一个不可避免的要素,现在这个功能在Qt SDK中发布了。Notification API 使得客户端即使在未激活的时候也能立即受到来自 邮件,facebook,twitter,天气更新,股票更新等的通知。 在这篇文章中我给出一个在GPS应用中使用Notification API的例子。

目标

利用 Notification API 开发一个实时粘附应用程序(sticky application),花几个小时和最少的开发工作。

问题表述

考虑在用户在某种紧急情况下,需要被追踪,以获得他们的实时位置。可以通过一个使用Notification API的GPS应用来处理。两个注册到服务器的用户,这个应用帮助一个用户被另一个连接到他的用户追踪。这个应用在下面这种情形下有用: 被追踪者不能回复消息,但是希望他/她的位置被跟踪。

实现策略

客户端应用获取经纬度数据,从服务器后端接受消息。使用Qt的Notification API发送通知,使用Qt Mobility Location API获取经纬度。 服务器后端提供功能包括: 用户注册,认证,发送消息给请求的用户。服务器使用Notification Serevice API向移动终端发消息。Notification Service API通过 HTTP REST接口 提供对通知服务器的访问。

为什么用Qt

使用第三方SMS网关服务器向移动终端发消息 花费不菲。 黑莓手机使用的推送机制 使用的黑莓企业服务 非常昂贵。 尽管苹果的推送服务机制跟Qt比较类似,但是iPhone开发智能在Mac环境下开发,而基于Qt的推送服务开发可以用Qt Quick完成,不指定开发平台,并且开发过程始终那么愉轻松快。 Android也是在开发上不如Qt Quick轻松,且其推送服务的实现不是通用的有效。

资源需求

- 一个支持Qt SDK 1.1的移动应用程序 - 移动设备有internet连接 - 一台有公共IP的服务器机器 - 一个开发者

应用如何工作

这个应用程序帮助注册的客户端追踪他们的地理位置,即使在他们不能回复消息的情况下。注册在服务器上的客户端可以访问服务器上连接的其他客户端,当一个客户端要求追踪另一个客户端,服务器通过Notification API给后者手机发信息,后者手机回复自己的经纬度给服务器,然后服务器将这个坐标推送给请求客户端,并将坐标以地标形式显示在前者移动终端的地图上。每个注册用户都有权按他们的意愿启用或禁用跟踪设备。

应用实现导引

实现这个工程十分容易,所有需要的资源,都在Forum Nokia和Qt Nokia网站上提供了。 客户端 客户端包含三个模块,即,UI, 经纬度获取和通知监听(notification listener) UI可以用Qt Quick轻松创建。Qt SDK自带很多例子,开发者可以从想要的例子中截取或进行相应的开发。 经纬度获取代码如下

void MainWindow::startGPS()
{
// Obtain the location data source if it is not obtained already
if (!locationDataSource)
{
locationDataSource =
QGeoPositionInfoSource::createDefaultSource(this);
if (locationDataSource)
{
// Whenever the location data source signals that the current
// position is updated, the positionUpdated function is called.
QObject::connect(locationDataSource,
SIGNAL(positionUpdated(QGeoPositionInfo)),
this,
SLOT(positionUpdated(QGeoPositionInfo)));
// Start listening for position updates
locationDataSource->startUpdates();
} else {
// Not able to obtain the location data source
// TODO: Error handling
}
} else {
// Start listening for position updates
locationDataSource->startUpdates();
}
}
 
void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
{
if (geoPositionInfo.isValid())
{
// Stop regular position updates, because a valid position has been
// obtained
locationDataSource->stopUpdates();
 
// Get the current location as latitude and longitude
QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();
 
// Fetch the map using the display size and the coordinates
QUrl url = createGoogleMapURL(size(), latitude, longitude);
webView->stop(); // Stop the ongoing request
webView->load(url); // Load the Google Map
}
}
 
QUrl MainWindow::createGoogleMapURL(const QSize& size, qreal latitude,qreal longitude)
{
const QString GOOGLE_MAPS_URL_TEMPLATE =
"http://maps.google.com/maps/api/staticmap?center=%1,%2&zoom=12&size=%3x%4&maptype=mobile&markers=color:red|label:Y|%1,%2&sensor=true";
QUrl url = QUrl(GOOGLE_MAPS_URL_TEMPLATE.arg(
QString::number(latitude), QString::number(longitude),
QString::number(size.width()), QString::number(size.height())));
return url;
}
Instantiate QWebView() as below and invoke startGPS() from your application module.
webView = new QWebView(this);
// Start the GPS
startGPS();

这可以从这个链接获取 http://www.forum.nokia.com/document/Mobile_Hands-on_Labs/Qt/MobilityLocation/05.html.,下载QtMobilityLocation.zip,在Qt SDK 1.1中打开,复制代码添加到工程中。

通知监听的实现细节可以从这个链接的到: https://projects.forum.nokia.com/notificationsapi/files。下载notificationexample.zip,改编需要的数据。 本工程需要的代码片段列在下面

注册&注销通知的部分:

void NotificationExample::registerApplication()
{
// Register to Notifications
iTextConsole->append("Registering to Notifications ...");
iNotificationInterface->registerApplication(application_id);
}
 
void NotificationExample::unregisterApplication()
{
// Unregister from Notifications
iTextConsole->append("Unregistering from Notifications...");
iNotificationInterface->unregisterApplication();
}

接收通知消息的代码如下。这可以定制使得当接收消息时,应用激活上面描述的经纬度获取部分。

void NotificationExample::received(QObject* aNotification)
{
// Casting the QObject to OviNotificationMessage to gain access
// to all its members.
OviNotificationMessage* notification =
static_cast<OviNotificationMessage*>(aNotification);
 
// Show the received notification in the screen
OviNotificationPayload* payload = static_cast<OviNotificationPayload*>(notification->payload());
 
iTextConsole->setText("Notification received!");
iTextConsole->append("Application " + notification->senderInformation()
+ " of the service "+notification->from()+ " service sent: \n"
+ "'" + payload->dataString() + "'");
// Printing out the notification details in text console. Optional.
iTextConsole->append("Notification received.");
iTextConsole->append("Notification was sent at " + notification->timestamp());
if (!payload->type().isEmpty())
{
iTextConsole->append("Payload type was set to " + payload->type());
}
if (!payload->encoding().isEmpty())
{
iTextConsole->append("Payload was encoded in " + payload->encoding());
}
 
delete notification;
}

服务器后端应用:

开发者可以在他选择的平台上开发一个简单的服务器端应用。服务器通过Notification API向移动终端发消息。 参考https://projects.forum.nokia.com/notificationsapi/wiki/serviceapisample ,这是一个使用Notification REST API给客户端发送消息的简单服务。


总结

在Facebook之前有很多社交网络站点,但他们都没有Facebook成功。这说明引进的时间不是最关键的,观点的唯一性表示才是关键。类似的,上面的使用Qt框架和Notification API开发的应用将会在同类中脱颖而出。


References:

http://www.forum.nokia.com/

http://developer.qt.nokia.com/prereleases

http://qt.gitorious.org/

http://qt.nokia.com/qtquick/

https://projects.forum.nokia.com/notificationsapi/

145 page views in the last 30 days.
×