从谷歌应用程序引擎推消息C2DM服务器应用程序、消息、服务器、引擎

2023-09-07 11:32:00 作者:ゾ不服的,来单挑

我已经能够

获取服务器authrization,并保存到数据存储; 注册手机C2DM服务器; 发送ID到保存应用程序C2DM regeistration ID为数据存储中的应用服务器。

现在我只想为实施检索服务器令牌的一个servlet。从数据存储Android应用程序regirstration ID,并用它们来推动一个消息给手机。

这是code为servlet:

This is the code for the servlet:

package com.visd.myfirstapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

//import com.visd.myfirstapp.MessageUtil.CustomizedHostnameVerifier;

public class Visd extends HttpServlet {
    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 void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException 
    {
        resp.setContentType("text/plain");
        Entity appRegIdEntity = null;
        Entity serverTokenEntity = null;
        int RetCode = 0;
        String message = "Congrats C2DM process completed";
        Key appRegIdKEY = KeyFactory.createKey("c2dmreg","cr");
        Key serverTokenKEY = KeyFactory.createKey("vToken", "tokenkn");
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        String appRegId = null, serverToken = null, chk =null;
        try {
             appRegIdEntity = datastore.get(appRegIdKEY);
             serverTokenEntity = datastore.get(serverTokenKEY);
             serverToken = (String) serverTokenEntity.getProperty("token");
             appRegId = (String) appRegIdEntity.getProperty("c2dmid");


             RetCode = sendMessage(serverToken, appRegId, message);

        } catch (EntityNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            chk = "Entered In Exception";       

        }  
        resp.getWriter().println("Return code :" + RetCode + "chk value :" + chk);
    }



    //  Message Sending method

    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);



        URL url = new URL("https://android.clients.google.com/c2dm/send");
        //HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());//commented as was causing error, i dont know why
        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;
    }


}

不过,浏览器总是显示惩戒code = 0,清洁香港值=输入的异常i.e.It从来没有将消息发送到Android设备上,而不是总是在异常进入。有什么错在code我无法弄清楚..结果请帮忙。谢谢你。

But the browser always shows the RetCode = 0 and Chk value = "Entered in Exception" i.e.It never sends the message to the android device, instead always enters in the exception. What wrong in the code i couldn't figure out.. please help. Thank you.

推荐答案

这是我如何终于解决了,code帮助: -

This is how I finally solved, code help :-

public class C2dmsender {
    public static String send(String regid, String appRegId, String mtype, String[] message) throws UnsupportedEncodingException
    {


        String serverToken = ""//give the sever token here;
    data.append("registration_id=" + appRegId);//appRegId is the C2DM id of the device in which you want to push

    // Collapse key is for grouping messages and only the last sent message
    // with the same key going to be sent to the phone when the phone is
    // ready to get the message if its not from the beginning
    data.append("&collapse_key=test");

    // Here is the message we sending, key1 can be changed to what you whant
    // or if you whant to send more then one you can do (i think, not tested
    // yet), Testing is the message here.
    data.append("&data.key1=");

    // If you whant the message to wait to the phone is not idle then set
    // this parameter
    // data.append("&delay_while_idle=1");

    byte[] postData = data.toString().getBytes("UTF-8");


    try {
        // Send data
        URL url = new URL("https://android.apis.google.com/c2dm/send");

        HttpURLConnection conn = (HttpURLConnection) 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="
                + serverToken);

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

        Integer responseCode = conn.getResponseCode();
        if (responseCode.equals(503)) {
            // the server is temporarily unavailable

        } else {
            if (responseCode.equals(401)) {
                // AUTH_TOKEN used to validate the sender is invalid

            } else {
                if (responseCode.equals(200)) {

                    // Check for updated token header
                    String updatedAuthToken = conn
                            .getHeaderField("Update-Client-Auth");
                    if (updatedAuthToken != null) {
                        serverToken = updatedAuthToken;

                    }

                    String responseLine = new BufferedReader(
                            new InputStreamReader(conn.getInputStream()))
                            .readLine();


            }
        }
    } catch (Exception e) {

        }


    }

}