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

Update payload limit to 2KB. #23

Open
wants to merge 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
.classpath
.project
.settings
Expand Down
Binary file not shown.
17 changes: 17 additions & 0 deletions dbay-apns4j.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" />
</component>
</module>
7 changes: 7 additions & 0 deletions src/main/java/com/dbay/apns4j/IApnsCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dbay.apns4j;

import com.dbay.apns4j.model.Payload;

public interface IApnsCallback {
public void success(Payload payload);
}
4 changes: 2 additions & 2 deletions src/main/java/com/dbay/apns4j/IApnsConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public interface IApnsConnection extends Closeable {

public void sendNotification(String token, Payload payload);
public Boolean sendNotification(String token, Payload payload);

public void sendNotification(PushNotification notification);
public Boolean sendNotification(PushNotification notification);
}
6 changes: 5 additions & 1 deletion src/main/java/com/dbay/apns4j/IApnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ public interface IApnsService {
* @param payload
*/
public void sendNotification(String token, Payload payload);

public void sendNotification(String token, Payload payload, IApnsCallback callback);

/**
* If you want to specify the ID of a notification, use this method
* @param notification
*/
public void sendNotification(PushNotification notification);

public void shutdown();

/**
Expand All @@ -48,4 +51,5 @@ public interface IApnsService {
* @return the device tokens which belong to the app that doesn't exist on the device.
*/
public List<Feedback> getFeedbacks();

}
16 changes: 9 additions & 7 deletions src/main/java/com/dbay/apns4j/impl/ApnsConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,28 @@ public ApnsConnectionImpl(SocketFactory factory, String host, int port, int maxR
}

@Override
public void sendNotification(String token, Payload payload) {
public Boolean sendNotification(String token, Payload payload) {
PushNotification notification = new PushNotification();
notification.setId(IDENTIFIER.incrementAndGet());
notification.setExpire(EXPIRE);
notification.setToken(token);
notification.setPayload(payload);
sendNotification(notification);
return sendNotification(notification);
}

@Override
public void sendNotification(PushNotification notification) {
public Boolean sendNotification(PushNotification notification) {
byte[] plBytes = null;
String payload = notification.getPayload().toString();
try {
plBytes = payload.getBytes(CHARSET_ENCODING);
if (plBytes.length > PAY_LOAD_MAX_LENGTH) {
logger.error("Payload execeed limit, the maximum size allowed is 256 bytes. " + payload);
return;
logger.error("Payload execeed limit, the maximum size allowed is " + PAY_LOAD_MAX_LENGTH + " bytes. " + payload);
return false;
}
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
return;
return false;
}

/**
Expand Down Expand Up @@ -177,7 +177,7 @@ public void sendNotification(PushNotification notification) {
}
if (!isSuccessful) {
logger.error(String.format("%s Notification send failed. %s", connName, notification));
return;
return false;
} else {
logger.info(String.format("%s Send success. count: %s, notificaion: %s", connName,
notificaionSentCount.incrementAndGet(), notification));
Expand Down Expand Up @@ -206,6 +206,8 @@ public void sendNotification(PushNotification notification) {
*/
startErrorWorker();
}

return true;
}
private Socket createNewSocket() throws IOException, UnknownHostException {
if (logger.isDebugEnabled()) {
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/com/dbay/apns4j/impl/ApnsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import javax.net.SocketFactory;

import com.dbay.apns4j.IApnsCallback;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -57,7 +58,13 @@ private ApnsServiceImpl(ApnsConfig config) {
connPool = ApnsConnectionPool.newConnPool(config, factory);
feedbackConn = new ApnsFeedbackConnectionImpl(config, factory);
}


private IApnsCallback callback = null;

public void setCallback(IApnsCallback callback) {
this.callback = callback;
}

@Override
public void sendNotification(final String token, final Payload payload) {
service.execute(new Runnable() {
Expand All @@ -77,6 +84,31 @@ public void run() {
}
});
}

@Override
public void sendNotification(final String token, final Payload payload, final IApnsCallback callback) {
service.execute(new Runnable() {
@Override
public void run() {
IApnsConnection conn = null;
try {
conn = getConnection();
Boolean success = conn.sendNotification(token, payload);
if (success && callback != null) {
callback.success(payload);
}

} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
if (conn != null) {
connPool.returnConn(conn);
}
}
}
});
}

@Override
public void sendNotification(final PushNotification notification) {
service.execute(new Runnable() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/dbay/apns4j/model/ApnsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public class ApnsConstants {

public static final int ERROR_RESPONSE_BYTES_LENGTH = 6;

public static final int PAY_LOAD_MAX_LENGTH = 256;
// Refer: http://stackoverflow.com/a/26994198/419348
// It is 2KB now.
public static final int PAY_LOAD_MAX_LENGTH = 2048;

public static final String CHARSET_ENCODING = "UTF-8";
}
21 changes: 21 additions & 0 deletions src/main/java/com/dbay/apns4j/model/Payload.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public class Payload {
private String sound = "default.caf";
private Integer contentAvailable;

private String alertTitle;
private String alertBody;
private String alertActionLocKey;
private String alertLocKey;
private String[] alertLocArgs;
private String alertLaunchImage;
private Boolean mutableContent = false;

public Map<String, Object> getParams() {
return params;
Expand Down Expand Up @@ -86,6 +88,7 @@ public String toString() {
} else {
if (getAlertBody() != null || getAlertLocKey() != null) {
JSONObject alertObj = new JSONObject();
putIntoJson("title", getAlertTitle(), alertObj);
putIntoJson("body", getAlertBody(), alertObj);
putIntoJson("action-loc-key", getAlertActionLocKey(), alertObj);
putIntoJson("loc-key", getAlertLocKey(), alertObj);
Expand All @@ -109,6 +112,9 @@ public String toString() {
if (getContentAvailable() != null) {
apsObj.put("content-available", getContentAvailable().intValue());
}
if (getMutableContent()) {
apsObj.put("mutable-content", 1);
}

object.put(APS, apsObj);
if (getParams() != null) {
Expand All @@ -133,6 +139,21 @@ public static void main(String[] args) {
payload.addParam("number", 12312312312L);
System.out.println(payload.toString());
}

public Boolean getMutableContent() {
return mutableContent;
}

public void setMutableContent(Boolean mutableContent) {
this.mutableContent = mutableContent;
}

public String getAlertTitle() {
return alertTitle;
}
public void setAlertTitle(String alertTitle) {
this.alertTitle = alertTitle;
}
public String getAlertBody() {
return alertBody;
}
Expand Down