极光推送-java工具类

前言
  • 先前项目中用的推送时腾讯的信鸽推送,总是存在很多问题,现在改为极光,记录下,java的推送工具类
代码
    /**
     * 指定注册Id发送
     *
     * @param pushPKId 保存的推送表记录主键id
     * @param jpushRegId 注册ID
     * @param type 类型
     * @param jsonObject 需要app里处理的数据
     */
    private static boolean androidSendPush(final String pushPKId,final String jpushRegId, final String type, final JsonObject jsonObject) {
        logger.debug("jpushRegId:"+jpushRegId + ", type:"+type+", data:"+(jsonObject != null ? jsonObject.toString() : "{}"));
        ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);

        PushPayload payload = buildPushAndroid(pushPKId,jpushRegId, type, getTypeMap().get(type), jsonObject);
        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.info("Jpush Got result - " + result);
            return true;

        } catch (APIConnectionException e) {
            logger.error("Connection error. Should retry later. ", e);
            return false;

        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. ", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
            return false;
        }
    }

    /**
     * 构造PushPayload
     *
     * @param pushPkID 保存的推送表id
     * @param jpushRegId
     * @param type
     * @param jsonObject
     * @return
     */
    private static PushPayload buildPushAndroid(String pushPkID,final String jpushRegId, final String type, final String msgContent, final JsonObject jsonObject) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(jpushRegId))
                //发送通知
//                .setNotification(Notification.newBuilder()
//                        .addPlatformNotification(AndroidNotification.newBuilder()
//                                .setAlert(ALERT)
//                                .addExtra("type", type)
//                                .addExtra("data", jsonObject)
//                                .build())
//                        .build())

                //发送消息
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .addExtra("msgType", type)
                        .addExtra("msgContent", jsonObject != null ? jsonObject.toString() : "{}" )
                        .addExtra("knTchgId", pushPkID) 
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//是否用于生产环境
                        .build())
                .build();
    }

    /**
     * 获取push推送描述
     *
     * @return
     */
    private static Map getTypeMap(){
        typeMap.put(PUSH_TYPE_BILL_, "您有一条新的待收集订单");
        typeMap.put(PUSH_TYPE_TRANSFER_, "您有一条USERNAME转交的订单");
        typeMap.put(PUSH_TYPE_DRIVER_CONFIRM_, "您有一条出库单待确认");
        //TODO 其他类型的按这个规则添加
        return typeMap;
    }

你可能感兴趣的:(自己造轮子-工具类)