Skip to content

Commit

Permalink
country filter fix
Browse files Browse the repository at this point in the history
some tests
filter chain example
  • Loading branch information
Jens Klingsporn committed Jan 5, 2018
1 parent 5e95278 commit 7f9c308
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 13 deletions.
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.5.0</version>
<version>1.6</version>
</dependency>
```

Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<packaging>jar</packaging>
<groupId>io.github.jklingsporn</groupId>
<artifactId>vertx-push-onesignal</artifactId>
<version>1.5.0</version>
<version>1.6</version>
<name>${project.groupId}:${project.artifactId}</name>
<properties>
<vertx.version>3.5.0</vertx.version>
<junit.version>4.12</junit.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Expand All @@ -37,6 +38,12 @@
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.jklingsporn.vertx.push.PushClient;
import io.github.jklingsporn.vertx.push.Segments;
import io.github.jklingsporn.vertx.push.SendOptions;
import io.github.jklingsporn.vertx.push.filters.Filter;
import io.github.jklingsporn.vertx.push.filters.Filters;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
Expand All @@ -16,6 +17,8 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;

/**
Expand Down Expand Up @@ -51,7 +54,7 @@ public static void exampleTwo(){
//add a heading
withHeadings(new JsonObject().put("en","Awesome App").put("de","Super App")).
//target the audience using filters (e.g. people having used your app only once or haven't logged in since 24 hours)
targetByFilter(Filters.lastSession().greater(24).or(Filters.sessionCount().equal(1))).
targetByFilter(Filters.lastSession().greater(24f).or(Filters.sessionCount().equal(1))).
//but only ios users!
addOptions(new SendOptions().setPlatform(EnumSet.of(SendOptions.Platform.IOS))).
//and wait another minute until they receive it
Expand Down Expand Up @@ -107,6 +110,35 @@ public static void exampleFour(){
});
}

public static void exampleFive(){
//chain tag-filters using noop
Collection<String> tagValues = Arrays.asList("foo","bar","baz");
Filter filter = Filters.noop();
for (String tagValue : tagValues) {
filter = filter.or(Filters.tag("MY_USER_TAG").equal(tagValue));
}
PushClient.create(Vertx.vertx(), "YOUR_APP_ID", "YOUR_API_KEY").
//setup the content of the message on the serverside
withContent(new JsonObject().put("en", "English Content.").put("de", "Deutscher Inhalt.")).
//add a heading
withHeadings(new JsonObject().put("en", "Awesome App").put("de", "Super App")).

targetByFilter(filter).
//but only ios users!
addOptions(new SendOptions().setPlatform(EnumSet.of(SendOptions.Platform.IOS))).
//and wait another minute until they receive it
sendAfter(
ZonedDateTime.ofInstant(Instant.now().plus(1, ChronoUnit.MINUTES), ZoneId.systemDefault()),
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
@@ -1,7 +1,10 @@
package io.github.jklingsporn.vertx.push.filters;

import io.github.jklingsporn.vertx.push.examples.Examples;
import io.github.jklingsporn.vertx.push.filters.relations.EqualRelation;
import io.vertx.core.impl.Arguments;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.Objects;

Expand Down Expand Up @@ -91,21 +94,21 @@ public static TagFilter tag(String key){

/**
* A filter based on a user's location.
* @param radiusInMeters
* @param latitude
* @param longitude
* @param radiusInMeters not null
* @param latitude not null
* @param longitude not null
* @return a Filter
* @see <a href="https://documentation.onesignal.com/reference#section-send-to-users-based-on-filters">Notification filters</a>
*/
public static Filter location(Integer radiusInMeters, String latitude, String longitude){
FilterImpl filter = new FilterImpl("location");
filter.content().put("radius",radiusInMeters).put("lat",latitude).put("long",longitude);
filter.content().put("radius",radiusInMeters.toString()).put("lat",latitude).put("long",longitude);
return filter;
}

/**
* A filter based on an email address.
* @param email
* @param email not null
* @return a Filter
* @see <a href="https://documentation.onesignal.com/reference#section-send-to-users-based-on-filters">Notification filters</a>
*/
Expand All @@ -117,16 +120,53 @@ public static Filter email(String email){

/**
* A filter based on a country.
* @param country
* @param country not null
* @return a Filter
* @see <a href="https://documentation.onesignal.com/reference#section-send-to-users-based-on-filters">Notification filters</a>
*/
public static Filter country(String country){
Objects.requireNonNull(country);
Arguments.require(country.length() != 2, "country must have exactly two digits.");
Arguments.require(country.length() == 2, "country must have exactly two digits.");
return ((EqualRelation<String>) () -> "country").equal(country);
}

/**
* A filter that does nothing. It can be used to build a Filter-chain based on a Collection of tag values.
* @return a Filter
* @see Examples#exampleFive()
*/
public static Filter noop(){
return NoopFilter.getInstance();
}

private static class NoopFilter implements Filter{

private static NoopFilter INSTANCE;
public static NoopFilter getInstance() {
return INSTANCE == null ? INSTANCE = new NoopFilter() : INSTANCE;
}

@Override
public Filter and(Filter other) {
return other;
}

@Override
public Filter or(Filter other) {
return other;
}

@Override
public JsonArray asJsonArray() {
throw new UnsupportedOperationException("noop");
}

@Override
public JsonObject content() {
throw new UnsupportedOperationException("noop");
}
}

private static class TagFilterImpl implements TagFilter{

private final String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
/**
* Created by jensklingsporn on 04.01.17.
*/
public interface LastSessionFilter extends GreaterRelation<Integer>,LessRelation<Integer> {
public interface LastSessionFilter extends GreaterRelation<Float>,LessRelation<Float> {

default String getContext(){
return "last_session";
}

@Override
default Filter greater(Integer value) {
default Filter greater(Float value) {
FilterImpl filter = new FilterImpl(getContext());
filter.content().put("relation", ">").put("hours_ago", value.toString());
return filter;
}

@Override
default Filter less(Integer value) {
default Filter less(Float value) {
FilterImpl filter = new FilterImpl(getContext());
filter.content().put("relation", "<").put("hours_ago", value.toString());
return filter;
Expand Down
Loading

0 comments on commit 7f9c308

Please sign in to comment.