diff --git a/pom.xml b/pom.xml index e9a0c78..795aa72 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.clevertap.apns apns-http2 - 1.0.3 + 1.0.4 apns-http2 A library for communicating with the Apple Push Gateway in HTTP/2. @@ -34,7 +34,7 @@ com.squareup.okhttp3 okhttp - 3.2.0 + 3.10.0 diff --git a/src/main/java/com/clevertap/apns/clients/SyncOkHttpApnsClient.java b/src/main/java/com/clevertap/apns/clients/SyncOkHttpApnsClient.java index 3ba261c..591d9b1 100644 --- a/src/main/java/com/clevertap/apns/clients/SyncOkHttpApnsClient.java +++ b/src/main/java/com/clevertap/apns/clients/SyncOkHttpApnsClient.java @@ -43,6 +43,7 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.security.spec.InvalidKeySpecException; +import java.util.Arrays; import java.util.UUID; /** @@ -177,12 +178,20 @@ public SyncOkHttpApnsClient(InputStream certificate, String password, boolean pr SSLContext sslContext = SSLContext.getInstance("TLS"); final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); - tmf.init((KeyStore) null); + tmf.init(ks); sslContext.init(keyManagers, tmf.getTrustManagers(), null); final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); - - builder.sslSocketFactory(sslSocketFactory); + TrustManagerFactory trustManagerFactory = + TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + trustManagerFactory.init((KeyStore) null); + TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); + if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) { + throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers)); + } + X509TrustManager trustManager = (X509TrustManager) trustManagers[0]; + sslContext.init(null, new TrustManager[]{trustManager}, null); + builder.sslSocketFactory(sslSocketFactory, trustManager); client = builder.build(); diff --git a/src/test/com/clevertap/apns/integration/NotificationTest.java b/src/test/com/clevertap/apns/integration/NotificationTest.java new file mode 100644 index 0000000..e2cf22e --- /dev/null +++ b/src/test/com/clevertap/apns/integration/NotificationTest.java @@ -0,0 +1,60 @@ +package com.clevertap.apns.integration; + +import com.clevertap.apns.ApnsClient; +import com.clevertap.apns.Notification; +import com.clevertap.apns.NotificationResponse; +import com.clevertap.apns.NotificationResponseListener; +import com.clevertap.apns.clients.ApnsClientBuilder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; + +public class NotificationTest { + private static String APN_AUTH_KEY; + private static String TOKEN; + private static String TEAM_ID; + private static String KEY_ID; + + ApnsClient asyncClient; + @BeforeClass + public static void init(){ + APN_AUTH_KEY = System.getProperty("com.clevertap.auth_key"); + TOKEN = System.getProperty("com.clevertap.token"); + TEAM_ID = System.getProperty("com.clevertap.team_id"); + KEY_ID = System.getProperty("com.clevertap.key_id"); + } + @Before + public void setUp() throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException { + asyncClient = new ApnsClientBuilder(). + inAsynchronousMode(). + withApnsAuthKey(APN_AUTH_KEY). + withTeamID(TEAM_ID). + withKeyID(KEY_ID). + build(); + } + @Test + public void sendNotification(){ + Notification notification = new Notification.Builder(TOKEN).alertBody("Hello").build(); + + asyncClient.push(notification, new NotificationResponseListener() { + @Override + public void onSuccess(Notification notification) { + + } + + @Override + public void onFailure(Notification notification, NotificationResponse response) { + Assert.fail("Failed to send notification: "+response.getResponseBody()); + } + }); + } + +}