-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathPush.java
More file actions
100 lines (82 loc) · 3.11 KB
/
Copy pathPush.java
File metadata and controls
100 lines (82 loc) · 3.11 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
package io.rong.methods.push;
import java.net.HttpURLConnection;
import com.alibaba.fastjson.JSONException;
import com.google.gson.JsonParseException;
import io.rong.RongCloud;
import io.rong.models.CheckMethod;
import io.rong.models.push.BroadcastModel;
import io.rong.models.push.PushModel;
import io.rong.models.response.PushResult;
import io.rong.models.response.ResponseResult;
import io.rong.util.CommonUtil;
import io.rong.util.GsonUtil;
import io.rong.util.HttpUtil;
/**
* Push notification service
*
* @author lang
*/
public class Push {
private static final String UTF8 = "UTF-8";
private static final String PATH = "push";
private String appKey;
private String appSecret;
private RongCloud rongCloud;
public RongCloud getRongCloud() {
return rongCloud;
}
public void setRongCloud(RongCloud rongCloud) {
this.rongCloud = rongCloud;
}
public Push(String appKey, String appSecret) {
this.appKey = appKey;
this.appSecret = appSecret;
}
/**
* Broadcast
*
* @param broadcast Broadcast data
* @return PushResult
**/
public PushResult message(BroadcastModel broadcast) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(broadcast, PATH, CheckMethod.BROADCAST);
if (null != message) {
return (PushResult) GsonUtil.fromJson(message, PushResult.class);
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/push.json",
"application/json");
HttpUtil.setBodyParameter(broadcast.toString(), conn, rongCloud.getConfig());
return (PushResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.BROADCAST, HttpUtil.returnResult(conn, rongCloud.getConfig())),
PushResult.class);
}
/**
* Push notification
*
* @param push Push data
* @return PushResult
**/
public PushResult push(PushModel push) throws Exception {
// Fields to be validated
String message = CommonUtil.checkFiled(push, PATH, CheckMethod.PUSH);
if (null != message) {
return (PushResult) GsonUtil.fromJson(message, PushResult.class);
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/push.json",
"application/json");
HttpUtil.setBodyParameter(push.toString(), conn, rongCloud.getConfig());
PushResult result = null;
String response = "";
try {
response = HttpUtil.returnResult(conn, rongCloud.getConfig());
result = (PushResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.PUSH, response), PushResult.class);
} catch (JSONException | JsonParseException | IllegalStateException e) {
rongCloud.getConfig().errorCounter.incrementAndGet();
result = new PushResult(500, "");
result.setErrorMessage("request:" + conn.getURL() + " ,response:" + response + " ,JSONException:" + e.getMessage());
}
result.setReqBody(push.toString());
return result;
}
}