2013年11月15日 星期五

Android: Implementing Flurry for Android

1. Flurry Mechanism
The following diagram is a work flow from applying for an API key to implementing a GCM client. The following contents are going to describe the both steps. These steps are divided into two parts, Basic setup and Understanding user behavior through tracking custom events.




1.1. Basic Setup
A. Download the Flurry Android SDK
Open the URL, and then create an account as well as download Flurry SDK.
B. Add the Flurry lib to your project
Add FlurryAgent.jar to your android project
C. Configure your AndroidManifest.xml, as the following diagram.
D. Add calls to onStartSession and onEndSession. The highlighted section is the API Key you applied on Flurry web site, as the following diagram.
1.2. Understanding user behavior through tracking custom events
If you want to understand this part, you can see the document flurry offered through the flurry website.
1.3. Testing result
The first diagram is a result of successfully creating a project named MyFlurryTest and its’ testing result. In addition, the others are the final result on the dashboard of the flurry; however, the red highlight section on the dashboard of the furry is a relative to the event your source code set.


Android: Implementing GCM Server for Android Test

1. Implementing GCM Server
So far the above contents have installed necessary libraries, register our account to the Google Cloud Console, and complete our client side. In this section, we need to create a simple application server with PHP language.

A. Create Table in your database
This application server receives the registration id from the application, stores it in the database and sends the message to the application using GCM connection server. To store the registration id, create a simple MySQL table using following query, as the following diagram.




B. Create a register.php file
When android application runs for first time, it registers the application to the GCM using sender ID (i.e. project ID mentioned above) and gets the registration Id. The application then sends the registration ID to the application server to store in the database. Here is the PHP file register.php which does the above mentioned task i.e. it receives the registration id and stores it in the database. If you want to see the most complete source code, you can refer to sample code about the GCM server.

D. Create a index.php file
If you want to send the message to the application then you write a message and fetch all the registration ids from the database. You then send the registration ids along with the message to the GCM server. The GCM servers gives the response back to the application server. index.php file does the above mentioned task i.e. it sends the message to the application and echo back the response from the GCM server. By the way, you should replace the API Key you appled, as the following diagram. If you want to see the most complete source code, you can refer to sample code about the GCM server.



2.Test result
First, we launched an emulator from Android Virtual Device Manager, and pressed the button named “Register Device” in Figure 9; meanwhile, the system will show a Toast message.


After registration to the server side, your registration id from the client side will be record in the database, as the following diagram.


To open index.php file through your web browser delivers a message, as the following diagram.


Finally, you can see the message from the server side in your emulator, as the following diagram.


3.References
A. GCM: https://developer.android.com/google/gcm/index.html

2013年11月14日 星期四

Android: Implementing GCM Client for Android

If you have no any idea about the topic, you can try to study this article - Introduction for GCM and Flurry.

1. Implementation Steps
The session is divided into two parts, GCM Mechanism for Android as well as Flurry Mechanism. There are two types on Implementation steps for GCM. The first method is deprecated by Google. The other way is the newest implementation method.

1.1. GCM Mechanism for Android
In the beginning, the session will introduce one thing about implementing GCM mechanism. It is the newest way using “google_play_service lib” project. Another way is an older method which Google has not suggested, but it will not be launched in the document. By the way, both ways are the same on applying for an API key, and only implementing GCM Client is different. The following diagram is a work flow from applying for an API key to implementing a GCM client. The following contents are going to describe the both steps.


1.1.1. Applying for an API Key
  • A. Creating a Google API project
    •  Open the Google Developers Console.
    •  If you haven't created an API project yet, click Create Project.
    •  Supply a project name and click Create.
    •  Copy down your project number.
    •  In the sidebar on the left, select APIs & auth.
    •  In the displayed list of APIs, turn the Google Cloud Messaging for Android toggle to ON.
  • B. Obtaining an API Key
    •  In the sidebar on the left, select APIs & auth > Credentials.
    •  Under Public API access, click Create new key.
    •  In the Create a new key dialog, click Server key.
    •  In the refreshed page, copy the API key. You will need the API key later on to perform authentication in your application server.
  • C. Set Up Google Play Services SDK
    •  Set Up a Project that Uses Google Play Services
    •  Using Eclipse or another IDE:
    •  Setting up “google-play-services_lib” Project

1.1.2. Implementing GCM Client
  • A. Edit Your Application's Manifest
    •  You should add the following contents into the Manifest in Android project, as the following diagram.




  • B. Create a simple layout
    • This layout is a simple view which shows a general bottom used to register device to server. Once we register device success, the button will be disabled, as the following diagram.



  • C. Create a GcmBroadcastReceiver.java
    • The GcmBroadcastReceiver needs to extend the WakefulBroadcastReceiver class. A WakefulBroadcastReceiver is a special type of broadcast receiver that takes care of creating and managing a partial wake lock for your app. It passes off the work of processing the GCM message to a Service (typically an IntentService), while ensuring that the device does not go back to sleep in the transition. If you don't hold a wake lock while transitioning the work to a service, you are effectively allowing the device to go back to sleep before the work completes. The net result is that the app might not finish processing the GCM message until some arbitrary point in the future, which is not what you want. If you want to know how to implement this class, you can refer to the sample code.
  • D. Create a GcmIntentService.java
    • The intent service does the actual work of handling the GCM message. When the service is finished, it calls GcmBroadcastReceiver.completeWakefulIntent() to release the wake lock. The completeWakefulIntent() method has as its parameter the same intent that was passed in from the WakefulBroadcastReceiver. If you want to know how to implement this class, you can refer to the sample code.
  • E. Create a RegisterApp.java
    • This class should extend AsyncTask class. We need to complete some important tasks within the class. You should send the registration ID to your server over HTTP, so it can use GCM/HTTP or CCS to send messages to your app. The request to your server should be authenticated if your app is using accounts. You need to pay attention to some things about how to correctly finish these tasks. Setting your “Sender ID” and “URL” in this file is the most principal thing, as the following diagram.


2. References








2013年11月13日 星期三

Android: Introduction for GCM and Flurry

1. Purpose
The purpose of the article is about how to implement Google Cloud Messaging (GCM) for Android, a mechanism for passing messages between clients/devices and servers, and Flurry mechanism, an analytic tool which can help developers or data scientists collect data from mobile devices, such as Iphone, Android or Windows phone, as well as assist those to parse data as available information and analyze them to improve effects on E-Commerce (EC).
1.1. Google Cloud Messaging for Android
Google Cloud Messaging for Android (GCM) is a service that allows you to send data from your server to your users' Android-powered device, and also to receive messages from devices on the same connection. The GCM service handles all aspects of queueing of messages and delivery to the target Android application running on the target device. GCM is completely free no matter how big your messaging needs are, and there are no quotas.
1.2. Flurry for Android
The "Analytics Service" means, collectively, the "Software", the "Analytics Reports" and the "Documentation", all as defined below in this Agreement. Under this Agreement, Flurry may allow you to access the Analytics Service by using Flurry's analytics site code (the “Agent”) and any fixes, updates and upgrades provided to you, provided that you have an active Flurry account. In addition, Flurry may provide you with on-line access to a variety of analytics reports (the " Analytics Reports") generated by Flurry's processing code and any fixes, updates and upgrades. The Agent and Flurry's processing code are defined collectively herein as "Software". The processing code analyzes the data collected by the Agent. This data concerns the characteristics and activities of end users of your applications.