极光推送服务端java简单实现

极光推送服务端java简单实现

这几天在公司使用了极光推送,所以就研究了一下
其实极光推送很简单,官方给的sdk全部都实现了,只需要简单的配置就可以实现推送:

极光推送分为 客户端 和 服务端,在这里小编就只写一下服务端Demo:


java服务端Demo

推送方式为:tag组的,message消息
public class JpushZhibuoClientUtil {
        //appkey和masterSecret是必须的,在激光推送的官网创建推送后就会有这两个值
        private final static String appKey = "xxxxxxxxxxxxxxxxxxxxxxxxx";

    private final static String masterSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx";

    private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);
/**
    * @Title: JPushIsTagAndMessage 
    * @Description: 极光推送,只需要调用此方法传入参数就可以推送
    * @author ljj
    * @date 2018年1月10日 下午2:36:21 
    * @param @param tag
    * @param @param ALERT
    * @param peopleMessage 
    * @param @return  参数说明 
    * @return int    返回类型 
    * @throws
     */
    public static int JPushIsTagAndMessage(String tag, String commentMessage, int peopleMessage){
        log.info("对Tag :" + tag + "的组推送信息");
        PushResult result = pushMessage(String.valueOf(tag),commentMessage,peopleMessage);
        if(result != null && result.isResultOK()){
            log.info("针对Tag :" + tag + "的信息推送成功!");
            return 1;
        }else{
            log.info("针对Tag :" + tag + "的信息推送失败!");
            return 0;
        }
    }
}

 /**
 * @param peopleMessage 
    * 
   * @Title: pushMessage 
   * @Description: 极光推送方法(采用java SDK)
   * @author ljj
   * @date 2018年1月10日 下午2:35:05 
   * @param @param tag
   * @param @param alert
   * @param @return  参数说明 
   * @return PushResult    返回类型 
   * @throws
    */
    public static PushResult pushMessage(String tag,String commentMessage, int peopleMessage){
        ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
        PushPayload payload = buildPushObject_android_ios_tag_message(tag, commentMessage,peopleMessage);
        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. ");
            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;
        }    
    }

 /**
     * @param peopleMessage 
     * 
    * @Title: buildPushObject_android_ios_alias_message 
    * @Description: 生成极光推送对象PushPayload(采用java SDK)
    * @author ljj
    * @date 2018年1月10日 下午2:24:06 
    * @param @param tag
    * @param @param message
    * @param @return  参数说明 
    * @return PushPayload    返回类型 
    * @throws
     */
    public static PushPayload buildPushObject_android_ios_tag_message(String tag,String commentMessage, int peopleMessage){
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag(tag))
                .setMessage(Message.newBuilder()
                        .setMsgContent("Hello word")
                        .setTitle("评论和人数")
                        .addExtra("comment",commentMessage)
                        .addExtra("people", peopleMessage)
                        .build())
                //.setMessage(Message.content("Hello world!"))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
                        .setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数)
                        .build())
                .build();
    }

在给一个全的例子,想要什么方式自己可以设置
 private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
        System.out.println("----------buildPushObject_android_registrationId_alertWithTitle");
        return PushPayload.newBuilder()
                //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
                .setPlatform(Platform.android())
                //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                .setAudience(Audience.all())
                //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
                .setNotification(Notification.newBuilder()
                        //指定当前推送的android通知
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(notification_title)
                                .setTitle(notification_title)
                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .addExtra("androidNotification extras key",extrasparam)
                                .build())
                        .build()
                )
                //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
                // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                .setMessage(Message.newBuilder()
                        .setMsgContent(msg_content)
                        .setTitle(msg_title)
                        .addExtra("message extras key",extrasparam)
                        .build())

                .setOptions(Options.newBuilder()
                        //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                        .setApnsProduction(false)
                        //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                        .setSendno(1)
                        //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                        .setTimeToLive(86400)
                        .build())
                .build();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值