Android c2dm learning-cloud push

Source: Internet
Author: User

I. Basic Knowledge

When developing an application that needs to interact with the server, we basically need to obtain the server-side data. For example, in earthquake timely, we need to obtain the latest seismic information on the server in a timely manner. Generally, there are two methods to obtain information updated from time to time on the server. The first method is that the client uses the pull (pull) method to obtain information on the server from time to time, check whether any updated information appears. The second is that the server uses the push method. When the server has new information, it pushes the latest information to the client.

Although both the pull and push methods can obtain the server-side update information, the push is better than pull. Because the pull method is more charged for the client's network traffic, more importantly, the electricity consumption.

Android 2.2 and later versions have added the cloud to device messaging (c2dm) framework and supported the push function in the system. It is easier to use the push function based on the Android platform. Although c2dm is still in the lab stage, small-scale usage should be fine.

Next we will try out the c2dm function of Android.

Ii. c2dm framework

The c2dm function of Android has the following requirements:

1. android2.2 or a later version is required.

2. Google accounts must be configured on Android devices that use the c2dm function.

3. register the user email account that uses the c2dm function here (it is best to register a separate Gmail email account for c2dm ).

Let's take a complete c2dm process. Here we will use the chrome to phone process diagram officially launched by Google to describe it.

 

To use c2dm for push, follow these six steps:

(1) Registration: the Android device uses c2dm function of the user account (such as android.c2dm.demo@gmail.com) and app name sent to c2dm server.

(2) The c2dm server returns a registration_id value to the Android device. The device needs to save this registration_id value.

(3) The Android device sends the obtained registration_id and c2dm functions of the user account (android.c2dm.demo@gmail.com) to their own server, but the general user account information is determined by the server, so do not have to send.

In this way, the Android device registers the c2dm function, and then receives the messages pushed by the c2dm server.

(4) The server obtains data. The example in the figure below shows chrome to phone. The server receives the data sent by the Chrome browser. Data can also be generated locally on the server. The server here is Google appengine (a good service, but it is a pity that it has been screen-up in China). You need to replace it with your own server. The server also gets the clientlogin permission auth for registering a user account (android.c2dm.demo@gmail.com) that uses the c2dm feature.

(5) the server sends the data to be sent together with the registration_id and carries the obtained auth in the header to the c2dm server in post mode.

(6) The c2dm server sends data to the corresponding Android device in Push mode. The Android device only needs to obtain data from the corresponding key in the program in the format previously discussed with the server.

In this way, we will probably understand the c2dm workflow. Next we will introduce the above six steps with an instance.

Iii. instance Development

The program to be created is named androidc2dmdemo and the package name is com. ichliebephone. c2dm.

Before getting started, register the user account that uses the c2dm function on the c2dm webpage.

Figure 2 Application name

The Application name must be the complete name with the package name. For example, Om. ichliebephone. c2dm. androidc2dmdemo is used here.

Figure 3 c2dm user account registration

Here, you can use a contact email address that can receive the email. It is best to register a separate Gmail email address in the following role (sender) account mailbox to use the c2dm service. Here we use a specially registered android.c2dm.deno@gmail.com mailbox.

After submission, you will receive a confirmation email sent from Google in a period of time, and then you can use the PUSH Service of c2dm.

After so many introductions, we can quickly complete an instance and only complete the registration of the Android device. It does not contain the specific code for sending registration_id to the server and sending data to the c2dm server, this part is simulated using the curl command in Ubuntu, mainly to quickly experience the push result.

Create an android project androidc2dmdemo and include the c2dm package com. Google. Android. c2dm in Google's open-source example chrome to phone. The package contains three Java classes:

The first class is c2dmbasereceiver:

C2dmbasereceiver

The second class is c2dmbroadcastreceiver:

C2dmbroadcastreceiver

The third class is c2dmessaging:

C2dmessaging

Some Chinese comments have been added to the Code. You can first get to know about them and wait for the entire project to be created and explained together.

Create our own package com. ichliebephone. c2dm, which contains two classes. One is the project portal androidc2dmdemo:

Androidc2dmdemo

It is easy to register with the c2dm server.

The other class is c2dmbasereceiver, a subclass of c2dmreceiver:

C2dmreceiver

In this class, we receive the message in onmessage, the callback function that receives the push, and display the message in the status bar using notification.

The complete project directory is as follows:

Figure 4 project directory

Finally, we need to add the corresponding permissions in androidmanifest. xml:

Androidmanifest. xml

Because c2dm is only supported by Android 2.2 and later, create an AVD of 2.2 and later, and set Google account in "Settings-> account and synchronization, as shown in:

Figure 5 set the Google account in the Android device

Then you can run the program. We will see the obtained registration_id in the ddms output:

Figure 6 registration_id

If the first running does not appear, try to run it again.

With registration_id, our server can send the data to be pushed to c2dm. Here, we will simplify the process, run the curl command in Ubuntu to simulate the server function and send data to c2dm.

Let's get the clientlogin permission auth of c2dm first, and enter in the Ubuntu terminal:

1 lingaohe@lingaohe-laptop:~$ curl -d "accountType=HOSTED_OR_GOOGLE&Email=android.c2dm.demo@gmail.com&Passwd=androidc2dmdemo&service=ac2dm&source=bupt-c2dmdemo-1.0" https://www.google.com/accounts/ClientLogin

This indicates to forward.

If your email address has been registered on the c2dm webpage and the password is correct, the required auth content will be returned:

1 SID=DQAAAKYAAADcTtHbBBNcZJEOfkfVRycD_ZOIidwsQ3UwIY7cSrYWaY6uhlfo0l9gRPB-mQxP4K2T5tWiG--vWVmSTeq5p8SPwgnsYvfzj7bkNiPPIy4xRimVVfBmAHnZgLohw7gHMKi5DS6kK-Ut5tNzdTkI0I2tUDF0ryQ7MnPpI6Sj-gUCyBXmvKatHHDnNTTV78XdGIx7FYej1DyqGsPsYo3bCstHgltjv3cd2Hs7D4yrpUWHZw2 LSID=DQAAAKgAAABCpaoUE4XvxM24Cofntw1IUGx5fKxX-m7aqTL0zhunP0OjzJ2sn9ywmPa1BMZ2cF2IchuxHFLVzaSQfydAmiHZJGXLgaUorpIN6yz1e0VFWKmS6j4wGjZOos3QoJ9rkha0jKbOiHfBesADjxk-qjJ24TJ0RL-xkZHQyzS69YlA1KyzqIKjAMCzgqaDfCwhqxylJzizJksO2h8xpAFXZ38d_grm8XYZtzejiCiAMAR65A3 Auth=DQAAAKoAAACRF4pgYULnXULoWgbwfdqmMiRhfZYa1l-LW_rwGD7cofov4L4c2bVrtCOXbEbkju_hhqdAonpMkrb5icptt28fU8c-s-u1y2MXNYDxPIdQzfA2t6oI3NTmyj35MpsR1NKL4TN7ZVEn6z9NueuiKAqLHukZYh1YMGkGC8M6rVvA7AWPW36064XCQED7KLVNp_pGT00lrni7UdZKZWEy0FT-EVR-OxDyHWw6C-5Kmfkisw

The returned content includes Sid, lsid, and Auth. Auth is what we need.

With the auth and registration_id values, we can continue to use the curl command to simulate the function of our own server and send the data to be pushed to c2dm:

1 lingaohe@lingaohe-laptop:~$ curl -H "Authorization:GoogleLogin auth=DQAAAKoAAACRF4pgYULnXULoWgbwfdqmMiRhfZYa1l-LW_rwGD7cofov4L4c2bVrtCOXbEbkju_hhqdAonpMkrb5icptt28fU8c-s-u1y2MXNYDxPIdQzfA2t6oI3NTmyj35MpsR1NKL4TN7ZVEn6z9NueuiKAqLHukZYh1YMGkGC8M6rVvA7AWPW36064XCQED7KLVNp_pGT00lrni7UdZKZWEy0FT-EVR-OxDyHWw6C-5Kmfkisw" -d "registration_id=APA91bGUBoSvt3G5Ny9t0IGLmIKAKYX6G6VHwSQHh3tP2fqcaQ0N4GPdKh5B3RDUHFCFF06YwT8ifOP_cOy5BAWyCLHL8d8NpuIW9AqXt9h2JSBVF2MitZA&collapse_key=1&data.msg=ichliebejiajia" https://android.apis.google.com/c2dm/send

The part of the sent data is data. MSG = ichliebejiajia, which indicates that the content of the sent data is ichliebejiajia and the key value is MSG. The key value is consistent with the program on the android terminal so that it can be obtained on the terminal. If the message is sent successfully, an ID value is returned, for example:

1 id=0:1308623423080544%6c5c15c2000000312 lingaohe@lingaohe-laptop:~$

At this time, our server has sent the data to the c2dm server, and the Android device will receive the data pushed by the c2dm server.

In our example, we can see the messages printed in ddms:

Figure 7 push data obtained

At the same time, the status bar of the android simulator will display corresponding notifications:

Figure 8 push data received by the android Simulator

In this way, we can quickly implement the push function of the c2dm framework of Android. Further explanations and server-side code processing will be available later.

 

Example of the complete code corresponding to the article:

Http://download.csdn.net/source/3425855

Reprinted from: http://blog.csdn.net/ichliebephone/article/details/6591071

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.