-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathPushedNotification.java
More file actions
274 lines (241 loc) · 7.69 KB
/
PushedNotification.java
File metadata and controls
274 lines (241 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package javapns.notification;
import javapns.devices.Device;
import javapns.notification.exceptions.ErrorResponsePacketReceivedException;
import java.util.ArrayList;
import java.util.List;
/**
* <p>An object representing the result of a push notification to a specific payload to a single device.</p>
* <p>
* <p>If any error occurred while trying to push the notification, an exception is attached.</p>
* <p>
* <p>If Apple's Push Notification Service returned an error-response packet, it is linked to the related PushedNotification
* so you can find out what the actual error was.</p>
*
* @author Sylvain Pedneault
*/
public class PushedNotification {
private Payload payload;
private Device device;
private ResponsePacket response;
private int identifier;
private long expiry;
private int transmissionAttempts;
private boolean transmissionCompleted;
private Exception exception;
protected PushedNotification(final Device device, final Payload payload) {
this.device = device;
this.payload = payload;
}
PushedNotification(final Device device, final Payload payload, final int identifier) {
this.device = device;
this.payload = payload;
this.identifier = identifier;
}
public PushedNotification(final Device device, final Payload payload, final Exception exception) {
this.device = device;
this.payload = payload;
this.exception = exception;
}
/**
* Filters a list of pushed notifications and returns only the ones that were successful.
*
* @param notifications a list of pushed notifications
* @return a filtered list containing only notifications that were succcessful
*/
public static List<PushedNotification> findSuccessfulNotifications(final List<PushedNotification> notifications) {
final List<PushedNotification> filteredList = new ArrayList<>();
for (final PushedNotification notification : notifications) {
if (notification.isSuccessful()) {
filteredList.add(notification);
}
}
return filteredList;
}
/**
* Filters a list of pushed notifications and returns only the ones that failed.
*
* @param notifications a list of pushed notifications
* @return a filtered list containing only notifications that were <b>not</b> successful
*/
public static List<PushedNotification> findFailedNotifications(final List<PushedNotification> notifications) {
final List<PushedNotification> filteredList = new ArrayList<>();
for (final PushedNotification notification : notifications) {
if (!notification.isSuccessful()) {
filteredList.add(notification);
}
}
return filteredList;
}
/**
* Returns the payload that was pushed.
*
* @return the payload that was pushed
*/
public Payload getPayload() {
return payload;
}
protected void setPayload(final Payload payload) {
this.payload = payload;
}
/**
* Returns the device that the payload was pushed to.
*
* @return the device that the payload was pushed to
*/
public Device getDevice() {
return device;
}
protected void setDevice(final Device device) {
this.device = device;
}
/**
* Returns the connection-unique identifier referred to by
* error-response packets.
*
* @return a connection-unique identifier
*/
public int getIdentifier() {
return identifier;
}
void setIdentifier(final int identifier) {
this.identifier = identifier;
}
/**
* Returns the expiration date of the push notification.
*
* @return the expiration date of the push notification.
*/
public long getExpiry() {
return expiry;
}
void setExpiry(final long expiry) {
this.expiry = expiry;
}
void addTransmissionAttempt() {
transmissionAttempts++;
}
/**
* Returns the number of attempts that have been made to transmit the notification.
*
* @return a number of attempts
*/
public int getTransmissionAttempts() {
return transmissionAttempts;
}
void setTransmissionAttempts(final int transmissionAttempts) {
this.transmissionAttempts = transmissionAttempts;
}
/**
* Returns a human-friendly description of the number of attempts made to transmit the notification.
*
* @return a human-friendly description of the number of attempts made to transmit the notification
*/
public String getLatestTransmissionAttempt() {
if (transmissionAttempts == 0) {
return "no attempt yet";
}
switch (transmissionAttempts) {
case 1:
return "first attempt";
case 2:
return "second attempt";
case 3:
return "third attempt";
case 4:
return "fourth attempt";
default:
return "attempt #" + transmissionAttempts;
}
}
/**
* Indicates if the notification has been streamed successfully to Apple's server.
* This does <b>not</b> indicate if an error-response was received or not, but simply
* that the library successfully completed the transmission of the notification to
* Apple's server.
*
* @return true if the notification was successfully streamed to Apple, false otherwise
*/
public boolean isTransmissionCompleted() {
return transmissionCompleted;
}
void setTransmissionCompleted(final boolean completed) {
this.transmissionCompleted = completed;
}
/**
* If a response packet regarding this notification was received,
* this method returns it. Otherwise it returns null.
*
* @return a response packet, if one was received for this notification
*/
public ResponsePacket getResponse() {
return response;
}
void setResponse(final ResponsePacket response) {
this.response = response;
if (response != null && exception == null) {
exception = new ErrorResponsePacketReceivedException(response);
}
}
/**
* <p>Returns true if no response packet was received for this notification,
* or if one was received but is not an error-response (ie command 8),
* or if one was received but its status is 0 (no error occurred).</p>
* <p>
* <p>Returns false if an error-response packet is attached and has
* a non-zero status code.</p>
* <p>
* <p>Returns false if an exception is attached.</p>
* <p>
* <p>Make sure you use the Feedback Service to cleanup your list of
* invalid device tokens, as Apple's documentation says.</p>
*
* @return true if push was successful, false otherwise
*/
public boolean isSuccessful() {
if (!transmissionCompleted) {
return false;
}
if (response == null) {
return true;
}
if (!response.isValidErrorMessage()) {
return true;
}
return false;
}
/**
* Returns a human-friendly description of this pushed notification.
*/
@Override
public String toString() {
final StringBuilder msg = new StringBuilder();
msg.append("[").append(identifier).append("]");
msg.append(transmissionCompleted ? " transmitted " + payload + " on " + getLatestTransmissionAttempt() : " not transmitted");
msg.append(" to token " );
try {
// Test if token is valid
new BasicDevice(device.getToken(), true);
msg.append(device.getToken().substring(0, 5) + ".." + device.getToken().substring(59, 64));
} catch (Exception e) {
msg.append("INVALID_TOKEN:[" + device.getToken() + "]");
}
if (response != null) {
msg.append(" ").append(response.getMessage());
}
if (exception != null) {
msg.append(" ").append(exception);
}
return msg.toString();
}
/**
* Get the exception that occurred while trying to push this notification, if any.
*
* @return an exception (if any was thrown)
*/
public Exception getException() {
return exception;
}
void setException(final Exception exception) {
this.exception = exception;
}
}