您的位置:首页 > 其它

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对象:

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  api jpush