Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add voip #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.clevertap.apns</groupId>
<artifactId>apns-http2</artifactId>
<version>1.0.7</version>
<artifactId>apns-http2-madlogic</artifactId>
<version>1.0.8</version>

<name>apns-http2</name>
<name>apns-http2-madlogic</name>
<description>A library for communicating with the Apple Push Gateway in HTTP/2.</description>
<url>https://github.com/CleverTap/apns-http2</url>

Expand Down Expand Up @@ -81,7 +81,6 @@
</execution>
</executions>
</plugin>
<!-- this plugin is for creating fat jar (jar with embedded dependencies). -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
Expand All @@ -106,9 +105,9 @@

<distributionManagement>
<repository>
<id>bintray-clevertap-Maven</id>
<name>clevertap-Maven</name>
<url>https://api.bintray.com/maven/clevertap/Maven/apns-http2/;publish=1</url>
<id>madlogic-nexus</id>
<name>Madlogic-Nexus</name>
<url>https://nexus.sqoony.com/nexus/content/repositories/apns-http2-madlogic/</url>
</repository>
</distributionManagement>

Expand Down
25 changes: 18 additions & 7 deletions src/main/java/com/clevertap/apns/CertificateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static Map<String, String> splitCertificateSubject(String subject) {
* @param production Validate the certificate environment, if required
* @param certificate The certificate to be validated
* @throws CertificateException When the certificate is not valid, or if it's expired,
* or if it's not a push certificate
* or if it's not a push or voip certificate
*/
public static void validateCertificate(boolean production, X509Certificate certificate)
throws CertificateException {
Expand All @@ -131,14 +131,25 @@ public static void validateCertificate(boolean production, X509Certificate certi
// Ensure that it's a push certificate
final Map<String, String> stringStringMap = CertificateUtils.splitCertificateSubject(certificate.getSubjectDN().getName());
final String cn = stringStringMap.get("CN");
if (!cn.toLowerCase().contains("push")) {
if (!cn.toLowerCase().contains("push") && !cn.toLowerCase().contains("voip")) {
throw new CertificateException("Not a push certificate - " + cn);
}
}

if (production && cn.toLowerCase().contains("apple development ios push services")) {
throw new CertificateEnvironmentMismatchException("Invalid environment for this certificate");
} else if (!production && cn.toLowerCase().contains("apple production ios push services")) {
throw new CertificateEnvironmentMismatchException("Invalid environment for this certificate");
}
/**
* Checks whether the given certificate is a 'voip' certificate
*
* @param certificate The certificate to be checked
* @throws CertificateException When the certificate is not valid, or if it's expired
*/
public static boolean checkIsVoipCertificate(X509Certificate certificate)
throws CertificateException {
if (certificate == null) throw new CertificateException("Null certificate");

// Ensure that it's a push certificate
final Map<String, String> stringStringMap = CertificateUtils.splitCertificateSubject(certificate.getSubjectDN().getName());
final String cn = stringStringMap.get("CN");

return cn.toLowerCase().contains("voip");
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/clevertap/apns/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ public int size() {
*/
public Notification build() {
root.put("aps", aps);
aps.put("alert", alert);

//No need to add the alert if it's empty.
if (alert.size() > 0) {
aps.put("alert", alert);
}

final String payload;
try {
Expand Down
29 changes: 21 additions & 8 deletions src/main/java/com/clevertap/apns/clients/SyncOkHttpApnsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class SyncOkHttpApnsClient implements ApnsClient {
private long lastJWTTokenTS = 0;
private String cachedJWTToken = null;

private boolean isVoip;

/**
* Creates a new client which uses token authentication API.
*
Expand Down Expand Up @@ -171,6 +173,8 @@ public SyncOkHttpApnsClient(InputStream certificate, String password, boolean pr
final X509Certificate cert = (X509Certificate) ks.getCertificate(ks.aliases().nextElement());
CertificateUtils.validateCertificate(production, cert);

this.isVoip = CertificateUtils.checkIsVoipCertificate(cert);

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, password.toCharArray());
KeyManager[] keyManagers = kmf.getKeyManagers();
Expand Down Expand Up @@ -264,10 +268,6 @@ public void writeTo(BufferedSink sink) throws IOException {
})
.header("content-length", notification.getPayload().getBytes(Constants.UTF_8).length + "");

if (topic != null) {
rb.header("apns-topic", topic);
}

if (collapseId != null) {
rb.header("apns-collapse-id", collapseId);
}
Expand All @@ -280,12 +280,25 @@ public void writeTo(BufferedSink sink) throws IOException {
rb.header("apns-expiration", String.valueOf(expiration));
}

if (priority != null) {
rb.header("apns-priority", String.valueOf(priority.getCode()));
if (this.isVoip) {
rb.header("apns-push-type", "voip");

if (topic != null) {
rb.header("apns-topic", topic + ".voip");
}
}
else {
if (topic != null) {
rb.header("apns-topic", topic);
}

if (pushType != null) {
rb.header("apns-push-type", pushType);
}

if (pushType != null) {
rb.header("apns-push-type", pushType);
if (priority != null) {
rb.header("apns-priority", String.valueOf(priority.getCode()));
}
}

if (keyID != null && teamID != null && apnsAuthKey != null) {
Expand Down