×
Namespaces

Variants
Actions
(Difference between revisions)

在GPS应用程序中使用Notification API

From Nokia Developer Wiki
Jump to: navigation, search
flycarl (Talk | contribs)
(Created page with ' ==介绍== 本文翻译自[Using Notification API in GPS based application] 。<br> Qt做为跨平台的应用程序和用户界面 开发框架,最近引进了"通知API"(…')
 
flycarl (Talk | contribs)
Line 2: Line 2:
  
 
==介绍==
 
==介绍==
本文翻译自[Using Notification API in GPS based application] 。<br>
+
*本文翻译自[[Using Notification API in GPS based application]] 。
 
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的例子。
  

Revision as of 05:50, 8 May 2011


Contents

介绍

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

Introduction

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.


Goals and objectives:

To utilize Notifications API and develop a real-time sticky application within hours with minimal development effort.


Problem statement:

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.


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

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

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.


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

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();
}

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.

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;
}

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



Conclusion:

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:

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/

254 page views in the last 30 days.
×