200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 极光推送JPush使用Java SDK开发

极光推送JPush使用Java SDK开发

时间:2019-07-07 09:29:23

相关推荐

极光推送JPush使用Java SDK开发

一、极光推送是什么?

极光推送消息介绍

上面是极光官方文档的介绍说明,作为初次使用者。如果对于官方文档解释看不明白的,可以多去网上找一些通俗易懂的博客文章之类的,然后再去看官方文档,最后再按需要进行集成调试和开发等。

简单来说:极光推送是一个免费的开源的手机APP消息推送平台(类似帅哥美女们常用的购物APP某宝、某东等,一些优惠券消息后台推送到你们手机上)。它集成了包括:SDK给客户端、JPush API给服务端、JPush Cloud服务器代理推送消息。一张图看懂整个逻辑(极光官方):

android和ios的区别在于,ios需使用apple APNS server专用服务代理。(这可能是米国佬的专业独裁吧)

二、使用Java SDK集成开发(服务端)

1、生成Appkey 和MasterSecret

关于Appkey 和MasterSecret 的介绍,这里只作简单说明:极光平台推送到对应的注册APP手机客户端所需要的参数;创建测试用的Appkey和MasterSecret的步骤和操作介绍可以去官网取经,或查阅其他博客文章等,一般作为服务器后台开发是不需要关心这个(客户端androi、ios开发者负责创建)。

AppKey和MasterSecret操作步骤

2、添加依赖jar包

笔者的项目使用的是SpringCloud微服务架构,自然使用的是Maven仓库。所以,首先需要添加maven依赖

<dependency><groupId>cn.jpush.api</groupId><artifactId>jpush-client</artifactId><version>3.3.9</version></dependency>

版本以3.3.9为例,目前最新版本出到3.3.12了,一般不建议使用最新版本,因为最新版本的东西都不太稳定。

3、集成Java SDK推送方法JPush

//极光推送至App端String title = record.getTitle(); //标题String contents = record.getContents(); //内容if(StringUtils.isEmpty(MasterSecret) || StringUtils.isEmpty(AppKey)){commonResponse.setCode(MsgReturnCode.APPKEY_MASTERSECRET_EXCEPTION.code);commonResponse.setMessage(MsgReturnCode.APPKEY_MASTERSECRET_EXCEPTION.message);return commonResponse;}PushResult pushResult = push(String.valueOf(title),contents);if(pushResult != null && pushResult.isResultOK()){log.info("针对别名" + title + "的信息推送成功!");commonResponse.setCode(CommonResponse.SUCCEE);commonResponse.setMessage("信息推送成功!");return commonResponse;}else{log.info("针对别名" + title + "的信息推送失败!");commonResponse.setCode(MsgReturnCode.SYSTEM_EXCEPTION.code);commonResponse.setMessage("信息推送失败!");return commonResponse;}

上面的String title = record.getTitle(); //标题

String contents = record.getContents(); //内容

就是推送的某条消息到用户手机上,所要展示的标题和内容。

JPushClient 调用集成SDK的推送方法sendPush();生成极光推送对象PushPayload

/*** 极光推送方法(采用java SDK)* @param title* @param contents* @return PushResult*/public PushResult push(String title,String contents){ClientConfig clientConfig = ClientConfig.getInstance();JPushClient jpushClient = new JPushClient(MasterSecret, AppKey, null, clientConfig);PushPayload payload = buildPushObject_android_ios_alias_alert(title,contents);try {return jpushClient.sendPush(payload);} catch (APIConnectionException e) {log.error("Connection error. Should retry later. ", e);return null;} 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());return null;}}

/*** 生成极光推送对象PushPayload(采用java SDK)* @param title* @param contents* @return PushPayload*/public PushPayload buildPushObject_android_ios_alias_alert(String title,String contents){return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all()).setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().addExtra("type", "infomation").setTitle(title).setAlert(contents).build()).addPlatformNotification(IosNotification.newBuilder().addExtra("type", "infomation").setAlert(contents).build()).build()).setOptions(Options.newBuilder().setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数).setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数).build()).build();}

.setPlatform(Platform.android_ios())

设置要推送的客户端的平台android、ios;可以单独设置android或ios;我这里就都设置了。

.setAudience(Audience.all())

设置要推送的目标群用户,按照各自业务区分需要,推送给目标群用户,我这里设置全部用户了。

.addPlatformNotification(AndroidNotification.newBuilder().addExtra("type", "infomation").setTitle(title).setAlert(contents).build()).addPlatformNotification(IosNotification.newBuilder().addExtra("type", "infomation").setAlert(contents).build())

设置安卓和ios平台的推送消息标题和内容。整个后台服务api接口开发就结束了,看着非常简单;因为极光本身集成了强大的SDK供用户使用,我们只需要引入jar依赖即可调用SDK中的方法,还有另一种单独调用极光服务的api开发,那个会稍微复杂点,当然对其原理的理解也会更加深入点。

三、在安卓和ios客户端对接

上面已经说了需要创建appKey和masterSecret。服务端跟客户端对应同一套参数,这样保证推送消息一致。

跟客户端的联调开发就不赘述了,这里只强调保证appKey和masterSecret参数一致就行了。

同名原创公众号:程序大视界

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。