极光推送(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);

}

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容