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

Remove redundant code from abstract class. #194

Open
wants to merge 4 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
12 changes: 11 additions & 1 deletion src/main/java/com/notnoop/apns/ApnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public interface ApnsService {

EnhancedApnsNotification push(String deviceToken, String payload, Date expiry) throws NetworkIOException;

EnhancedApnsNotification push(String deviceToken, String payload, int expiry) throws NetworkIOException;

/**
* Sends a push notification with the provided {@code payload} to the
* iPhone of {@code deviceToken}.
Expand All @@ -82,6 +84,8 @@ public interface ApnsService {
*/
ApnsNotification push(byte[] deviceToken, byte[] payload) throws NetworkIOException;

EnhancedApnsNotification push(byte[] deviceToken, byte[] payload, Date expiry) throws NetworkIOException;

EnhancedApnsNotification push(byte[] deviceToken, byte[] payload, int expiry) throws NetworkIOException;

/**
Expand All @@ -98,8 +102,11 @@ public interface ApnsService {
* attempting to send the message
*/
Collection<? extends ApnsNotification> push(Collection<String> deviceTokens, String payload) throws NetworkIOException;

Collection<? extends EnhancedApnsNotification> push(Collection<String> deviceTokens, String payload, Date expiry) throws NetworkIOException;

Collection<? extends EnhancedApnsNotification> push(Collection<String> deviceTokens, String payload, int expiry) throws NetworkIOException;

/**
* Sends a bulk push notification with the provided
* {@code payload} to iPhone of {@code deviceToken}s set.
Expand All @@ -114,6 +121,9 @@ public interface ApnsService {
* attempting to send the message
*/
Collection<? extends ApnsNotification> push(Collection<byte[]> deviceTokens, byte[] payload) throws NetworkIOException;

Collection<? extends EnhancedApnsNotification> push(Collection<byte[]> deviceTokens, byte[] payload, Date expiry) throws NetworkIOException;

Collection<? extends EnhancedApnsNotification> push(Collection<byte[]> deviceTokens, byte[] payload, int expiry) throws NetworkIOException;

/**
Expand Down Expand Up @@ -168,5 +178,5 @@ public interface ApnsService {
* or the service cannot send notifications for now
*/
void testConnection() throws NetworkIOException;

}
117 changes: 61 additions & 56 deletions src/main/java/com/notnoop/apns/internal/AbstractApnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,92 +43,97 @@
import com.notnoop.exceptions.NetworkIOException;

abstract class AbstractApnsService implements ApnsService {
private ApnsFeedbackConnection feedback;
private AtomicInteger c = new AtomicInteger();
private final ApnsFeedbackConnection feedback;
private final AtomicInteger c = new AtomicInteger();

public AbstractApnsService(ApnsFeedbackConnection feedback) {
public AbstractApnsService(final ApnsFeedbackConnection feedback) {
this.feedback = feedback;
}

public EnhancedApnsNotification push(String deviceToken, String payload) throws NetworkIOException {
EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), EnhancedApnsNotification.MAXIMUM_EXPIRY, deviceToken, payload);
push(notification);
return notification;
@Override
public EnhancedApnsNotification push(final String deviceToken, final String payload) throws NetworkIOException {
return push(deviceToken, payload, EnhancedApnsNotification.MAXIMUM_EXPIRY);
}

public EnhancedApnsNotification push(String deviceToken, String payload, Date expiry) throws NetworkIOException {
EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), (int)(expiry.getTime() / 1000), deviceToken, payload);
push(notification);
return notification;
@Override
public EnhancedApnsNotification push(final String deviceToken, final String payload, final Date expiry) throws NetworkIOException {
return push(deviceToken, payload, (int) (expiry.getTime() / 1000));
}

public EnhancedApnsNotification push(byte[] deviceToken, byte[] payload) throws NetworkIOException {
EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), EnhancedApnsNotification.MAXIMUM_EXPIRY, deviceToken, payload);
@Override
public EnhancedApnsNotification push(final String deviceToken, final String payload, final int expiry) throws NetworkIOException {
final EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), expiry, deviceToken, payload);
push(notification);
return notification;
}

public EnhancedApnsNotification push(byte[] deviceToken, byte[] payload, int expiry) throws NetworkIOException {
EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), expiry, deviceToken, payload);
@Override
public EnhancedApnsNotification push(final byte[] deviceToken, final byte[] payload) throws NetworkIOException {
return push(deviceToken, payload, EnhancedApnsNotification.MAXIMUM_EXPIRY);
}

@Override
public EnhancedApnsNotification push(final byte[] deviceToken, final byte[] payload, final Date expiry) throws NetworkIOException {
return push(deviceToken, payload, (int) (expiry.getTime() / 1000));
}

@Override
public EnhancedApnsNotification push(final byte[] deviceToken, final byte[] payload, final int expiry) throws NetworkIOException {
final EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), expiry, deviceToken, payload);
push(notification);
return notification;
}

public Collection<EnhancedApnsNotification> push(Collection<String> deviceTokens, String payload) throws NetworkIOException {
byte[] messageBytes = Utilities.toUTF8Bytes(payload);
List<EnhancedApnsNotification> notifications = new ArrayList<EnhancedApnsNotification>(deviceTokens.size());
for (String deviceToken : deviceTokens) {
byte[] dtBytes = Utilities.decodeHex(deviceToken);
EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), EnhancedApnsNotification.MAXIMUM_EXPIRY, dtBytes, messageBytes);
notifications.add(notification);
push(notification);
}
return notifications;
@Override
public Collection<EnhancedApnsNotification> push(final Collection<String> deviceTokens, final String payload) throws NetworkIOException {
return push(deviceTokens, payload, EnhancedApnsNotification.MAXIMUM_EXPIRY);
}

public Collection<EnhancedApnsNotification> push(Collection<String> deviceTokens, String payload, Date expiry) throws NetworkIOException {
byte[] messageBytes = Utilities.toUTF8Bytes(payload);
List<EnhancedApnsNotification> notifications = new ArrayList<EnhancedApnsNotification>(deviceTokens.size());
for (String deviceToken : deviceTokens) {
byte[] dtBytes = Utilities.decodeHex(deviceToken);
EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), (int)(expiry.getTime() / 1000), dtBytes, messageBytes);
notifications.add(notification);
push(notification);
}
return notifications;
@Override
public Collection<EnhancedApnsNotification> push(final Collection<String> deviceTokens, final String payload, final Date expiry) throws NetworkIOException {
return push(deviceTokens, payload, (int) (expiry.getTime() / 1000));
}

public Collection<EnhancedApnsNotification> push(Collection<byte[]> deviceTokens, byte[] payload) throws NetworkIOException {
List<EnhancedApnsNotification> notifications = new ArrayList<EnhancedApnsNotification>(deviceTokens.size());
for (byte[] deviceToken : deviceTokens) {
EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), EnhancedApnsNotification.MAXIMUM_EXPIRY, deviceToken, payload);
notifications.add(notification);
push(notification);
@Override
public Collection<EnhancedApnsNotification> push(final Collection<String> deviceTokens, final String payload, final int expiry) throws NetworkIOException {
final byte[] messageBytes = Utilities.toUTF8Bytes(payload);
final List<EnhancedApnsNotification> notifications =
new ArrayList<EnhancedApnsNotification>(deviceTokens.size());
for (final String deviceToken : deviceTokens) {
final byte[] dtBytes = Utilities.decodeHex(deviceToken);
notifications.add(push(dtBytes, messageBytes, expiry));
}
return notifications;
}

public Collection<EnhancedApnsNotification> push(Collection<byte[]> deviceTokens, byte[] payload, int expiry) throws NetworkIOException {
List<EnhancedApnsNotification> notifications = new ArrayList<EnhancedApnsNotification>(deviceTokens.size());
for (byte[] deviceToken : deviceTokens) {
EnhancedApnsNotification notification =
new EnhancedApnsNotification(c.incrementAndGet(), expiry, deviceToken, payload);
notifications.add(notification);
push(notification);
@Override
public Collection<EnhancedApnsNotification> push(final Collection<byte[]> deviceTokens, final byte[] payload) throws NetworkIOException {
return push(deviceTokens, payload, EnhancedApnsNotification.MAXIMUM_EXPIRY);
}

@Override
public Collection<EnhancedApnsNotification> push(final Collection<byte[]> deviceTokens, final byte[] payload, final Date expiry) throws NetworkIOException {
return push(deviceTokens, payload, (int) (expiry.getTime() / 1000));
}

@Override
public Collection<EnhancedApnsNotification> push(final Collection<byte[]> deviceTokens, final byte[] payload, final int expiry) throws NetworkIOException {
final List<EnhancedApnsNotification> notifications =
new ArrayList<EnhancedApnsNotification>(deviceTokens.size());
for (final byte[] deviceToken : deviceTokens) {
notifications.add(push(deviceToken, payload, expiry));
}
return notifications;
}

@Override
public abstract void push(ApnsNotification message) throws NetworkIOException;

public Map<String, Date> getInactiveDevices() throws NetworkIOException {
@Override
public Map<String, Date> getInactiveDevices() throws NetworkIOException
{
return feedback.getInactiveDevices();
}
}