-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNotificationTaskServlet.java
More file actions
109 lines (85 loc) · 3.92 KB
/
NotificationTaskServlet.java
File metadata and controls
109 lines (85 loc) · 3.92 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
package com.firstrowria.pushnotificationtester.task;
import java.io.OutputStream;
import java.net.*;
import java.util.LinkedHashMap;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import org.json.JSONArray;
import org.json.JSONObject;
import com.firstrowria.pushnotificationtester.manager.ExceptionManager;
import com.google.appengine.api.taskqueue.*;
@WebServlet(
name = "NotificationTaskServlet",
urlPatterns = {"/notificationTask"}
)
public class NotificationTaskServlet extends HttpServlet {
private static final long serialVersionUID = -7629227131764824109L;
private static final Logger log = Logger.getLogger(NotificationTaskServlet.class.getName());
private static final String API_KEY = "";
// the automatic call of a TASK is always a post!
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String queueNameHeader = request.getHeader("X-AppEngine-QueueName");
if (queueNameHeader != null && queueNameHeader.equals("NotificationQueue")) {
String pushId = request.getParameter("id");
String deliveryPrio = request.getParameter("deliveryPrio");
String notificationPrio = request.getParameter("notificationPrio");
int count = Integer.parseInt(request.getParameter("count"));
log.info("pushId: " + pushId);
log.info("count: " + count);
log.info("deliveryPrio:" + deliveryPrio);
log.info("notificationPrio:" + notificationPrio);
if (API_KEY == null || API_KEY.equals(""))
log.warning("API KEY NOT SET - not sending notification");
else {
try {
JSONObject obj = new JSONObject();
LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();
data.put("title", "Test notification");
data.put("serverTime", String.valueOf(System.currentTimeMillis()));
data.put("notificationPrio", notificationPrio);
JSONArray registrationsIDList = new JSONArray();
registrationsIDList.put(pushId);
obj.put("registration_ids", registrationsIDList);
obj.put("data", data);
obj.put("priority", deliveryPrio.equals("0") ? "normal" : "high"); // normal or high
obj.put("time_to_live", 86400); // 1day
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json ; charset=utf-8");
connection.setRequestProperty("Authorization", "key=" + API_KEY);
OutputStream writer = connection.getOutputStream();
writer.write(obj.toString().getBytes("UTF-8"));
writer.close();
int responseCode = connection.getResponseCode();
String message = connection.getResponseMessage();
if (responseCode != HttpURLConnection.HTTP_OK) {
log.severe("GCM Error, HTTP RC: " + responseCode + ", message: " + message);
throw new Exception("GCM Error, HTTP RC: " + responseCode + ", message: " + message);
}
else
log.info("GCM Success, HTTP RC: " + responseCode);
}
catch (Exception e) {
ExceptionManager.handleException(e, "Exeption during sending GCM Message");
if (count == 0) // retry
{
count++;
(QueueFactory.getQueue("NotificationQueue")).add(createSendWatchListTask(pushId, count, deliveryPrio, notificationPrio));
}
}
}
}
}
private static TaskOptions createSendWatchListTask(String pushId, int count, String deliveryPrio, String notificationPrio) {
TaskOptions to = TaskOptions.Builder.withParam("id", pushId);
to = to.param("count", String.valueOf(count));
to = to.param("deliveryPrio", deliveryPrio);
to = to.param("notificationPrio", notificationPrio);
to = to.countdownMillis(5000); //try again in 5 sec
to = to.url("/notificationTask");
return to;
}
}