• Google GCM推送


    负责人:玄月冰灵

    主任务原文链接:http://developer.android.com/guide/google/gcm/demo.html

    GCM的链接已有所更新:
    http://developer.android.com/google/gcm/index.html

    谷歌云推送(GCM)样例展示了如何在你的安卓应用中使用谷歌云推送框架。向导指引你通过设置来运行样例。

    这个样例包含以下部分:

    • 一个网页服务器包含一个你可以发送消息的页面。
    • 一个安卓应用接收和显示这些消息。

    此处的API参考文档是基于样例的帮助库:

    • 客户端参考
    • 服务端参考

    以下部分描述了如何从SDK管理器下载样例代码和帮助库。样例代码和帮助库在开源站点同样有效。

    要求

    网页服务器:

    • Ant1.8(可能早期版本也能运行,但不保证)。
    • 以下其中之一: :一个兼容Servlets API版本2.5的运行中的网页服务器,例如Tomcat6或Jetty,或 :Java App Engine SDK 版本1.6或更新。
    • 使用GCM需要一个已注册的谷歌账号。
    • 账号的API密钥。

    安卓应用:
    * 使用Google API且运行安卓2.2的模拟器(或设备)。
    * 使用GCM需要已注册账号的谷歌API工程ID。

    Setting Up GCM

    Before proceeding with the server and client setup, it's necessary to register a Google account with the Google API Console, enable Google Cloud Messaging in GCM, and obtain an API key from the Google API Console.

    For instructions on how to set up GCM, see Getting Started.

    设置服务器

    本节描述的不同选项设置了一个服务器。

    使用标准网页服务器

    使用标准的、servlet-compliant网页服务器来设置服务器。
    1. 从SDK管理器,安装Extras > Google Cloud Messaging for Android Library。
    这会在YOUR_SDK_ROOT/extras/google/ 目录下创建一个包含这些子目录的gcm目录:gcm-client, gcm-server, samples/gcm-demo-client, samples/gcm-demo-server, 和 samples/gcm-demo-appengine。

    注释:如果你在SDK管理器中没有看到Extras > Google Cloud Messaging for Android Library,确保你已经运行了了版本20或更高版本。确保在更新后重启SDK管理器。

    2. 在文本编辑器中,编辑
    samples/gcm-demo-server/WebContent/WEB-INF/classes/api.key
    并用获取的API密钥代替存在的文本。
    3. 在命令行窗口,进入samples/gcm-demo-server 目录。
    4. 运行ant war 生成服务器的WAR文件:

    $ ant war
    Buildfile:build.xml

    init:
    [mkdir] Created dir: build/classes
    [mkdir] Created dir: dist

    compile:
    [javac] Compiling 6 source files to build/classes

    war:
    [war] Building war: dist/gcm-demo.war

    BUILD SUCCESSFUL
    Total time: 0 seconds

    5.在你运行的服务器上部署dist/gcm-demo.war。例如,如果你使用Jetty,复制demo.war 到安装Jetty的webapps 目录。
    6.在浏览器中打开服务器主页。URL取决于你使用的服务器和你机器的IP地址。但它将会类似这样http://192.168.1.10:8080/gcm-demo/home, gcm-demo 是应用的环境,/home 是主servlet的路径。

    注释:你可以获得IP在Linux或MacOS上运行ifconfig ,或在Windows上运行ipconfig 。

    http://developer.android.com/images/gcm/gcm-demo-homepage.png

    你的服务器现在准备好了。

    使用JAVA的App Engine

    使用标准的Java App Engine设置服务器:

    1. 从SDK管理器中,安装Extras > Google Cloud Messaging for Android Library。 这将在 YOUR_SDK_ROOT/extras/google/ 目录下创建包含这些子目录的gcm 目录: gcm-client, gcm-server, samples/gcm-demo-client, samples/gcm-demo-server, and samples/gcm-demo-appengine.
    2. 在文本编辑器中,编辑: samples/gcm-demo-appengine/src/com/google/android/gcm/demo/server/ApiKeyInitializer.java 并用获取的API密钥代替存在的文本。 注释:在那个类中设置的API密钥值将在App Engine中一次性创建持久的实体。如果你部署应用,你可以使用App Engine的Datastore Viewer 在之后修改它。
    3. 在命令行窗口,进入samples/gcm-demo-appengine 目录。
    4. 开始用ant runserver开发App Engine服务器,使用-Dsdk.dir 指向本地的App Engine SDK,并用-Dserver.host 设置你服务器的主机名和IP地址。

    $ ant -Dsdk.dir192.168.1.10
    Buildfile: gcm-demo-appengine/build.xml

    init:
    [mkdir] Created dir: gcm-demo-appengine/dist

    copyjars:

    compile:

    datanucleusenhance:
    [enhance] DataNucleus Enhancer (version 1.1.4) : Enhancement of classes
    [enhance] DataNucleus Enhancer completed with success for 0 classes. Timings : input0 ms, total=28 ms. Consult the log for full details
    [enhance] DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details

    runserver:
    [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.jetty.JettyLogger info
    [java] INFO: Logging to JettyLogger(null) via com.google.apphosting.utils.jetty.JettyLogger
    [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
    [java] INFO: Successfully processed gcm-demo-appengine/WebContent/WEB-INF/appengine-web.xml
    [java] Jun 15, 2012 8:46:06 PM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
    [java] INFO: Successfully processed gcm-demo-appengine/WebContent/WEB-INF/web.xml
    [java] Jun 15, 2012 8:46:09 PM com.google.android.gcm.demo.server.ApiKeyInitializer contextInitialized
    [java] SEVERE: Created fake key. Please go to App Engine admin console, change its value to your API Key (the entity type is 'Settings' and its field to be changed is 'ApiKey'), then restart the server!
    [java] Jun 15, 2012 8:46:09 PM com.google.appengine.tools.development.DevAppServerImpl start
    [java] INFO: The server is running at http://192.168.1.10:8080/
    [java] Jun 15, 2012 8:46:09 PM com.google.appengine.tools.development.DevAppServerImpl start
    [java] INFO: The admin console is running at http://192.168.1.10:8080/_ah/admin

    5.在浏览器中打开服务器主页。URL取决于你使用的服务器和你机器的ID地址,但它将会类似这样 http://192.168.1.10:8080/home, /home 是主Servlet的路径。

    注释:你可以通过在Linux或MacOS中运行 ifconfig 或者在Windows中运行ipconfig来获取IP。

    http://developer.android.com/images/gcm/gcm-demo-homepage-appengine.png

    你的服务器现在准备好了。

    设置设备

    设置设备:

    1.从SDK管理器中,安装Extras > Google Cloud Messaging for Android Library。
    这将会在包含这些子目录的YOUR_SDK_ROOT/extras/google 目录下创建一个gcm目录:
    gcm-client, gcm-server, samples/gcm-demo-client, samples/gcm-demo-server, and samples/gcm-demo-appengine。

    2.使用文本编辑器,打开
    samples/gcm-demo-client/src/com/google/android/gcm/demo/app/CommonUtilities.java
    并为SENDER_ID 和 SERVER_URL 常量设置特征值。例如:

    static final String SERVER_URL = "http://192.168.1.10:8080/gcm-demo";
    static final String SENDER_ID = "4815162342";

    注意 SERVER_URL 是服务器的URL,应用环境(或只是服务器,如果你使用App Engine),它并不包含斜杠(/)。同时注意SENDER_ID 是你在服务器设置步骤中获取的谷歌API项目ID。

    3.在命令行窗口,进入gcm-demo-client 目录。

    4.使用SDK的android 工具生成ant 构造文件:

    $ android update project --name GCMDemo -p . --target android-16
    Updated project.properties
    Updated local.properties
    Updated file ./build.xml
    Updated file ./proguard-project.txt

    如果此命令因 android-16 无法识别而失败,尝试不同的目标(至少android-15)

    5.使用ant 创建应用APK文件:

    $ ant clean debug
    Buildfile: build.xml

    ...

    -do-debug:
    [zipalign] Running zip align on final apk...
    [echo] Debug Package: bin/GCMDemo-debug.apk
    [propertyfile] Creating new property file: bin/build.prop
    [propertyfile] Updating property file: bin/build.prop
    [propertyfile] Updating property file: bin/build.prop
    [propertyfile] Updating property file: bin/build.prop

    -post-build:

    debug:

    BUILD SUCCESSFUL
    Total time: 3 seconds

    6.开启安卓模拟器:

    $emulator -avd my_avd

    这个例子呈现了命名为my_avd 的AVD(安卓虚拟设备)用安卓2.2和谷歌API级别8的预先设置。需要更多关于如果运行一个安卓模拟器的信息,查看安卓开发向导中的管理虚拟设备。

    7.确认谷歌账号被添加入模拟器。尤其不能是其他账号(例如senderId )。
    如果模拟器运行安卓4.0.4或更新的,这步是可选的,这个版本GCM不要求账号。

    8.在模拟器中安装应用:

    $ ant installd
    Buildfile: gcm-demo-client/build.xml

    -set-mode-check:

    -set-debug-files:

    install:
    [echo] Installing gcm-demo-client/bin/GCMDemo-debug.apk onto default emulator or device...
    [exec] 1719 KB/s (47158 bytes in 0.026s)
    [exec] pkg: /data/local/tmp/GCMDemo-debug.apk
    [exec] Success

    installd:

    BUILD SUCCESSFUL
    Total time: 3 seconds

    9.在模拟器中,启动GCM样例应用。初始化屏幕会像这样:

    http://developer.android.com/images/gcm/gcm-avd-home-auto-reg.png

    注释:发生什么了?当设备从GCM收到一个注册返回intent,它联系服务器注册它自己,使用注册的servlet 从GCM返回通过的注册ID,服务器保存注册ID并用它向手机发送消息。

    10.现在,返回你的浏览器刷新页面。它将会显示有一个设备已注册。

    http://developer.android.com/images/gcm/gcm-device-reg.png

    11.点击发送消息。浏览器会显示:

    http://developer.android.com/images/gcm/gcm-sent-server.png

    并且在你的模拟器:

    注释:发生什么了?当你点击按钮,网页服务器发送了一条消息到你设备的GCM地址(更特别的是,在注册步骤中GCB返回了注册ID)。设备受到消息在主activity中显示。它将发布一条系统通知,即使样例程序没有运行用户也会被通知到。

    原文:http://wiki.eoe.cn/page/GCM_Demo_Application.html#

  • 相关阅读:
    为调试JavaScript添加输出窗口
    后悔自己没有学好数学
    IEnumeralbe<T>被误用一例
    开发软件真是一件有意思的事情
    在网页上实现WinForm控件:ComboBox
    WinForm异步编程中一个容易忽视的问题
    网页上的DataGridView
    用Excel生成代码
    游戏处女作 打方块
    用GDI+保存Image到流时的一个有趣现象
  • 原文地址:https://www.cnblogs.com/zhudongfang/p/3358834.html
Copyright © 2020-2023  润新知