1、首先去极光官网下载java对应sdk,官网通道:
https://docs.jiguang.cn/jpush/resources/#sdk_1
2、把对应的jar包复制到自己web项目的lib下,并添加进去。
3、接下来直接使用就可以了,可能会有报错,但都不难解决,下述的代码涵盖推送几乎所有的情况
public class JpushClientUtil {
private final static String masterSecret = "";
private final static String appKey = "";
private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);
/**
* 推送给设备标识参数的用户
* @param registrationId 设备标识
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToRegistrationId(List<String> registrationList,String notification_title, String msg_title, String msg_content, String extrasparam,String type) {
int result = 0;
try {
PushPayload pushPayload=null;
if(type.equals("0")){//自定义推送
pushPayload=JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationList,notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
}else if(type.equals("1")){
pushPayload=JpushClientUtil.buildPushObject(registrationList,notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
}
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
//iOS测试
public static void testSendIosAlert() {
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
IosAlert alert = IosAlert.newBuilder()
.setTitleAndBody("test alert", "subtitle", "test ios alert json")
.setActionLocKey("PLAY")
.build();
try {
PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap<String, String>(), "alias1");
System.out.println("Got result - " + result);
} catch (APIConnectionException e) {
System.out.println("Connection error. Should retry later. "+e);
} catch (APIRequestException e) {
System.out.println("Error response from JPush server. Should review and fix it. "+e);
}
}
/**
* 发送给所有安卓用户
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有IOS用户
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JpushClientUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){