Skip to content

Commit

Permalink
1.7 add option to ignore "All players are not subscribed"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Klingsporn committed Jan 15, 2018
1 parent 7f9c308 commit 6bacd1d
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 jklingsporn
Copyright (c) 2018 jklingsporn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ To use this library you need to create a [OneSignal](https://onesignal.com/)-acc
<dependency>
<groupId>io.github.jklingsporn</groupId>
<artifactId>vertx-push-onesignal</artifactId>
<version>1.6</version>
<version>1.7</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<packaging>jar</packaging>
<groupId>io.github.jklingsporn</groupId>
<artifactId>vertx-push-onesignal</artifactId>
<version>1.6</version>
<version>1.7</version>
<name>${project.groupId}:${project.artifactId}</name>
<properties>
<vertx.version>3.5.0</vertx.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.Objects;

/**
* Created by jensklingsporn on 02.01.17.
*/
Expand All @@ -21,20 +24,20 @@ class OneSignalPushClient implements PushClient{
private static final String DEFAULT_HOST = "onesignal.com";

private final HttpClient httpClient;
private final String appId;
private final String restApiKey;
private final PushClientOptions pushClientOptions;
private final String host = DEFAULT_HOST;
private final RequestOptions requestOptions;


OneSignalPushClient(Vertx vertx, String appId, String restApiKey) {
this(vertx.createHttpClient(new HttpClientOptions().setSsl(true).setVerifyHost(false)), appId, restApiKey);
OneSignalPushClient(Vertx vertx, PushClientOptions pushClientOptions) {
this(vertx.createHttpClient(new HttpClientOptions().setSsl(true).setVerifyHost(false)), pushClientOptions);
}

OneSignalPushClient(HttpClient httpClient, String appId, String restApiKey) {
this.appId = appId;
this.restApiKey = restApiKey;
OneSignalPushClient(HttpClient httpClient, PushClientOptions pushClientOptions) {
Objects.requireNonNull(pushClientOptions.getAppId());
Objects.requireNonNull(pushClientOptions.getRestApiKey());
this.httpClient = httpClient;
this.pushClientOptions = pushClientOptions;
this.requestOptions = new RequestOptions().setHost(host).setPort(PORT).setSsl(true).setURI(Endpoints.PUSH);
}

Expand All @@ -43,8 +46,9 @@ void sendRequest(JsonObject content,Handler<AsyncResult<JsonObject>> resultHandl
response.bodyHandler(body -> {
try {
JsonObject responseBody = body.toJsonObject();
if (responseBody.getJsonArray("errors")!=null) {
resultHandler.handle(Future.failedFuture(new OneSignalResponseException(responseBody.getJsonArray("errors").encodePrettily())));
JsonArray errors = responseBody.getJsonArray("errors");
if (errors !=null && !isIgnoreAllPlayersAreNotSubscribed(responseBody)) {
resultHandler.handle(Future.failedFuture(new OneSignalResponseException(errors.encodePrettily())));
}else if (response.statusCode() == HttpResponseStatus.OK.code()) {
resultHandler.handle(Future.succeededFuture(responseBody));
} else {
Expand All @@ -54,17 +58,21 @@ void sendRequest(JsonObject content,Handler<AsyncResult<JsonObject>> resultHandl
resultHandler.handle(Future.failedFuture(e));
}
});
}).putHeader(HttpHeaders.CONTENT_TYPE, "application/json").putHeader(HttpHeaders.AUTHORIZATION, "Basic " + restApiKey).end(content.encode());
}).putHeader(HttpHeaders.CONTENT_TYPE, "application/json").putHeader(HttpHeaders.AUTHORIZATION, "Basic " + pushClientOptions.getRestApiKey()).end(content.encode());
}

private boolean isIgnoreAllPlayersAreNotSubscribed(JsonObject responseBody){
return pushClientOptions.isIgnoreAllPlayersAreNotSubscribed() && responseBody.getInteger("recipients")==0;
}

@Override
public AddHeadersStep withTemplate(String templateId) {
return new AddHeadersStepImpl(new PushTemplateStepImpl(new JsonObject().put("app_id", appId),this, templateId));
return new AddHeadersStepImpl(new PushTemplateStepImpl(new JsonObject().put("app_id", pushClientOptions.getAppId()),this, templateId));
}

@Override
public AddHeadersStep withContent(JsonObject contents) {
return new AddHeadersStepImpl(new PushContentsStepImpl(new JsonObject().put("app_id", appId),this,contents));
return new AddHeadersStepImpl(new PushContentsStepImpl(new JsonObject().put("app_id", pushClientOptions.getAppId()),this,contents));
}

@Override
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/io/github/jklingsporn/vertx/push/PushClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,41 @@ public interface PushClient {
* @param appId your OneSignal AppId
* @param restApiKey your OneSignal API-Key
* @return a PushClient
* @deprecated Consider using PushClient#create(Vertx,PushClientOptions) instead
*/
static PushClient create(Vertx vertx, String appId, String restApiKey){
return new OneSignalPushClient(vertx,appId,restApiKey);
return new OneSignalPushClient(vertx, new PushClientOptions().setAppId(appId).setRestApiKey(restApiKey));
}

/**
* @param httpClient the HttpClient to use for calling the OneSignal-API.
* @param appId your OneSignal AppId
* @param restApiKey your OneSignal API-Key
* @return a PushClient
* @deprecated Consider using PushClient#create(HttpClient,PushClientOptions) instead
*/
static PushClient create(HttpClient httpClient, String appId, String restApiKey){
return new OneSignalPushClient(httpClient,appId,restApiKey);
return new OneSignalPushClient(httpClient, new PushClientOptions().setAppId(appId).setRestApiKey(restApiKey));
}

/**
* @param httpClient the HttpClient to use for calling the OneSignal-API.
* @param pushClientOptions the options
* @return a PushClient
* @since 1.7
*/
static PushClient create(HttpClient httpClient, PushClientOptions pushClientOptions){
return new OneSignalPushClient(httpClient, pushClientOptions);
}

/**
* @param vertx your Vertx instance
* @param pushClientOptions the options
* @return a PushClient
* @since 1.7
*/
static PushClient create(Vertx vertx, PushClientOptions pushClientOptions){
return new OneSignalPushClient(vertx, pushClientOptions);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.jklingsporn.vertx.push;

/**
* Created by jensklingsporn on 10.01.18.
* @since 1.7
*/
public class PushClientOptions {

private String appId;
private String restApiKey;
/**
* When set to <code>true</code> it will silently ignore the "All included players are not subscribed"-error by OneSignal
* instead of creating an expensive OneSignalException.
*/
private boolean ignoreAllPlayersAreNotSubscribed=false;

public String getAppId() {
return appId;
}

public PushClientOptions setAppId(String appId) {
this.appId = appId;
return this;
}

public String getRestApiKey() {
return restApiKey;
}

public PushClientOptions setRestApiKey(String restApiKey) {
this.restApiKey = restApiKey;
return this;
}

public boolean isIgnoreAllPlayersAreNotSubscribed() {
return ignoreAllPlayersAreNotSubscribed;
}

public PushClientOptions setIgnoreAllPlayersAreNotSubscribed(boolean ignoreAllPlayersAreNotSubscribed) {
this.ignoreAllPlayersAreNotSubscribed = ignoreAllPlayersAreNotSubscribed;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.jklingsporn.vertx.push.examples;

import io.github.jklingsporn.vertx.push.PushClient;
import io.github.jklingsporn.vertx.push.PushClientOptions;
import io.github.jklingsporn.vertx.push.Segments;
import io.github.jklingsporn.vertx.push.SendOptions;
import io.github.jklingsporn.vertx.push.filters.Filter;
Expand Down Expand Up @@ -139,6 +140,34 @@ public static void exampleFive(){
});
}

/**
* Demonstrates usage of new PushClientOptions and the <code>ignoreAllPlayersAreNotSubscribed</code> property. When
* set to <code>true</code> it will silently ignore the "All included players are not subscribed"-error by OneSignal
* instead of creating an expensive OneSignalException.
* @since 1.7
* @see <a href="https://documentation.onesignal.com/reference#section-results-create-notification">Notification results</a>
*/
public static void exampleSix(){
PushClient.create(Vertx.vertx(), new PushClientOptions()
.setAppId("YOUR_APP_ID")
.setRestApiKey("YOUR_API_KEY")
.setIgnoreAllPlayersAreNotSubscribed(true) //
).
//setup a template on the OneSignal-Dashboard and use it here
withTemplate("someTemplateId").
//all users should receive this
targetBySegments(Segments.ALL).
sendNow(
h -> {
if (h.succeeded()) {
System.err.println(h.result().encodePrettily());
} else {
h.cause().printStackTrace();
}
System.exit(0);
});
}

public static void exampleHttpClientProxy(){
String proxyHost = "yourProxyHost";
int proxyPort = 123;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.jklingsporn.vertx.push;

import io.vertx.core.Vertx;
import org.junit.Assert;
import org.junit.Test;

/**
* Created by jensklingsporn on 15.01.18.
*/
public class PushClientCreateTest {

@Test(expected = NullPointerException.class)
public void emptyPushClientOptionsShouldFail(){
PushClient.create(Vertx.vertx(), new PushClientOptions());
}

@Test(expected = NullPointerException.class)
public void noRestAPIKeyInPushClientOptionsShouldFail(){
PushClient.create(Vertx.vertx(), new PushClientOptions().setAppId("foo"));
}

@Test(expected = NullPointerException.class)
public void noAppIdInPushClientOptionsShouldFail(){
PushClient.create(Vertx.vertx(), new PushClientOptions().setRestApiKey("bar"));
}

@Test
public void createPushClientWithRegularOptionsShouldSucceed(){
PushClient pushClient = PushClient.create(Vertx.vertx(), new PushClientOptions().setAppId("foo").setRestApiKey("bar"));
Assert.assertNotNull(pushClient);
}

}

0 comments on commit 6bacd1d

Please sign in to comment.