极光推送java后端代码示例

/**
     * 极光消息推送(sendJGPush)
     * @author  3hcxd
     * @param type  推送方式(alias别名 registrationID注册ID  tag标签  all广播)
     * @param parm  推送内容参数(msg、account、title)
     * @param regID 推送ID(取决于前端使用推送方式产生的推送ID)
     * */
    public static void sendJGPush(String type,Map<String, String> parm,String regID) {
        String jpush_master_secret = "极光秘钥"; //app密钥 创建应用后生成(对应极光账号创建的应用秘钥)
        String jpush_app_key = "极光Key";//appKey 创建应用后生成(对应极光账号创建的应用ID)
        JPushClient jpushClient = new JPushClient(jpush_master_secret, jpush_app_key);
        PushPayload payload = buildPushPayload(type, parm,regID);  //极光推送消息体
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result.error);
            System.out.println(result.statusCode);
            System.out.println(result.msg_id);
            System.out.println(result.sendno);
            // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
            // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
            jpushClient.close();
        } catch (APIConnectionException e) {
            System.out.println("APIConnectionException");
            e.printStackTrace();
        } catch (APIRequestException e) {
            System.out.println("APIRequestException");
            e.printStackTrace();
        }
    }

    /**
     * 生成极光推送对象PushPayload
     * @param type
     * @param regID
     * @param parm
     * @return PushPayload
     */
    public static PushPayload buildPushPayload(String type,Map<String, String> parm,String regID){
        String msg = parm.get("msg");
        String title = parm.get("title");
        Map<String, String> extras = new HashMap<>();
        extras.put("Account", parm.get("account"));
        Audience audience = null;
        switch (type) {
            case "registrationID":
                audience = Audience.registrationId(regID);//指定设备标识的用户
                break;
            case "alias":
                audience = Audience.alias(regID);//使用别名标识的用户
                break;
            case "tag":
                audience = Audience.tag(regID);//使用标签标识的用户
                break;
            default:
                audience = Audience.all(); //所有用户
                break;
        }
        return  PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(audience)
                .setOptions(Options.sendno())
                .setNotification(Notification.android(msg, title, extras))
                .build();
    }

    public static void main(String[] args) {
        String type = "alias";              //此系统使用别名作为推送方式
        Map<String, String> parm = new HashMap<>();
        String content = "收到请及时处理";
        parm.put("msg",content);
        parm.put("account","testAccountID");
        parm.put("title","新待办通知");
        String regId = "2c9223876961c2d3016979c169760dd1";  //(此系统使用系统用户ID作为别名推送,前端注意判断)
        sendJGPush(type,parm,regId);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值