Skip to content
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
24 changes: 15 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@
<url>https://github.com/CleverTap/apns-http2</url>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.0</version>
</dependency>
<!--<dependency>-->
<!--<groupId>com.fasterxml.jackson.core</groupId>-->
<!--<artifactId>jackson-core</artifactId>-->
<!--<version>2.7.0</version>-->
<!--</dependency>-->

<!--<dependency>-->
<!--<groupId>com.fasterxml.jackson.core</groupId>-->
<!--<artifactId>jackson-databind</artifactId>-->
<!--<version>2.1.4</version>-->
<!--</dependency>-->

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.4</version>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.50</version>
</dependency>

<dependency>
Expand All @@ -34,7 +40,7 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.2.0</version>
<version>3.11.0</version>
</dependency>

<dependency>
Expand Down
51 changes: 37 additions & 14 deletions src/main/java/com/clevertap/apns/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@

package com.clevertap.apns;

import com.alibaba.fastjson.JSON;
import com.clevertap.apns.clients.AsyncOkHttpApnsClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
Expand Down Expand Up @@ -65,7 +64,6 @@ public int getCode() {
}
}


/**
* Constructs a new Notification with a payload and token.
*
Expand All @@ -77,7 +75,8 @@ public int getCode() {
* @param priority The priority of the notification (10 or 5)
* @param uuid A canonical UUID that identifies the notification
*/
protected Notification(String payload, String token, String topic, String collapseId, long expiration, Priority priority, UUID uuid) {
protected Notification(String payload, String token, String topic, String collapseId, long expiration,
Priority priority, UUID uuid) {
this.payload = payload;
this.token = token;
this.topic = topic;
Expand Down Expand Up @@ -139,7 +138,6 @@ public UUID getUuid() {
* Builds a notification to be sent to APNS.
*/
public static class Builder {
private final ObjectMapper mapper = new ObjectMapper();

private final HashMap<String, Object> root, aps, alert;
private final String token;
Expand All @@ -148,6 +146,7 @@ public static class Builder {
private long expiration = -1; // defaults to -1, as 0 is a valid value (included only if greater than -1)
private Priority priority;
private UUID uuid;
private String threadId;

/**
* Creates a new notification builder.
Expand Down Expand Up @@ -253,6 +252,36 @@ public Builder priority(Priority priority) {
return this;
}

/**
* according to: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual
* /RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1
*
* @param threadId
* @return
*/
public Builder threadId(String threadId) {
aps.put("thread-id", threadId);
return this;
}

public Builder launchImage(String value) {
alert.put("launch-image", value);
return this;
}

/**
*
* Add some custome Object to root, for some costomer situation
*
* @param key
* @param obj
* @return
*/
public Builder addRoot(String key, Object obj) {
this.root.put(key, obj);
return this;
}

public int size() {
try {
return build().getPayload().getBytes("UTF-8").length;
Expand All @@ -262,22 +291,16 @@ public int size() {
}

/**
* Builds the notification.
* Also see {@link AsyncOkHttpApnsClient#push(Notification, NotificationResponseListener)}
* Builds the notification. Also see {@link AsyncOkHttpApnsClient#push(Notification,
* NotificationResponseListener)}
*
* @return The notification
*/
public Notification build() {
root.put("aps", aps);
aps.put("alert", alert);

final String payload;
try {
payload = mapper.writeValueAsString(root);
} catch (JsonProcessingException e) {
// Should not happen
throw new RuntimeException(e);
}
payload = JSON.toJSONString(root);
return new Notification(payload, token, topic, collapseId, expiration, priority, uuid);
}
}
Expand Down