Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit dea609c

Browse files
committed
Added the capability to build the payload externally: Message.Builder for GCM and PayloadBuilder for APNS.
1 parent 43d8042 commit dea609c

File tree

10 files changed

+255
-30
lines changed

10 files changed

+255
-30
lines changed

CHANGELOG.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ First release of the GCM Push Client.
1717

1818
+ Fixed Java8 required binary.
1919

20+
## [1.0.3](https://github.com/devsu/push-sender/releases/tag/1.0.3) (2016-08-24)
21+
22+
+ PayloadBuilder can now be built externally.
23+
+ Added validity checks where they were missing.
24+

README.MD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Push Sender #
22

3-
Push Sender is a Java library that allows sending push messages easily, based on the awesome libraries [gcm-server](https://github.com/theganyo/gcm-server) by [theganyo](https://github.com/theganyo) and [java-apns](https://github.com/notnoop/java-apns) by [notnoop](https://github.com/notnoop). Send push messages with one line of code, forget about the rest, and focus on making your application's business logic!
3+
Push Sender is a Java library that allows sending push messages easily, based on the awesome libraries [gcm-server](https://github.com/theganyo/gcm-server) by [theganyo](https://github.com/theganyo) and [java-apns](https://github.com/notnoop/java-apns) by [notnoop](https://github.com/notnoop). Be sure to check them out! Send push messages with one line of code, forget about the rest, and focus on making your application's business logic!
44

55
## Overview ###
66

@@ -10,7 +10,7 @@ Using Push Sender is very simple. You can import Push Sender on your POM file us
1010
<dependency>
1111
<groupId>com.devsu</groupId>
1212
<artifactId>push-sender</artifactId>
13-
<version>1.0.2</version>
13+
<version>1.0.3</version>
1414
</dependency>
1515
```
1616

open_source_licenses.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## theganyo/gcm-server
1+
## notnoop/java-apns
22

33
Copyright 2009, Mahmood Ali.
44
All rights reserved.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.devsu</groupId>
77
<artifactId>push-sender</artifactId>
8-
<version>1.0.2</version>
8+
<version>1.0.3</version>
99
<packaging>jar</packaging>
1010
<name>Push Sender</name>
1111
<description>Push Sender is a library that allows you to quickly send push messages via APNS or GCM, on your Java powered servers.</description>

src/main/java/com/devsu/push/sender/service/async/AsyncAndroidPushService.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package com.devsu.push.sender.service.async;
22

3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
36
import com.devsu.push.sender.callback.PushCallback;
47
import com.devsu.push.sender.service.sync.SyncAndroidPushService;
8+
import com.google.android.gcm.server.Message;
59

610
/**
711
* The async push service for Android (GCM).
812
*/
913
public class AsyncAndroidPushService extends AsyncPushServiceBase {
1014

15+
private static final String BUILDER_OBJECT = Message.Builder.class.getSimpleName();
16+
1117
/**
1218
* @see com.devsu.push.sender.service.sync.SyncAndroidPushService#AndroidPushService(String)
1319
*/
@@ -24,6 +30,62 @@ public AsyncAndroidPushService(String gcmApiKey, PushCallback pushCallback) {
2430
super(new SyncAndroidPushService(gcmApiKey), pushCallback);
2531
}
2632

33+
/**
34+
* Sends a single push message.
35+
* @param msgBuilder The Message.Builder object.
36+
* @param token The push token.
37+
* @return <i>true</i> if the push message request was sent.
38+
* @throws Exception
39+
*/
40+
public void sendPush(final Message.Builder msgBuilder, final String token) {
41+
ExecutorService executorService = Executors.newSingleThreadExecutor();
42+
executorService.execute(new Runnable() {
43+
@Override
44+
public void run() {
45+
try {
46+
SyncAndroidPushService service = (SyncAndroidPushService) pushService;
47+
boolean result = service.sendPush(msgBuilder, token);
48+
if (pushCallback != null) {
49+
pushCallback.onSingleSuccess(result, BUILDER_OBJECT, msgBuilder.build().toString(), null, token);
50+
}
51+
} catch (Exception e) {
52+
if (pushCallback != null) {
53+
pushCallback.onError(e);
54+
}
55+
}
56+
}
57+
});
58+
executorService.shutdown();
59+
}
60+
61+
/**
62+
* Sends a bulk push message.
63+
* @param msgBuilder The Message.Builder object.
64+
* @param token The push token.
65+
* @return <i>true</i> if the push message request was sent.
66+
* @throws Exception
67+
*/
68+
public void sendPushInBulk(final Message.Builder msgBuilder, final String... tokens) {
69+
ExecutorService executorService = Executors.newSingleThreadExecutor();
70+
executorService.execute(new Runnable() {
71+
@Override
72+
public void run() {
73+
try {
74+
SyncAndroidPushService service = (SyncAndroidPushService) pushService;
75+
boolean result = service.sendPushInBulk(msgBuilder, tokens);
76+
if (pushCallback != null) {
77+
pushCallback.onBulkSuccess(result, BUILDER_OBJECT, msgBuilder.build().toString(), null, tokens);
78+
}
79+
} catch (Exception e) {
80+
if (pushCallback != null) {
81+
pushCallback.onError(e);
82+
}
83+
}
84+
}
85+
});
86+
executorService.shutdown();
87+
}
88+
2789
/**
2890
* @see com.devsu.push.sender.service.sync.SyncAndroidPushService#setMaxRetries(int)
2991
*/

src/main/java/com/devsu/push/sender/service/async/AsyncApplePushService.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.devsu.push.sender.service.async;
22

3+
import java.util.Date;
4+
import java.util.Map;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
38
import com.devsu.push.sender.callback.PushCallback;
49
import com.devsu.push.sender.service.sync.SyncApplePushService;
10+
import com.notnoop.apns.PayloadBuilder;
511
import com.notnoop.exceptions.InvalidSSLConfig;
612
import com.notnoop.exceptions.RuntimeIOException;
713

@@ -10,6 +16,8 @@
1016
*/
1117
public class AsyncApplePushService extends AsyncPushServiceBase {
1218

19+
private static final String BUILDER_OBJECT = PayloadBuilder.class.getSimpleName();
20+
1321
/**
1422
* @see com.devsu.push.sender.service.sync.SyncApplePushService#ApplePushService(String, String, boolean)
1523
*/
@@ -32,6 +40,62 @@ public AsyncApplePushService(String certificatePath, String certificatePassword,
3240
super(new SyncApplePushService(certificatePath, certificatePassword, useProductionServer), pushCallback);
3341
}
3442

43+
/**
44+
* Sends a single push message.
45+
* @param msgBuilder The PayloadBuilder object.
46+
* @param token The push token.
47+
* @return <i>true</i> if the push message request was sent.
48+
* @throws Exception
49+
*/
50+
public void sendPush(final PayloadBuilder msgBuilder, final String token) {
51+
ExecutorService executorService = Executors.newSingleThreadExecutor();
52+
executorService.execute(new Runnable() {
53+
@Override
54+
public void run() {
55+
try {
56+
SyncApplePushService service = (SyncApplePushService) pushService;
57+
boolean result = service.sendPush(msgBuilder, token);
58+
if (pushCallback != null) {
59+
pushCallback.onSingleSuccess(result, BUILDER_OBJECT, msgBuilder.toString(), null, token);
60+
}
61+
} catch (Exception e) {
62+
if (pushCallback != null) {
63+
pushCallback.onError(e);
64+
}
65+
}
66+
}
67+
});
68+
executorService.shutdown();
69+
}
70+
71+
/**
72+
* Sends a bulk push message.
73+
* @param msgBuilder The Message.Builder object.
74+
* @param token The push token.
75+
* @return <i>true</i> if the push message request was sent.
76+
* @throws Exception
77+
*/
78+
public void sendPushInBulk(final PayloadBuilder msgBuilder, final String... tokens) {
79+
ExecutorService executorService = Executors.newSingleThreadExecutor();
80+
executorService.execute(new Runnable() {
81+
@Override
82+
public void run() {
83+
try {
84+
SyncApplePushService service = (SyncApplePushService) pushService;
85+
boolean result = service.sendPushInBulk(msgBuilder, tokens);
86+
if (pushCallback != null) {
87+
pushCallback.onBulkSuccess(result, BUILDER_OBJECT, msgBuilder.build().toString(), null, tokens);
88+
}
89+
} catch (Exception e) {
90+
if (pushCallback != null) {
91+
pushCallback.onError(e);
92+
}
93+
}
94+
}
95+
});
96+
executorService.shutdown();
97+
}
98+
3599
/**
36100
* @see com.devsu.push.sender.service.sync.SyncApplePushService#setupDevelopmentServer(java.lang.String, java.lang.String)
37101
*/
@@ -52,4 +116,8 @@ public void setupProductionServer(String certificatePath, String certificatePass
52116
public void setPushEnabled(boolean pushEnabled) {
53117
((SyncApplePushService)pushService).setPushEnabled(pushEnabled);
54118
}
119+
120+
public Map<String, Date> getInactiveDevices() {
121+
return ((SyncApplePushService)pushService).getInactiveDevices();
122+
}
55123
}

src/main/java/com/devsu/push/sender/service/async/AsyncPushServiceBase.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Abstract base class for every async push service.
1212
*/
13-
public abstract class AsyncPushServiceBase implements AsyncPushService {
13+
public abstract class AsyncPushServiceBase implements AsyncPushService {
1414

1515
/**
1616
* The push service that will be used in this async service.
@@ -33,23 +33,23 @@ protected AsyncPushServiceBase(SyncPushService pushService, PushCallback pushCal
3333
}
3434

3535
/*
36-
* @see com.rion18.pusher.service.async.AsyncPushService#sendPush(java.lang.String, java.lang.String)
36+
* @see com.rion18.push.sender.service.async.AsyncPushService#sendPush(java.lang.String, java.lang.String)
3737
*/
3838
@Override
3939
public void sendPush(String message, String token) {
4040
sendPush(null, message, null, token);
4141
}
4242

4343
/*
44-
* @see com.rion18.pusher.service.async.AsyncPushService#sendPush(java.lang.String, java.lang.String, java.lang.String)
44+
* @see com.rion18.push.sender.service.async.AsyncPushService#sendPush(java.lang.String, java.lang.String, java.lang.String)
4545
*/
4646
@Override
4747
public void sendPush(String title, String message, String token) {
4848
sendPush(title, message, null, token);
4949
}
5050

5151
/*
52-
* @see com.rion18.pusher.service.async.AsyncPushService#sendPush(java.lang.String, java.lang.String, java.util.Map, java.lang.String)
52+
* @see com.rion18.push.sender.service.async.AsyncPushService#sendPush(java.lang.String, java.lang.String, java.util.Map, java.lang.String)
5353
*/
5454
@Override
5555
public void sendPush(final String title, final String message,
@@ -74,23 +74,23 @@ public void run() {
7474
}
7575

7676
/*
77-
* @see com.rion18.pusher.service.async.AsyncPushService#sendPushInBulk(java.lang.String, java.lang.String[])
77+
* @see com.rion18.push.sender.service.async.AsyncPushService#sendPushInBulk(java.lang.String, java.lang.String[])
7878
*/
7979
@Override
8080
public void sendPushInBulk(String message, String... tokens) {
8181
sendPushInBulk(null, message, null, tokens);
8282
}
8383

8484
/*
85-
* @see com.rion18.pusher.service.async.AsyncPushService#sendPushInBulk(java.lang.String, java.lang.String, java.lang.String[])
85+
* @see com.rion18.push.sender.service.async.AsyncPushService#sendPushInBulk(java.lang.String, java.lang.String, java.lang.String[])
8686
*/
8787
@Override
8888
public void sendPushInBulk(String title, String message, String... tokens) {
8989
sendPushInBulk(title, message, null, tokens);
9090
}
9191

9292
/*
93-
* @see com.rion18.pusher.service.async.AsyncPushService#sendPushInBulk(java.lang.String, java.lang.String, java.util.Map, java.lang.String[])
93+
* @see com.rion18.push.sender.service.async.AsyncPushService#sendPushInBulk(java.lang.String, java.lang.String, java.util.Map, java.lang.String[])
9494
*/
9595
@Override
9696
public void sendPushInBulk(final String title, final String message,
@@ -115,7 +115,7 @@ public void run() {
115115
}
116116

117117
/*
118-
* @see com.rion18.pusher.service.async.AsyncPushService#setPushCallback(com.rion18.pusher.callback.PushCallback)
118+
* @see com.rion18.push.sender.service.async.AsyncPushService#setPushCallback(com.rion18.push.sender.callback.PushCallback)
119119
*/
120120
@Override
121121
public void setPushCallback(PushCallback pushCallback) {

src/main/java/com/devsu/push/sender/service/sync/SyncAndroidPushService.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ private void setDefaultValues() {
7979
}
8080

8181
/*
82-
* @see com.rion18.pusher.service.sync.PushService#sendPush(java.lang.String, java.lang.String, java.util.Map, java.lang.String)
82+
* @see com.devsu.push.sender.service.sync.SyncPushService#sendPush(java.lang.String, java.lang.String, java.util.Map, java.lang.String)
8383
*/
8484
@Override
85-
public boolean sendPush(final String title, final String message,
86-
final Map<String, String> additionalFields, final String token) throws IOException {
85+
public boolean sendPush(final String title, final String message, final Map<String, String> additionalFields,
86+
final String token) throws IOException {
8787
if (!validateSingleData(log, message, token)) {
8888
return false;
8989
}
@@ -92,8 +92,23 @@ public boolean sendPush(final String title, final String message,
9292
return resultIsOk(result);
9393
}
9494

95+
/**
96+
* Sends a single push message.
97+
* @param msgBuilder The Message.Builder object.
98+
* @param token The push token.
99+
* @return <i>true</i> if the push message request was sent.
100+
* @throws Exception
101+
*/
102+
public boolean sendPush(Message.Builder msgBuilder, String token) throws Exception {
103+
if (!validateToken(log, token)) {
104+
return false;
105+
}
106+
Result result = senderService.send(msgBuilder.build(), token, maxRetries);
107+
return resultIsOk(result);
108+
}
109+
95110
/*
96-
* @see com.rion18.pusher.service.sync.PushService#sendPushInBulk(java.lang.String, java.lang.String, java.util.Map, java.lang.String[])
111+
* @see com.devsu.push.sender.service.sync.SyncPushService#sendPushInBulk(java.lang.String, java.lang.String, java.util.Map, java.lang.String[])
97112
*/
98113
@Override
99114
public boolean sendPushInBulk(final String title, final String message,
@@ -111,6 +126,23 @@ public boolean sendPushInBulk(final String title, final String message,
111126
return booleanResult;
112127
}
113128

129+
/**
130+
* Sends a bulk push message.
131+
* @param msgBuilder The Message.Builder object.
132+
* @param token The push token.
133+
* @return <i>true</i> if the push message request was sent.
134+
* @throws Exception
135+
*/
136+
public boolean sendPushInBulk(Message.Builder msgBuilder, String... tokens) throws Exception {
137+
boolean booleanResult = true;
138+
List<String[]> tokenLimitedList = ArrayUtil.splitArray(tokens, maxBulkSize);
139+
for (String[] tokenArray: tokenLimitedList){
140+
MulticastResult result = senderService.send(msgBuilder.build(), Arrays.asList(tokenArray), maxRetries);
141+
booleanResult = booleanResult ? resultIsOk(result) : false;
142+
}
143+
return booleanResult;
144+
}
145+
114146
/**
115147
* Default message builder generator.
116148
* @param title The push message title.

0 commit comments

Comments
 (0)