极光推送(Jpush) Java实现

大家在做消息推送时, 有没有去分别研究过android 和ios的消息推送原理,反正我觉得挺恶心的,所以还是借力于第三方推送吧, jpush完美地兼容了android 和ios , 最重要的是还免费。 https://www.jiguang.cn/push

第一步, 注册,获取MASTERSECRET和APPKEY

第二步, 下载JPushClient包

第三步,就按需看以下代码进行调用了。


public classPusherUtil {

private staticStringMASTERSECRET="";

private staticStringAPPKEY="";

private staticLoglog= LogFactory.getLog(PusherUtil.class);

private staticJPushClientjpushClient;

/**

* 交易成功消息推送

*/

public static booleansendPush(List personList,Map extraMap,String title,String alert) {

try{

Properties props = PropertiesLoaderUtils.loadAllProperties("notification.properties");

APPKEY= props.get("appKey").toString();

MASTERSECRET= props.get("masterSecret").toString();

String sound = extraMap.get("sound").toString();

sendPushByID(personList,extraMap,title,alert,sound);

return true;

}catch(IOException e) {

//            e.printStackTrace();

return false;

}catch(APIConnectionException e) {

//            e.printStackTrace();

return false;

}catch(APIRequestException e) {

//            e.printStackTrace();

System.out.println("APIRequestException");

return false;

}

}

/**

* 点对点推送

*/

public static voidsendPushByID(List toList,Map extraMap,String title,String alert,String sound)throwsAPIConnectionException,APIRequestException {

jpushClient=newJPushClient(MASTERSECRET,APPKEY);

Audience audienceList = Audience.registrationId(toList);

if(toList !=null){

for(Object o : toList) {

System.out.println("o = "+ o);

}

for(Integer integer : PersonController.appLoginPersonCache.keySet()) {

System.out.println("redId = "+ PersonController.appLoginPersonCache.get(integer).get("regId"));

}

}

Notification notification = Notification.newBuilder()

.addPlatformNotification(

IosNotification.newBuilder().setAlert(alert).setSound(sound +".mp3").addExtras(extraMap).build())

.addPlatformNotification(

AndroidNotification.newBuilder().setAlert(alert).setTitle(title).setBuilderId(1).addExtras(extraMap).build()).build();

PushPayload payload = PushPayload.newBuilder().setAudience(audienceList)

.setNotification(notification).setOptions(Options.newBuilder().setApnsProduction(false).build())

.setPlatform(Platform.all()).build();

try{

jpushClient.sendPush(payload);

}catch(NullPointerException e) {

e.printStackTrace();

System.out.println("NullPointerException");

}

}

/**

* 广播推送

*/

public staticPushResultsendPushAll(String title,String alert,Map map)throwsAPIConnectionException,APIRequestException {

jpushClient=newJPushClient(MASTERSECRET,APPKEY);

PushPayload payload = PushPayload.alertAll(alert);

returnjpushClient.sendPush(payload);

}

你可能感兴趣的:(极光推送(Jpush) Java实现)