jpush极光推送机制
2016-08-23 10:46
218 查看
极光推送官方文档地址:http://docs.jiguang.cn/
极光推送可以集成Android SDK和IOS SDK,编写后台服务轻松实现后端到前端的消息推送。虽然可以通过极光自己的portal界面(jpush用户平台)
不写代码实现推送,但为了方便,程序员还是会在自己的后台服务调用jpush的API实现消息推送。
1. 服务器端SDk下载: http://docs.jpush.cn/download/attachments/2228302/jpush-client-3.2.3.zip?version=2&modificationDate=1415166491000
除此之外,还可以使用maven自动加载jar包。
2. 创建JPushClient对象:
3. 创建PushPayload推送对象:
1)发送给所有平台的所有用户。
2)发送给android平台,向目标标签为tag的用户推送标题为title的内容
4. 推送消息:
5. 示例代码:
极光推送可以集成Android SDK和IOS SDK,编写后台服务轻松实现后端到前端的消息推送。虽然可以通过极光自己的portal界面(jpush用户平台)
不写代码实现推送,但为了方便,程序员还是会在自己的后台服务调用jpush的API实现消息推送。
1. 服务器端SDk下载: http://docs.jpush.cn/download/attachments/2228302/jpush-client-3.2.3.zip?version=2&modificationDate=1415166491000
除此之外,还可以使用maven自动加载jar包。
2. 创建JPushClient对象:
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
3. 创建PushPayload推送对象:
1)发送给所有平台的所有用户。
public static PushPayload buildPushObject_all_all_alert(String content) { return PushPayload.alertAll(content); }
2)发送给android平台,向目标标签为tag的用户推送标题为title的内容
public static PushPayload buildPushObject_android_tag_alertWithTitle (String alias,String title,String content) { return PushPayload.newBuilder().setPlatform(Platform.android()) .setAudience(Audience.tag(tag))//向指定的组推送 .setNotification(Notification.android(message, title, null)).build(); }
4. 推送消息:
PushResult result=jpushClient.sendPush(payload);
5. 示例代码:
public class MessagePush { private static final String appKey = "d1c241706d82996e1fcdc2b2"; private static final String masterSecret = "7ee1df1a631aee5a6a5a1129"; private JPushClient jpushClient ; private String title; private String content; public MessagePush(String message) { this.message = message; jpushClient = new JPushClient(masterSecret, appKey,3); } public MessagePush(String message,String title) { this(message); this.title=title; } /** * 向所有人发送消息 * @return 消息id */ public long sendPushAll(){ PushPayload payload=buildPushObject_all_all_alert(); long msgId=0; try { PushResult result=jpushClient.sendPush(payload); msgId=result.msg_id; } catch (APIConnectionException e) { // TODO Auto-generated catch block LOG.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { LOG.info("HTTP Status: " + e.getStatus()); msgId=e.getMsgId(); } return msgId; } /** * 向指定别名的客户端发送消息 * @param alias 所有别名信息集合,这里表示发送所有学生编号 * @return 消息id */ public long sendPushAlias(Set<String> alias){ PushPayload payloadAlias=buildPushObject_android_alias_alertWithTitle(alias); long msgId=0; try { PushResult result=jpushClient.sendPush(payloadAlias); msgId=result.msg_id; } catch (APIConnectionException e) { LOG.error("Connection error. Should retry later. ", e); } catch (APIRe b4cc questException 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()); msgId=e.getMsgId(); } return msgId; } /** * 向指定组发送消息 * @param tag 组名称 * @return 消息id */ public long sendPushTag(String tag) { PushPayload payloadtag = buildPushObject_android_tag_alertWithTitle(tag); long msgId=0; try { PushResult result = jpushClient.sendPush(payloadtag); msgId=result.msg_id; LOG.info("Got result - " + result); } catch (APIConnectionException e) { LOG.error("Connection error. Should retry later. ", e); } catch (APIRequestException 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()); msgId=e.getMsgId(); } return msgId; } /** * 下列封装了三种获得消息推送对象(PushPayload)的方法 * buildPushObject_android_alias_alertWithTitle、 * buildPushObject_android_tag_alertWithTitle、 * buildPushObject_all_all_alert */ public PushPayload buildPushObject_android_alias_alertWithTitle(Set<String> alias) { return PushPayload.newBuilder().setPlatform(Platform.android()) .setAudience(Audience.alias(alias)) .setNotification(Notification.android(message,title,null)).build(); } public PushPayload buildPushObject_android_tag_alertWithTitle(String tag){ return PushPayload.newBuilder().setPlatform(Platform.android()) .setAudience(Audience.tag(tag)) .setNotification(Notification.android(message, title, null)).build();} public PushPayload buildPushObject_all_all_alert() { return PushPayload.alertAll(message); }
相关文章推荐
- 极光推送 JPush 项目简单使用
- JPush极光推送API工具类(JAVA)
- JPush API client library for C Sharp(极光推送API)
- Android JPush(极光推送)的使用教
- ios开发--极光推送(jpush)遇坑1---导入sdk
- Android JPush(极光推送)的使用教程
- Android45_JPush极光推送及ShareSDK
- 极光推送JPush---自定义提示音
- 极光推送JPush --- 简单使用
- react-native 集成极光推送jpush-react-native时的小问题
- JPush极光推送 Java调用服务器端API开发
- java远程调用JPush极光推送api
- Jpush 极光推送遇到的几个问题
- Jpush 极光推送java服务器端
- thinkphp3.2.3集成Jpush极光推送
- Laravel 集成 JPush 极光推送指北
- JPush极光推送 Java调用服务器端API开发
- 极光推送(JPush)-认识提升
- 极光推送JPush客户端与服务器端的集成
- iOS JPush极光推送