极光推送 java

mave依赖
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.7</version>
</dependency>

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<String,String> 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<String,String> 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<String,String> 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;
    }


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **移动端设备: **Android:走自建的TCP长连接通道iOS : 走自家的系统推送通道,WinPhone:...
    亭台雨榭111阅读 3,835评论 0 4
  • publicclassJPushUtil { /** * 安卓iOS标签推送 *@paramtagValues 标...
    大灰狼_dee8阅读 1,080评论 0 1
  • 官方demo下载 由于项目需要推送功能,通过讨论,初步使用第三方平台推送,因为自己弄的话,时间上不允许,所以,就...
    X_Arts阅读 5,776评论 1 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,342评论 19 139
  • 极光官方java sdk文档http://www.jianshu.com/p/e1f6335d40f0 1.注册 ...
    陽_young阅读 1,656评论 0 3

友情链接更多精彩内容