1、别名推送
public static void sendPush(List<String> list, Map<String, String> map,String taskName) {
JPushClient jpushClient = new JPushClient(PushTool.masterSecret, PushTool.appKey, 3);
PushPayload payload = null;
if (list != null && list.size() > 0) {
payload = buildPushObject_android_and_iosByAlias(list, map, taskName);
} else {
payload = buildPushPushPayloadToAll(map);
}
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
}
/**
* 根据别名指定唯一发送者
* @param alias
* @param map
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static PushPayload buildPushObject_android_and_iosByAlias(List alias,Map<String, String> map, String taskName) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.setAlert(taskName)
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(TITLE).addExtra("content", JSON.toJSONString(map)).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra("content", JSON.toJSONString(map)).build())
.build())
.build();
}
//组装数据
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>(0);
list.add("13");//list存放别名值,可以存放多个值
Map<String, String> map = new HashMap<String, String>(0);
map.put("id", "3542");
sendPush(list, map,"1223");
}
2、分组推送
public static void main(String[] args) {
testSendPush();
}
public static void testSendPush() {
// HttpProxy proxy = new HttpProxy("localhost", 3128);
// Can use this https proxy: https://github.com/Exa-Networks/exaproxy
ClientConfig clientConfig = ClientConfig.getInstance();
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
// Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig);
// Call setHttpClient to set httpClient,
// If you don't invoke this method, default httpClient will use NativeHttpClient.
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
jpushClient.getPushClient().setHttpClient(httpClient);
final PushPayload payload = buildPushObject_android_and_ios();
// for(int i=0;i<10;i++) {
// Thread thread = new Thread() {
// public void run() {
// for (int j = 0; j < 200; j++) {
// long start = System.currentTimeMillis();
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
LOG.error("Sendno: " + payload.getSendno());
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
LOG.error("Sendno: " + payload.getSendno());
}
// System.out.println("耗时" + (System.currentTimeMillis() - start) + "毫秒 sendCount:" + (++sendCount));
// }
// }
// };
// thread.start();
// }
// // For push, all you need do is to build PushPayload object.
// PushPayload payload = buildPushObject_all_alias_alert();
// try {
// PushResult result = jpushClient.sendPush(payload);
// LOG.info("Got result - " + result);
// // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
// // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
// // jpushClient.close();
// } catch (APIConnectionException e) {
// LOG.error("Connection error. Should retry later. ", e);
// LOG.error("Sendno: " + payload.getSendno());
//
// } catch (APIRequestException e) {
// LOG.error("Error response from JPush server. Should review and fix it. ", e);
// LOG.info("HTTP Status: " + e.getStatus());
// LOG.info("Error Code: " + e.getErrorCode());
// LOG.info("Error Message: " + e.getErrorMessage());
// LOG.info("Msg ID: " + e.getMsgId());
// LOG.error("Sendno: " + payload.getSendno());
// }
}
public static PushPayload buildPushObject_android_and_ios() {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.tag("111"))//设置别名
.setNotification(Notification.newBuilder()
.setAlert("alert content")
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle("Android Title").build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra("extra_key", "extra_value").build())
.build())
.build();
}
pom文件
<dependency>
<groupId>org.hibernate.java-persistence</groupId>
<artifactId>jpa-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>quartz</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.9</version>
</dependency>
版权声明:本文为qq970014558原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。