加入谷歌云Messagin(GCM)为Android - 注册过程 [英] Adding Google Cloud Messagin (GCM) for Android - Registration process

查看:226
本文介绍了加入谷歌云Messagin(GCM)为Android - 注册过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在挣扎GCM实现了几个星期了,但我怎么真的想了解它是如何工作,我决定采取小步。

首先,正如这里,我明白,要做的第一件事在注册设备/应用程序必须先注册GCM。

  

要验证他们是否可以发送和接收邮件,客户端应用程序必须GCM注册。在这个过程中,客户端获得一个唯一的注册标记。

我想知道如果跌破重新present的程序和code所必需的非常小的code键使这种登记(以下简称很小的$ C $必需C 是因为,当我得知的OpenGL ES 2.0,并开始处理着色器,我看到,当硬/容易混淆的概念打交道,如果你理解所必需的最低限度code,可后来明白了外围code)

程序来registrate与GCM的应用程序:

  1。建立在谷歌开发者控制台项目;
 1.1。激活云通讯Android的API;
 1.2。在创建信用凭证服务器API密钥;
 1.3。请注意该项目的ID;
 1.4。请注意的项目数量;
 1.5。请注意服务器API密钥的;

2.创建的Andr​​oid Studio项目;
 2.1添加一个App Engine的后端与CGM模块类型;
 2.2关于AppEngine上 -  web.xml中窗口将打开,输入应用程序ID和服务器API密钥。
 2.3创建一个主要活动,通过项目编号(SENDER_ID)。
 

在我没有这一切,运行应用程序,我用152个字符的令牌。 是所有正确的吗?考虑到我有一个记号回来,是我与GCM?注册设备

AppEngine上-web.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
<应用程序引擎,web应用程序的xmlns =htt​​p://appengine.google.com/ns/1.0>
    <应用> mygcmtest ...< /用途>
    <版> 1< /版本>
    <线程>真< /线程>

    <系统 - 性状>
        <属性名=java.util.logging.config.file值=WEB-INF / logging.properties/>

        <属性名=gcm.api.key值=AIza ....../>
    < /系统性状>
< / AppEngine上-web-app的>
 

MainActivity:

 公共类MainActivity扩展AppCompatActivity {

    私人最终上下文mContext =这一点;
    私人最终字符串SENDER_ID =319899 ......;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        getGCMToken();
    }

    私人无效getGCMToken(){
        新的AsyncTask<虚空,虚空,虚空>(){
            @覆盖
            保护无效doInBackground(虚空...... PARAMS){
                尝试 {
                    实例id =实例ID InstanceID.getInstance(mContext);
                    字符串标记= instanceID.getToken(SENDER_ID,GoogleCloudMessaging.INSTANCE_ID_SCOPE,NULL);
                    Log.e(GCM令牌,令牌);
                }赶上(IOException异常E){
                    e.printStackTrace();
                }
                返回null;
            }
        }。执行();
    }
}
 

清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.example.aninha.mygcmtest ......>

    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.GET_ACCOUNTS/>
    <使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK/>
    <使用-权限的Andr​​oid:名称=com.google.android.c2dm.permission.RECEIVE/>

    <许可
        机器人:名称=com.example.gcm.permission.C2D_MESSAGE
        安卓的ProtectionLevel =签名/>

    <使用-权限的Andr​​oid:名称=com.example.gcm.permission.C2D_MESSAGE/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@纹理映射/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=。MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

解决方案

是我与源$ C ​​$ C回答你的问题<一href="http://stackoverflow.com/questions/32299997/how-to-implement-a-gcm-hello-world-for-android-using-android-studio">How采用Android工作室是不够的,你要创建两个简单的GCM项目(服务器端和客户端)尚未实现GCM的Hello World为Android? :)

当然,样本code我用的是只是一个很基本的情况下的服务器应用程序发送,客户端应用程序接收并显示消息的。

综上所述,你需要做以下步骤:

  1. 创建于谷歌开发者控制台一个新的项目。在这 一步,为简单起见,你只需要取2个值注: 项目数 ,其中 将用作 SENDER_ID 在我的客户的项目;和 API服务器键 (在创建的证书),这 将用作 API_KEY 在我的服务器项目。
  2. 创建用于服务器端一个新的简单的Andr​​oid项目(基本源$ C ​​$ C作为我在你的previous问题的答案)。
  3. 创建为客户端一个新的简单的Andr​​oid项目(基本源$ C ​​$ C作为我在你的previous问题的答案,我从在的谷歌云消息传递 - GitHub的
  4. 运行客户端应用程序,你会得到的注册标记(意思是您的设备已成功注册)。然后,粘贴(硬code)此标记在 CLIENT_REGISTRATION_TOKEN 在服务器上的应用程序变量。
  5. 运行服务器应用程序,并检查结果(客户端应用程序接收到的短消息。)

希望这有助于!

P / S:我不使用任何 AppEngine上-web.xml中文件

I have been struggling with GCM implementation for some weeks now, but how I really want to get to understand how it works I decided to take 'baby steps'.

First of all, as mentioned here, I understood that the first thing to do is register my device/app must first register with GCM.

To verify that they can send and receive messages, client apps must register with GCM. In this process, the client obtains a unique registration token.

I'd like to know if the procedure and code below represent the very minimal code necessary to make such registration (the 'very minimal code necessary' is because when I learned OpenGL ES 2.0 and started to deal with shaders, I saw that when dealing with hard/confusing concepts, If you understand the minimal code necessary you can later understand what the "peripheral" code)

Procedure to registrate the app with GCM:

1. Create a project at Google Developer Console;
 1.1. Activate "Cloud Messaging for Android" API;
 1.2. Create a server API key at "Credentials";
 1.3. Take note of the project ID;
 1.4. Take note of the project number;
 1.5. Take note of the server API key;

2. Create a Android Studio project;
 2.1 Add an "App Engine Backend with CGM" module type;
 2.2 On the "appengine-web.xml" window that will open, type in the application ID and the server API Key.
 2.3 Create a main activity, using the project number (SENDER_ID).

After I didn all of that and run the app, I got a token with 152 characters. Is all of that correct? Considering that I got a token back, is my device registered with GCM?

appengine-web.xml:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>mygcmtest...</application>
    <version>1</version>
    <threadsafe>true</threadsafe>

    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />

        <property name="gcm.api.key" value="AIza..." />
    </system-properties>
</appengine-web-app>

MainActivity:

public class MainActivity extends AppCompatActivity {

    private final Context mContext = this;
    private final String SENDER_ID = "319899...";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getGCMToken();
    }

    private void getGCMToken() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    InstanceID instanceID = InstanceID.getInstance(mContext);
                    String token = instanceID.getToken(SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                    Log.e("GCM Token", token);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
    }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aninha.mygcmtest..." >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.example.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

解决方案

Is my answer with source code at your question How to implement a GCM Hello World for Android using Android Studio is not enough for you to create two simple GCM projects (server-side and client-side) yet? :)

Of course, the sample code I used is just for a very basic case "server app sends, client app receives and displays message".

To sum up, you need to do the following steps:

  1. Create a new project at Google Developers Console . At this step, for simplicity, you just need to take note of 2 values: Project Number, which will be used as SENDER_ID in my client project; and API server key (created at Credentials), which will be used as API_KEY in my server project.
  2. Create a new simple Android project for server side (with basic source code as my answer in your previous question).
  3. Create a new simple Android project for client side (with basic source code as my answer in your previous question, I customized from the original source at Google Cloud Messaging - GitHub).
  4. Run the client app, you will get the registration token (means that your device has successfully registered). Then, paste (hard-code) this token at CLIENT_REGISTRATION_TOKEN variable in server app.
  5. Run the server app, and check the result (client app received the message or not)

Hope this helps!

P/S: I don't use any appengine-web.xml file

这篇关于加入谷歌云Messagin(GCM)为Android - 注册过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆