jpush极光推送机制

极光推送官方文档地址: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 (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;
    }
    /**
     * 向指定组发送消息
     * @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);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值