com.cht.imserver.push.TLPushNotification.java Source code

Java tutorial

Introduction

Here is the source code for com.cht.imserver.push.TLPushNotification.java

Source

package com.cht.imserver.push;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;

/**
 * Push 
 * @author 842
 */
public class TLPushNotification {

    private static final Logger logger = Logger.getLogger(TLPushNotification.class.toString());

    //?APP???842??
    private static final String appname = "CHTIM";
    //842?URL?iOS?Android?
    //??domain name:happizone.com.tw
    //?domain name:sandbox.happizone.com.tw
    private static final String devAndroidHost = "http://sandbox.happizone.com.tw/pushser/sendtoken_android.aspx";
    private static final String deviOSHost = "http://sandbox.happizone.com.tw/pushser/sendtoken_ios.aspx";
    private static final String officialAndroidHost = "http://happizone.com.tw/pushser/sendtoken_android.aspx";
    private static final String officeialiOSHost = "http://happizone.com.tw/pushser/sendtoken_ios.aspx";

    /* iOS??
     * ?
     * token:ios?device token
     * message:?APP?iOS?????
     * lockey:APP???key?APP
     *       ???nullkeyGAME_PLAY_REQUEST_FORMATAPP
     *       "GAME_PLAY_REQUEST_FORMAT" = "%@ and %@ have invited you to play Monopoly";
     * locargs:APP???APP???
     *       ??json???"  ["Jenna", "Frank"] (?) "Jenna" (?)
     *       ???UnsupportedEncodingException?
     * sound:? ""
     * badge:1
     * licenseKey: 842key
     * PNServerType: ????, iOSAndroid????PNServerType enum??
     * proxy: ???proxy??
     * port: proxyport
     * 
     * PNResult:
     *    error_code:?
     *             0: ?iOS token
     *             100: ?iOS
     *             900: 
     *  description:
     *  pushuid: UID
     * 
     * IOException:???
     * UnsupportedEncodingException:?http POST???
     * ClientProtocolException: http???
    */
    public static PNResult pushMessage_iOS(String token, String message, String lockey, String locargs,
            String sound, int badge, String licenseKey, PNServerType type, String proxy, int port)
            throws IOException, UnsupportedEncodingException, ClientProtocolException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpHost httpproxy = null;
        String serverURL = null;
        PNResult result = null;
        if (type == PNServerType.ios_dev) {
            serverURL = deviOSHost;
        } else if (type == PNServerType.ios_official) {
            serverURL = officeialiOSHost;
        }
        String jsontoken = "{\"iosTokens\":[{\"token\":\"" + token + "\",\"badge\" : " + String.valueOf(badge)
                + "}]}";
        String jsonmessage = null;
        if (lockey == null || "".equals(lockey)) {
            jsonmessage = "{\"message\":\"" + message + "\",\"sound\":\"" + sound + "\"}";
        } else {
            jsonmessage = "{\"message\":\"" + message + "\",\"sound\":\"" + sound + "\",\"loc-key\":\"" + lockey
                    + "\",\"loc-args\":" + locargs + "}";
        }
        //System.out.println("iosdata=" + jsonmessage);
        //System.out.println("iostoken=" + jsontoken);

        //logger.info("jsonmessage:" + jsonmessage + ", jsontoken:" + jsontoken + ", licenseKey:" + licenseKey );
        logger.info("jsonmessage:" + jsonmessage + ", licenseKey:" + licenseKey);

        try {
            HttpPost httpPost = new HttpPost(serverURL);
            if (proxy != null) {
                httpproxy = new HttpHost(proxy, port);
                RequestConfig config = RequestConfig.custom().setProxy(httpproxy).build();
                httpPost.setConfig(config);
            }
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("iosdata", jsonmessage));
            nvps.add(new BasicNameValuePair("iostoken", jsontoken));
            nvps.add(new BasicNameValuePair("licensekey", licenseKey));
            nvps.add(new BasicNameValuePair("appname", appname));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            //System.out.println(EntityUtils.toString(httpPost.getEntity()) );
            CloseableHttpResponse response2 = httpclient.execute(httpPost);
            Gson mGson = new Gson();
            try {
                //System.out.println(response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                //System.out.println(EntityUtils.toString(entity2));
                result = mGson.fromJson(EntityUtils.toString(entity2), PNResult.class);

                EntityUtils.consume(entity2);

            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
        return result;

    }

    /* Android??
     * ?
     * token:Android?device token
     * message:?APP??APP?APP
     * sender: ?
     * licenseKey: 842key
     * PNServerType: ?? ??, iOSAndroid????PNServerType enum??
     * proxy: ???proxy??
     * port: proxyport
     * 
     * PNResult:
     *    error_code:?
     *             0: ?Android token
     *             100: ?Android
     *             900: 
     *  description:
     *  pushuid: UID
     * 
     * IOException:???
     * UnsupportedEncodingException:?http POST???
     * ClientProtocolException: http???
    */
    public static PNResult pushMessage_Android(String token, String message, String sender, String licenseKey,
            PNServerType type, String proxy, int port)
            throws IOException, UnsupportedEncodingException, ClientProtocolException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpHost httpproxy = null;
        String serverURL = null;
        PNResult result = null;
        if (type == PNServerType.android_dev) {
            serverURL = devAndroidHost;
        } else if (type == PNServerType.android_official) {
            serverURL = officialAndroidHost;
        }
        String jsontoken = "{\"androidTokens\":[{\"token\":\"" + token + "\"}]}";
        String jsonmessage = "{\"sender\":\"" + sender + "\",\"message\":\"" + message + "\"}";
        //System.out.println("androiddata=" + jsonmessage);
        //System.out.println("androidtoken=" + jsontoken);

        //logger.info("jsonmessage:" + jsonmessage + ", jsontoken:" + jsontoken + ", licenseKey:" + licenseKey );
        logger.info("jsonmessage:" + jsonmessage + ", licenseKey:" + licenseKey);

        try {
            HttpPost httpPost = new HttpPost(serverURL);
            if (proxy != null) {
                httpproxy = new HttpHost(proxy, port);
                RequestConfig config = RequestConfig.custom().setProxy(httpproxy).build();
                httpPost.setConfig(config);
            }
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("androiddata", jsonmessage));
            nvps.add(new BasicNameValuePair("androidtoken", jsontoken));
            nvps.add(new BasicNameValuePair("licensekey", licenseKey));
            nvps.add(new BasicNameValuePair("appname", appname));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            //System.out.println(EntityUtils.toString(httpPost.getEntity()) );

            CloseableHttpResponse response2 = httpclient.execute(httpPost);
            Gson mGson = new Gson();
            try {
                //System.out.println(response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                //System.out.println(EntityUtils.toString(entity2));
                result = mGson.fromJson(EntityUtils.toString(entity2), PNResult.class);

                EntityUtils.consume(entity2);

            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
        return result;
    }
}