JPustUtil.java
6.37 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
package com.zteits.pay.util;
import java.util.HashMap;
import java.util.Map;
import cn.jpush.api.push.model.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zteits.pay.configuration.JPushConfigure;
import com.zteits.pay.configuration.JPushPDAConfigure;
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.Message;
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 JPustUtil {
private static final Logger log = LoggerFactory.getLogger(JPustUtil.class);
private static JPushClient jpushClient = null;
private static JPushClient pdaJpushClient = null;
/**
* 单个android用户推送消息
* @param title
* @param msgContent
* @param alias
* @return
*/
public static PushResult sendaAndroidMessage(String title, String msgContent, String alias) {
PushResult result = new PushResult();
try {
result = getJpushClient().sendAndroidMessageWithAlias(title, msgContent, alias);
} catch (Exception e) {
log.debug(" 单个android用户推送消息 失败:"+e);
}
return result;
}
/**
* PDA单个android用户推送消息
* @param title
* @param msgContent
* @param alias
* @return
* 2017年11月28日 zhaowg
*/
public static PushResult pdaSendPush(String title, String msgContent,String... alias) {
PushResult result = new PushResult();
try {
PushPayload payload = sendAndroidMessageWithAlias(title,msgContent,null,alias);
result = getPdaJpushClient().sendPush(payload);
} catch (Exception e) {
e.printStackTrace();
log.debug(" 单个用户推送消息 失败:"+e);
}
return result;
}
/**
* @author wangbiao
* @date 2018/5/9 17:34
* @param alias, msgType, deivceType]
* @return cn.jpush.api.push.PushResult
*/
public static PushResult sendPush(String msgContent, String alias,String msgType,Integer deivceType) {
PushResult result = new PushResult();
try {
alias = alias.replace("-", "");
PushPayload payload = buildPushObject_all_alias_alert(msgContent,alias,msgType,deivceType);
result = getJpushClient().sendPush(payload);
} catch (Exception e) {
log.debug(" 单个用户推送消息 失败:"+e);
}
return result;
}
public static PushResult sendPush(String title, String msgContent, String contentType,String... alias) {
PushResult result = new PushResult();
try {
PushPayload payload = sendAndroidMessageWithAlias(title,msgContent,contentType,alias);
result = getJpushClient().sendPush(payload);
} catch (Exception e) {
log.debug(" 单个用户推送消息 失败:"+e);
}
return result;
}
public static PushResult sendPushIos(String title, String msgContent, String contentType,String... alias) {
PushResult result = new PushResult();
try {
PushPayload payload = sendIosMessageWithAlias(title,msgContent,contentType,alias);
result = getJpushClient().sendPush(payload);
} catch (Exception e) {
log.debug(" 单个用户推送消息 失败:"+e);
}
return result;
}
public static PushPayload buildPushObject_all_alias_alert(String msgContent,String alias,String msgType,Integer deivceType ) {
Map<String, String> extras = new HashMap<String, String>();
extras.put("msgType", msgType);
if(deivceType !=null && deivceType == 2){
log.info("IOS极光推送,alias:"+alias+",content:"+msgContent);
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(alias))
.setNotification(Notification.ios(msgContent, extras))
.setMessage(Message.newBuilder().setMsgContent(msgContent).addExtra("msgType", msgType).build())
.setOptions(Options.newBuilder().setApnsProduction(true).build())
.build();
}else{
log.info("Android极光推送,alias:"+alias+",content:"+msgContent);
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(alias))
// .setNotification(Notification.ios(alias, extras))
.setMessage(Message.newBuilder().setMsgContent(msgContent).addExtra("msgType", msgType).build())
.build();
}
}
public static PushPayload sendAndroidMessageWithAlias(String title, String msgContent, String contentType,String... alias)
throws APIConnectionException, APIRequestException {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.alias(alias))
.setMessage(Message.newBuilder()
.setTitle(title)
.setMsgContent(msgContent)
.setContentType(contentType)
.build())
.build();
}
/**
* 单个ios用户推送消息
* @param title
* @param msgContent
* @param alias
* @return
*/
public static PushResult sendaIosMessage(String title, String msgContent, String alias) {
PushResult result = new PushResult();
try {
result = getJpushClient().sendIosMessageWithAlias(title, msgContent, alias);
} catch (Exception e) {
log.error("单个ios用户推送消息 失败:{}",e);
}
return result;
}
public static PushPayload sendIosMessageWithAlias(String title, String msgContent, String contentType,String... alias)
throws APIConnectionException, APIRequestException {
return PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.alias(alias))
.setMessage(Message.newBuilder()
.setTitle(title)
.setMsgContent(msgContent)
.setContentType(contentType)
.build())
.build();
}
public static JPushClient getJpushClient() {
if(jpushClient==null){
return new JPushClient(JPushConfigure.getMASTER_SECRET(), JPushConfigure.getAPP_KEY(), null,
ClientConfig.getInstance());
}
return jpushClient;
}
public static JPushClient getPdaJpushClient() {
if(pdaJpushClient == null){
return new JPushClient(JPushPDAConfigure.getMASTER_SECRET(), JPushPDAConfigure.getAPP_KEY(), null,
ClientConfig.getInstance());
}
return pdaJpushClient;
}
}