Skip to content

Commit ae8d039

Browse files
committed
Make ApnsPayloadBuilder a concrete class
1 parent 5ad8193 commit ae8d039

9 files changed

Lines changed: 63 additions & 102 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Pushy's APNs clients maintain an internal pool of connections to the APNs server
8787
final SimpleApnsPushNotification pushNotification;
8888

8989
{
90-
final ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
90+
final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();
9191
payloadBuilder.setAlertBody("Example!");
9292

9393
final String payload = payloadBuilder.build();
@@ -97,7 +97,7 @@ final SimpleApnsPushNotification pushNotification;
9797
}
9898
```
9999

100-
Pushy includes a [`SimpleApnsPayloadBuilder`](https://pushy-apns.org/apidocs/0.15/com/eatthepath/pushy/apns/util/SimpleApnsPayloadBuilder.html). [APNs payloads are just JSON strings](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification), and callers may produce payloads by the method of their choice; while Pushy's payload builders may be convenient, callers are _not_ obligated to use them.
100+
Pushy includes an [`ApnsPayloadBuilder`](https://pushy-apns.org/apidocs/0.15/com/eatthepath/pushy/apns/util/ApnsPayloadBuilder.html). [APNs payloads are just JSON strings](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification), and callers may produce payloads by the method of their choice; while Pushy's payload builders may be convenient, callers are _not_ obligated to use them.
101101

102102
The process of sending a push notification is asynchronous; although the process of sending a notification and getting a reply from the server may take some time, the client will return a [`CompletableFuture`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html) right away. You can use that `CompletableFuture` to track the progress and eventual outcome of the sending operation. Note that sending a notification returns a [`PushNotificationFuture`](https://pushy-apns.org/apidocs/0.15/com/eatthepath/pushy/apns/util/concurrent/PushNotificationFuture.html), which is a subclass of `CompletableFuture` that always holds a reference to the notification that was sent.
103103

benchmark/src/main/java/com/eatthepath/pushy/apns/ApnsClientBenchmark.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.eatthepath.pushy.apns.server.BenchmarkApnsServer;
2727
import com.eatthepath.pushy.apns.server.BenchmarkApnsServerBuilder;
2828
import com.eatthepath.pushy.apns.util.ApnsPayloadBuilder;
29-
import com.eatthepath.pushy.apns.util.SimpleApnsPayloadBuilder;
3029
import com.eatthepath.pushy.apns.util.SimpleApnsPushNotification;
3130
import io.netty.channel.nio.NioEventLoopGroup;
3231
import io.netty.util.concurrent.Future;
@@ -102,7 +101,7 @@ public void setUp() throws Exception {
102101

103102
this.pushNotifications = new ArrayList<>(this.notificationCount);
104103
{
105-
final ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
104+
final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();
106105

107106
for (int i = 0; i < this.notificationCount; i++) {
108107
final String payload =

benchmark/src/main/java/com/eatthepath/pushy/apns/util/SimpleApnsPayloadBuilderBenchmark.java renamed to benchmark/src/main/java/com/eatthepath/pushy/apns/util/ApnsPayloadBuilderBenchmark.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import java.util.List;
3030

3131
@State(Scope.Thread)
32-
public class SimpleApnsPayloadBuilderBenchmark {
32+
public class ApnsPayloadBuilderBenchmark {
3333

34-
private SimpleApnsPayloadBuilder payloadBuilder;
34+
private ApnsPayloadBuilder payloadBuilder;
3535

3636
@Param({"512", "4096"})
3737
public int messageBodyLength;
@@ -43,7 +43,7 @@ public class SimpleApnsPayloadBuilderBenchmark {
4343

4444
@Setup
4545
public void setUp() {
46-
this.payloadBuilder = new SimpleApnsPayloadBuilder();
46+
this.payloadBuilder = new ApnsPayloadBuilder();
4747

4848
final char[] messageBodyCharacters;
4949
{

pushy/src/main/java/com/eatthepath/pushy/apns/util/ApnsPayloadBuilder.java

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,28 @@
2222

2323
package com.eatthepath.pushy.apns.util;
2424

25+
import com.eatthepath.json.JsonSerializer;
26+
2527
import java.time.Instant;
2628
import java.util.*;
29+
import java.util.function.Function;
2730

2831
/**
2932
* <p>A base utility class for constructing JSON payloads suitable for inclusion in APNs push notifications. Payload
3033
* builders are reusable, but are <em>not</em> thread-safe.</p>
3134
*
35+
* <p>{@code ApnsPayloadBuilder} will, by default, use a {@link JsonSerializer} to serialize payloads. Callers may
36+
* provide their own serialization functions (see {@link #build(Function)}) in order to serialize payloads with
37+
* third-party JSON libraries.</p>
38+
*
3239
* @author <a href="https://github.com/jchambers">Jon Chambers</a>
3340
*
3441
* @see <a href="https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification">Generating a Remote Notification</a>
3542
*
3643
* @since 0.14.0
3744
*/
3845
@SuppressWarnings({"UnusedReturnValue", "unused"})
39-
public abstract class ApnsPayloadBuilder {
46+
public class ApnsPayloadBuilder {
4047

4148
private String alertBody = null;
4249

@@ -886,7 +893,7 @@ public ApnsPayloadBuilder setDismissalDate(final Instant dismissalDate) {
886893
*
887894
* @since 0.14.0
888895
*/
889-
protected Map<String, Object> buildPayloadMap() {
896+
private Map<String, Object> buildPayloadMap() {
890897
final boolean isLiveActivityPayload = event != null;
891898

892899
final Map<String, Object> payload = new HashMap<>();
@@ -1049,15 +1056,29 @@ protected Map<String, Object> buildPayloadMap() {
10491056
}
10501057

10511058
/**
1052-
* Returns a JSON representation of the push notification payload under construction.
1059+
* Returns a JSON representation of the push notification payload under construction. This method serializes
1060+
* payloads using a {@link JsonSerializer}. Please see the documentation for {@link JsonSerializer} for details
1061+
* about how this payload builder serializes custom properties.
10531062
*
10541063
* @return a JSON representation of the payload under construction
10551064
*
1056-
* @see #buildPayloadMap()
1057-
*
10581065
* @since 0.14.0
10591066
*/
1060-
public abstract String build();
1067+
public String build() {
1068+
return build(JsonSerializer::writeJsonTextAsString);
1069+
}
1070+
1071+
/**
1072+
* Returns a JSON representation of the push notification payload under construction using the serialization method
1073+
* of the caller's choice. Callers may use this method to serialize payloads with third-party JSON libraries.
1074+
*
1075+
* @return a JSON representation of the payload under construction
1076+
*
1077+
* @since 0.16.0
1078+
*/
1079+
public String build(final Function<Map<String, Object>, String> buildPayloadFunction) {
1080+
return buildPayloadFunction.apply(this.buildPayloadMap());
1081+
}
10611082

10621083
/**
10631084
* Returns a map representing a
@@ -1072,14 +1093,16 @@ protected Map<String, Object> buildPayloadMap() {
10721093
*
10731094
* @since 0.14.0
10741095
*/
1075-
protected Map<String, String> buildMdmPayloadMap(final String pushMagicValue) {
1096+
private Map<String, String> buildMdmPayloadMap(final String pushMagicValue) {
10761097
return Collections.singletonMap(MDM_KEY, pushMagicValue);
10771098
}
10781099

10791100
/**
10801101
* Returns a JSON representation of a
10811102
* <a href="https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/MobileDeviceManagementProtocolRef/1-Introduction/Introduction.html#//apple_ref/doc/uid/TP40017387-CH1-SW1">Mobile
1082-
* Device Management</a> "wake up" payload.
1103+
* Device Management</a> "wake up" payload. This method serializes payloads using a {@link JsonSerializer}. Please
1104+
* see the documentation for {@link JsonSerializer} for details about how this payload builder serializes custom
1105+
* properties.
10831106
*
10841107
* @param pushMagicValue the "push magic" string that the device sends to the MDM server in a {@code TokenUpdate}
10851108
* message
@@ -1090,8 +1113,28 @@ protected Map<String, String> buildMdmPayloadMap(final String pushMagicValue) {
10901113
* Device Management (MDM) Protocol</a>
10911114
*
10921115
* @since 0.14.0
1116+
*/
1117+
public String buildMdmPayload(final String pushMagicValue) {
1118+
return buildMdmPayload(pushMagicValue, JsonSerializer::writeJsonTextAsString);
1119+
}
1120+
1121+
/**
1122+
* Returns a JSON representation of a
1123+
* <a href="https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/MobileDeviceManagementProtocolRef/1-Introduction/Introduction.html#//apple_ref/doc/uid/TP40017387-CH1-SW1">Mobile
1124+
* Device Management</a> "wake up" payload. This method uses the serialization method of the caller's choice.
1125+
* Callers may use this method to serialize payloads with third-party JSON libraries.
1126+
*
1127+
* @param pushMagicValue the "push magic" string that the device sends to the MDM server in a {@code TokenUpdate}
1128+
* message
10931129
*
1094-
* @see #buildMdmPayloadMap(String)
1130+
* @return a JSON representation of an MDM "wake up" notification payload
1131+
*
1132+
* @see <a href="https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/MobileDeviceManagementProtocolRef/3-MDM_Protocol/MDM_Protocol.html#//apple_ref/doc/uid/TP40017387-CH3-SW2">Mobile
1133+
* Device Management (MDM) Protocol</a>
1134+
*
1135+
* @since 0.16.0
10951136
*/
1096-
public abstract String buildMdmPayload(final String pushMagicValue);
1137+
public String buildMdmPayload(final String pushMagicValue, final Function<Map<String, String>, String> buildPayloadFunction) {
1138+
return buildPayloadFunction.apply(buildMdmPayloadMap(pushMagicValue));
1139+
}
10971140
}

pushy/src/main/java/com/eatthepath/pushy/apns/util/SimpleApnsPayloadBuilder.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

pushy/src/test/java/com/eatthepath/pushy/apns/AbstractClientServerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.eatthepath.pushy.apns.auth.KeyPairUtil;
2828
import com.eatthepath.pushy.apns.server.*;
2929
import com.eatthepath.pushy.apns.util.ApnsPayloadBuilder;
30-
import com.eatthepath.pushy.apns.util.SimpleApnsPayloadBuilder;
3130
import io.netty.channel.nio.NioEventLoopGroup;
3231
import io.netty.util.concurrent.*;
3332
import org.junit.jupiter.api.AfterAll;
@@ -174,7 +173,7 @@ protected static String generateRandomDeviceToken() {
174173
}
175174

176175
protected static String generateRandomPayload() {
177-
final ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
176+
final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();
178177
payloadBuilder.setAlertBody(UUID.randomUUID().toString());
179178

180179
return payloadBuilder.build();

pushy/src/test/java/com/eatthepath/pushy/apns/ExampleApp.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.eatthepath.pushy.apns.auth.ApnsSigningKey;
2626
import com.eatthepath.pushy.apns.proxy.Socks5ProxyHandlerFactory;
2727
import com.eatthepath.pushy.apns.util.ApnsPayloadBuilder;
28-
import com.eatthepath.pushy.apns.util.SimpleApnsPayloadBuilder;
2928
import com.eatthepath.pushy.apns.util.SimpleApnsPushNotification;
3029
import com.eatthepath.pushy.apns.util.TokenUtil;
3130
import com.eatthepath.pushy.apns.util.concurrent.PushNotificationFuture;
@@ -77,7 +76,7 @@ public static void main(final String[] args) throws Exception {
7776
final SimpleApnsPushNotification pushNotification;
7877

7978
{
80-
final ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
79+
final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();
8180
payloadBuilder.setAlertBody("Example!");
8281

8382
final String payload = payloadBuilder.build();

pushy/src/test/java/com/eatthepath/pushy/apns/util/ApnsPayloadBuilderTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,13 @@
4141
import static org.junit.jupiter.api.Assertions.*;
4242
import static org.junit.jupiter.params.provider.Arguments.arguments;
4343

44-
public abstract class ApnsPayloadBuilderTest {
44+
class ApnsPayloadBuilderTest {
4545

4646
private ApnsPayloadBuilder builder;
4747

48-
protected abstract ApnsPayloadBuilder getBuilder();
49-
5048
@BeforeEach
5149
public void setUp() {
52-
this.builder = getBuilder();
50+
this.builder = new ApnsPayloadBuilder();
5351
}
5452

5553
@Test

pushy/src/test/java/com/eatthepath/pushy/apns/util/SimpleApnsPayloadBuilderTest.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)