1. 程式人生 > >java遠端呼叫JPush極光推送api

java遠端呼叫JPush極光推送api

謝謝大家關注此博文,很多人都問原始碼,但是很抱歉,原始碼已經找不到了,具體可以參考官網的api,裡面很詳細的。

呼叫JPush推送的api,官網上已經有很詳細的說明了。這裡主要描述一下我在呼叫過程中遇到的問題,

比如

我的HttpClient不支援https的請求、

引數配置錯誤 和 驗證錯誤   等等。

開發的時候需要引用appache的包commons-httpclient.jar 、commons-codec.jar、commons-logging.jar這些包可以到官網上下載,如果有需要的話也我也可以發給你。

引入上述這些包之後,就可以進行開發了。

這裡需要特別說明的兩點是:

1、通過 HttpClient client = new DefaultHttpClient(); 獲得HttpClient物件不支援https,需要自己重寫。

2、我們的MD5編碼要和伺服器那邊的一樣。(我用我自己寫的MD5編碼驗證的時候,總是驗證失敗,後來跟他們的技術人員要了他們的md5的實現方式就通過驗證了)

好了,廢話不多說了,接下來是貼程式碼的時候了:

類MySSLSocketFactory用來實現對https的支援

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ssl.SSLSocketFactory;

public class MySSLSocketFactory extends SSLSocketFactory {

	    SSLContext sslContext = SSLContext.getInstance("TLS");  
	  
	    public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {  
	        super(truststore);  
	  
	        TrustManager tm = new X509TrustManager() {  
	            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
	            }  
	  
	            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
	            }  
	  
	            public X509Certificate[] getAcceptedIssuers() {  
	                return null;  
	            }  
	        };  
	  
	        sslContext.init(null, new TrustManager[] { tm }, null);  
	    }  
	  
	    @Override  
	    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {  
	        return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);  
	    }  
	  
	    @Override  
	    public Socket createSocket() throws IOException {  
	        return sslContext.getSocketFactory().createSocket();  
	    }  

}

類ClientUtil 獲取可以支援https的HttpClient物件,呼叫MySSLSocketFactory來取得
import java.security.KeyStore;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
public class ClientUtil {
	
	public static HttpClient getNewHttpClient() {
		 try {  
		        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());  
		        trustStore.load(null, null);  
		  
		        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);  
		        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
		  
		        HttpParams params = new BasicHttpParams();  
		        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);  
		        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);  
		  
		        SchemeRegistry registry = new SchemeRegistry();  
		        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));  
		        registry.register(new Scheme("https", sf, 443));  
		  
		        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);  
		  
		        return new DefaultHttpClient(ccm, params);  
		    } catch (Exception e) {
		    	e.printStackTrace();
		        return new DefaultHttpClient();  
		    }  
	}

}

接下來就是呼叫JPush的api來推送訊息了
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 呼叫遠端api實現推送
 * @author naiyu
 *
 */
public class PushMsgUtil {
	
//	public static final String PUSH_URL = "https://api.jpush.cn:443/sendmsg/sendmsg";
	public static final String PUSH_URL = "http://api.jpush.cn:8800/sendmsg/sendmsg";
	
	public static void pushMsg(String msg) {
		BasicNameValuePair name = new BasicNameValuePair("username", "test");  //使用者名稱
		BasicNameValuePair sendno = new BasicNameValuePair("sendno", "3621");  // 傳送編號。由開發者自己維護,標識一次傳送請求
		BasicNameValuePair appkeys = new BasicNameValuePair("appkeys", "your appkeys");  // 待發送的應用程式(appKey),只能填一個。
		BasicNameValuePair receiver_type = new BasicNameValuePair("receiver_type", "4");  
		//驗證串,用於校驗傳送的合法性。
		BasicNameValuePair verification_code = new BasicNameValuePair("verification_code", getVerificationCode());
		//傳送訊息的型別:1 通知 2 自定義
		BasicNameValuePair msg_type = new BasicNameValuePair("msg_type", "1");
		BasicNameValuePair msg_content = new BasicNameValuePair("msg_content", msg);
		//目標使用者終端手機的平臺型別,如: android, ios 多個請使用逗號分隔。
		BasicNameValuePair platform = new BasicNameValuePair("platform", "android");
		List<BasicNameValuePair> datas = new ArrayList<BasicNameValuePair>();
		datas.add(name);
		datas.add(sendno);
		datas.add(appkeys);
		datas.add(receiver_type);
		datas.add(verification_code);
		datas.add(msg_type);
		datas.add(msg_content);
		datas.add(platform);
		try {
			HttpEntity entity = new UrlEncodedFormEntity(datas, "utf-8");
			HttpPost post = new HttpPost(PUSH_URL);
			post.setEntity(entity);
			HttpClient client = ClientUtil.getNewHttpClient();
			HttpResponse reponse = client.execute(post);
			HttpEntity resEntity = reponse.getEntity();
			System.out.println(EntityUtils.toString(resEntity));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
	}
	
	
	private static String getVerificationCode() {

		String username = "test";  //username 是開發者Portal帳戶的登入帳戶名
		String password = "pasword";
		int sendno = 3621;
		int receiverType = 4;
		String md5Password = StringUtils.toMD5(password);; //password 是開發者Portal帳戶的登入密碼
		 
		String input = username + sendno + receiverType + md5Password;
		String verificationCode = StringUtils.toMD5(input);
		return verificationCode;
	}
	
	public static void main(String[] args) {
		String msg = "{\"n_title\":\"來點外賣\",\"n_content\":\"你好\"}";
		System.out.println(msg);
		PushMsgUtil.pushMsg(msg);
	}

}

執行成功:


附上StringUtils,java

import java.security.MessageDigest;

public class StringUtils {
	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };

	private static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String toMD5(String origin) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteArrayToHexString(md.digest(resultString
					.getBytes()));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return resultString;
	}
}