极光推送 java

mave依赖

cn.jpush.api
jpush-client
3.3.7

private static final String APP_KEY ="自己的APP_KEY";
private static final String MASTER_SECRET = "自己的MASTER_SECRET ";


/***
     * 通用推送方法
     * @param userId 用户ID
     * @param msg 消息
     */

    public String messagePush(Integer userId, String clienType, String msg, Map map, Integer type){
        JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY , null, ClientConfig.getInstance());
        PushPayload payload =null;
        try{

            if("1".equals(clienType)){
                payload = buildPushObject_android_tag_alertWithTitle(userId+"",msg,map,type);
                PushResult  result = jpushClient.sendPush(payload);
                return result.statusCode+"";
            }
            if("2".equals(clienType)){
                payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(userId+"",msg,map,type);
                PushResult  result = jpushClient.sendPush(payload);
                return result.statusCode+"";
            }
        }catch (Exception e){
            LOGGER.error(e.getMessage());
        }finally {
            jpushClient.close();
        }

        return "客户端不存在";
    }



 public PushPayload buildPushObject_android_tag_alertWithTitle(String userId,String msg,Map map,Integer type) {
        map.put("alert",msg); // 消息
        map.put("title","标题"); // 标题
        JSONObject jsonObject = JSONObject.fromObject(map);
        PushPayload pushPayload = null;
        if(type.intValue()==0){
            pushPayload = PushPayload.newBuilder()
                    .setNotification(Notification.newBuilder().addPlatformNotification(
                            AndroidNotification.newBuilder()
                            .setAlert(msg)
                            .setTitle("间书")
                            .addExtras(map)
                            .build()
                    ).build())
                    .setOptions(Options.newBuilder().setApnsProduction(true).build())
                    .setPlatform(Platform.android()) // 发送Android
                    .setAudience(Audience.alias(userId)) // Audience.alias("userId") Audience.alias(userId+"")
                    /*.setMessage(Message.content(jsonObject.toString()))*/
                    .build();
        }else{
            pushPayload = PushPayload.newBuilder()
                    .setPlatform(Platform.android()) // 发送Android
                    .setAudience(Audience.alias(userId)) // Audience.alias("userId") Audience.alias(userId+"")
                   .setMessage(Message.content(jsonObject.toString()))
                    .build();
        }
        return pushPayload;
    }

    public  PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(String userId,String msg,Map map,Integer type) {
        map.put("alert",msg);

        JSONObject jsonObject = JSONObject.fromObject(map);
        PushPayload pushPayload = null;
        if(type.intValue()==0){
            pushPayload = PushPayload.newBuilder()
                    .setPlatform(Platform.ios())
                    .setAudience(Audience.alias(userId)) 
                    .setNotification(Notification.newBuilder()
                             .addPlatformNotification(IosNotification.newBuilder()
                                     .setContentAvailable(true)
                                     .setAlert(msg)
                                     .setBadge(0) // 角标数
                                     .setSound("happy") // 通知声音
                                     .addExtras(map)
                                     .build())
                             .build())
                    .setOptions(Options.newBuilder()
                            .setApnsProduction(false) // true-推送生产环境 false-推送开发环境(测试使用参数)
                            .build())
                    .build();
        }else{
            pushPayload = PushPayload.newBuilder()
                    .setPlatform(Platform.ios())
                    .setAudience(Audience.alias(userId)) 
                    .setMessage(Message.content(jsonObject.toString()))
                    .setOptions(Options.newBuilder()
                            .setApnsProduction(false) // true-推送生产环境 false-推送开发环境(测试使用参数)
                            .build())
                    .build();
        }

        return pushPayload;
    }

    /***
     * 更新绑定设备
     * @param registrationId
     * @param userId
     */
    public void updateDeviceTagAlias(String registrationId,Integer userId){
        JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY , null, ClientConfig.getInstance());
        try{
            jpushClient.updateDeviceTagAlias(registrationId,userId.toString(),null,null);
        }catch (Exception e){
            LOGGER.error(e.getMessage());
        }finally {
            jpushClient.close();
        }
    }

    /***
     * 删除设备信息
     * @param usreId
     */
    public void deleteAlias(Integer usreId){
        JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY , null, ClientConfig.getInstance());
        try{
            jpushClient.deleteAlias(usreId.toString(),null);
        }catch (Exception e){
            LOGGER.error(e.getMessage());
        }finally {
            jpushClient.close();
        }
    }

    /***
     * 根据设备 查信息
     * @param registrationId
     * @return
     */
    public String getDeviceTagAlias(String registrationId){
        JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY , null, ClientConfig.getInstance());
        String pushPayload = null;
        try {
            pushPayload =  jpushClient.getDeviceTagAlias("设备").toString();
        }catch (Exception e){
            LOGGER.error(e.getMessage());
        }finally {
            jpushClient.close();
        }
        return pushPayload;
    }


你可能感兴趣的:(极光推送 java)