PushKit.java
3.0 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
package com.air.push;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.jfinal.log.Log;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
public class PushKit {
// 康爱思
private final static String APP_KEY = "aced41f410cced7e08982f2c";
private final static String MASTER_SECRET = "0eda135e4bbba17f7e7a11d3";
public static void main(String[] args) {
Random random = new Random(System.currentTimeMillis());
Map<String, String> extras = new HashMap<String, String>();
extras.put("title", "title by kevin " + random.nextInt());
extras.put("summary", "summary " + random.nextInt());
extras.put("content", "content " + random.nextInt());
extras.put("createTime", new Date().toString());
extras.put("updateTime", new Date().toString());
extras.put("pushId", String.valueOf(10001));
extras.put("pushType", String.valueOf(PushType.PUSH_MESSAGE));
String alert = "alert by kevin " + random.nextInt();
String title = extras.get("title");
boolean flag = PushKit.push(PushKit.allAndroid(alert, title, extras));
Log.getLog(new PushKit().getClass()).error("flag-->" + flag);
}
private PushKit() {
}
public static boolean push(PushPayload payload) {
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
PushResult result = null;
try {
result = jpushClient.sendPush(payload);
Log.getLog(new PushKit().getClass()).error("Got result - " + result);
} catch (APIConnectionException e) {
// Connection error, should retry later
Log.getLog(new PushKit().getClass()).error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
Log.getLog(new PushKit().getClass()).error("Should review the error, and fix the request", e);
Log.getLog(new PushKit().getClass()).error("HTTP Status: " + e.getStatus());
Log.getLog(new PushKit().getClass()).error("Error Code: " + e.getErrorCode());
Log.getLog(new PushKit().getClass()).error("Error Message: " + e.getErrorMessage());
}
if (result != null) {
if (result.isResultOK()) {
return true;
}
}
return false;
}
public static PushPayload allAndroid(String alert, String title, Map<String, String> extras) {
return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all())
.setNotification(Notification.android(alert, title, extras)).build();
}
public static PushPayload allIos(String alert, String title, Map<String, String> extras) {
return PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all())
.setNotification(Notification.android(alert, title, extras)).build();
}
}