Last active
April 17, 2017 10:28
-
-
Save rahulpandey/ee1f98e73934e801f8e1d35ade375fe0 to your computer and use it in GitHub Desktop.
An efficient utility class for sending Apple push notification (Apns) over HTTP 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.mycompany</groupId> | |
<artifactId>AppleNotificationH2</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
<alpn-boot-version>8.1.6.v20151105</alpn-boot-version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>com.squareup.okhttp3</groupId> | |
<artifactId>okhttp</artifactId> | |
<version>3.4.1</version> | |
</dependency> | |
<dependency> | |
<groupId>com.squareup.okhttp3</groupId> | |
<artifactId>logging-interceptor</artifactId> | |
<version>3.4.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.security.KeyManagementException; | |
import java.security.KeyStore; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.security.UnrecoverableKeyException; | |
import java.security.cert.CertificateException; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.concurrent.TimeUnit; | |
import javax.net.ssl.HostnameVerifier; | |
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.KeyManager; | |
import javax.net.ssl.KeyManagerFactory; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSession; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.TrustManagerFactory; | |
import javax.net.ssl.X509TrustManager; | |
import okhttp3.CipherSuite; | |
import okhttp3.ConnectionSpec; | |
import okhttp3.MediaType; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Protocol; | |
import okhttp3.Request; | |
import okhttp3.RequestBody; | |
import okhttp3.Response; | |
import okhttp3.TlsVersion; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
public class PushClass { | |
public static final MediaType JSON | |
= MediaType.parse("application/json; charset=utf-8"); | |
private static final String url = "https://api.development.push.apple.com/3/device/DEVICE_ID"; | |
private static final String json = "{\"aps\" :{\"alert\" : \"Hello\"}}"; | |
public static void main(String[] args) throws IOException { | |
OkHttpClient client = getOkHttpClient(); | |
RequestBody body = RequestBody.create(JSON, json); | |
Request request = new Request.Builder() | |
.url(url) | |
.post(body) | |
.build(); | |
Response response = client.newCall(request).execute(); | |
System.out.println("Response=>" + response.body().string()); | |
} | |
private static final int CONNECT_TIMEOUT_MILLIS = 2 * 60; // 2 minute | |
private static final int READ_TIMEOUT_MILLIS = 2 * 60; // 2 minute | |
private static OkHttpClient getOkHttpClient() { | |
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); | |
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | |
OkHttpClient.Builder builder = new OkHttpClient.Builder() | |
.protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1)) | |
.retryOnConnectionFailure(true); | |
//if (BuildConfig.DEBUG) { | |
try { | |
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) | |
.tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1) | |
.cipherSuites(CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, | |
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, | |
CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384, | |
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, | |
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, | |
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, | |
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) | |
.build(); | |
builder.connectionSpecs(Collections.singletonList(spec)); | |
builder.sslSocketFactory(getSslContext().getSocketFactory(), getTrustManagers()); | |
builder.hostnameVerifier(hostNameVerifire()); | |
} catch (CertificateException | KeyStoreException | NoSuchAlgorithmException | KeyManagementException | IOException | UnrecoverableKeyException e) { | |
e.printStackTrace(); | |
} | |
builder.addNetworkInterceptor(interceptor); | |
// } | |
return builder.readTimeout(READ_TIMEOUT_MILLIS, TimeUnit.SECONDS) | |
.connectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.SECONDS) | |
.build(); | |
} | |
private static SSLContext getSslContext() throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException { | |
KeyStore clientStore = KeyStore.getInstance("PKCS12"); | |
clientStore.load(new FileInputStream("CERT_FILE_PATH"), "PASSWORD".toCharArray()); | |
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | |
kmf.init(clientStore, "PASSWORD".toCharArray()); | |
KeyManager[] kms = kmf.getKeyManagers(); | |
X509TrustManager tms = getTrustManagers(); | |
SSLContext sslContext; | |
sslContext = SSLContext.getInstance("TLS"); | |
sslContext.init(kms, new TrustManager[]{tms}, new SecureRandom()); | |
return sslContext; | |
} | |
private static X509TrustManager getTrustManagers() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { | |
String keyStoreType = KeyStore.getDefaultType(); | |
KeyStore trustStore = KeyStore.getInstance(keyStoreType); | |
trustStore.load(null, null); | |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | |
tmf.init((KeyStore) null); | |
TrustManager[] trustManagers = tmf.getTrustManagers(); | |
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) { | |
throw new IllegalStateException("Unexpected default trust managers:" | |
+ Arrays.toString(trustManagers)); | |
} | |
return (X509TrustManager) trustManagers[0]; | |
} | |
public static HostnameVerifier hostNameVerifire() { | |
HostnameVerifier hostnameVerifier = new HostnameVerifier() { | |
@Override | |
public boolean verify(String hostname, SSLSession session) { | |
// Log.i(TAG, "HOST NAME " + hostname); | |
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); | |
return true; | |
} | |
}; | |
return hostnameVerifier; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you are using net beans, add ALPN JAR in VM option by right-clicking on
project-properties-run; in VM option add -Xbootclasspath/p:ALPN_BOOT_JAR_PATH.
Make sure ALPN boot jar is loaded in vm.