1

可能重复:
向 C2DM 注册 Android 应用程序

所以,我一直在四处寻找,阅读所有我能拿到的东西,以检查如何将 C2DM 与我的超酷聊天应用程序一起使用。

我看到我的服务器人员必须处理各种各样的事情,但我没有理解一件事。

注册过程必须包含一个 senderId,它基本上是一个 Google ID,它作为正在使用该应用的应用(或用户)被传输到 Google 服务器,这将您标识为推送客户端。

我的问题是,我是否必须通过注册对话框提示用户?这对我的用户来说似乎是一件可怕的事情,因为该应用程序已经使用 Facebook 连接并且提示太多这些对用户怀有敌意,并且肯定会让他们卸载该应用程序。

一个应用程序注册C2DM的过程是什么?以及如何使用 Google Play 使用的现有身份验证令牌?


我(第三次)阅读了 Vogella 关于使用 C2DM 的教程,这是我提出问题的基础:

public void register(View view) {
    Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
    intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
    intent.putExtra("sender", "youruser@gmail.com");  //WHICH EMAIL?
    startService(intent);
}

第 4 行中使用的电子邮件是设备所有者的电子邮件还是服务电子邮件?

如果我们谈论的是用户,有没有办法在没有另一个身份验证对话框的情况下获得它?我已经有 Facebook 登录,我不想让用户感到不适。

4

2 回答 2

1

请参阅 vogella 的文章,非常简短的 C2DM 实施指南。

http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html

那里提到的所有东西以及代码,我个人都实现了它的代码,并且运行良好。

于 2012-06-26T12:07:56.093 回答
0

首先你需要注册google的C2DM

其次为您的 C2DM 应用程序获取 Auth 令牌

function(){
        // Create the post data
        // Requires a field with the email and the password
        StringBuilder builder = new StringBuilder();
        builder.append("Email=").append(email);
        builder.append("&Passwd=").append(password);
        builder.append("&accountType=GOOGLE");
        builder.append("&source=MyLittleExample");
        builder.append("&service=ac2dm");

        // Setup the Http Post
        byte[] data = builder.toString().getBytes();
        URL url = new URL("https://www.google.com/accounts/ClientLogin");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setUseCaches(false);
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        con.setRequestProperty("Content-Length", Integer.toString(data.length));

        // Issue the HTTP POST request
        OutputStream output = con.getOutputStream();
        output.write(data);
        output.close();

        // Read the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line = null;
        String auth_key = null;
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("Auth=")) {
                auth_key = line.substring(5);
            }
        }

        // Finally get the authentication token
        // To something useful with it
        return auth_key;
}

现在您需要将客户端移动设备注册到 C2DM 以接收更新

public void register(View view) {
    Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
    intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
    intent.putExtra("sender", "youruser@gmail.com");
    startService(intent);
}

该服务将异步向 Google 注册,并在成功注册后发送“ com.google.android.c2dm.intent.REGISTRATION ”意图。您的应用程序需要为此意图注册一个广播接收器。这也需要使用基于您的包的权限,因为 Android 系统会在内部对此进行检查。

<receiver android:name=".C2DMMessageReceiver"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE"></action>
        <category android:name="de.vogella.android.c2dm.simpleclient" />
    </intent-filter>
</receiver>

//

public class C2DMRegistrationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Log.w("C2DM", "Registration Receiver called");
    if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
        Log.w("C2DM", "Received registration ID");
        final String registrationId = intent
                .getStringExtra("registration_id");
        String error = intent.getStringExtra("error");

        Log.d("C2DM", "dmControl: registrationId = " + registrationId
                + ", error = " + error);
        // Send and store this in your application server(unique for each device)
    }
}
}

现在您可以开始通过您的服务器发送 C2DM 消息

private final static String AUTH = "authentication";

    private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";

    public static final String PARAM_REGISTRATION_ID = "registration_id";

    public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";

    public static final String PARAM_COLLAPSE_KEY = "collapse_key";

    private static final String UTF8 = "UTF-8";

    public static int sendMessage(String auth_token, String registrationId,
            String message) throws IOException {

        StringBuilder postDataBuilder = new StringBuilder();
        postDataBuilder.append(PARAM_REGISTRATION_ID).append("=")
                .append(registrationId);
        postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=")
                .append("0");
        postDataBuilder.append("&").append("data.payload").append("=")
                .append(URLEncoder.encode(message, UTF8));

        byte[] postData = postDataBuilder.toString().getBytes(UTF8);

        // Hit the dm URL.

        URL url = new URL("https://android.clients.google.com/c2dm/send");
        HttpsURLConnection
                .setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
        conn.setRequestProperty("Content-Length",
                Integer.toString(postData.length));
        conn.setRequestProperty("Authorization", "GoogleLogin auth="
                + auth_token);

        OutputStream out = conn.getOutputStream();
        out.write(postData);
        out.close();

        int responseCode = conn.getResponseCode();
        return responseCode;
    }

参考

于 2012-06-26T12:25:30.347 回答