欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

APP消息推送 极光推送 示例代码

程序员文章站 2022-03-03 21:49:01
APP消息推送 极光推送 示例代码APP 进行别名绑定,以下接口进行 消息推送个推消息推送代码示例链接https://blog.csdn.net/qq_36992948/article/details/110129625/** * 描述: * * @author 闲走天涯 * @create 2020/11/6 16:59 */@Slf4j@RestController@RequestMapping("/jpushNew")public class JPushNewControll...

APP消息推送 极光推送 示例代码


APP 进行别名绑定,以下接口进行 消息推送

个推消息推送代码示例链接https://blog.csdn.net/qq_36992948/article/details/110129625

/**
 * 描述:
 *
 * @author 闲走天涯
 * @create 2020/11/6 16:59
 */
@Slf4j
@RestController
@RequestMapping("/jpushNew")
public class JPushNewController {

    @Value("${jpush.token:}")
    private String jpush_token;
    @Value("${jpush.pushUrl:}")
    private String jpush_url;//推送接口
    @Value("${jpush.android.intent:}")
    private String intent;//android 指定跳转页面intent
    @Value("${jpush.send.content:}")
    private String content;//消息内容 默认标题为APP

    /**
     * APP发送极光消息通知
     * @param alias 别名
     * @return
     */
    @RequestMapping("/appJpush")
    public JSONObject app_jpush(String alias){
        String data = "{\"platform\":\"all\",\"audience\":{\"alias\":[\""+alias+"\"]},\"notification\":{\"alert\":\""+content+"\",\"android\":{\"intent\":\""+intent+"\"},\"ios\":{\"sound\":\"sound.caf\"}}";
        return httpConnect_jpush(jpush_url,data);
    }

    /**
     *  极光推送 rest api
     * @param data
     * @return
     */
    public JSONObject httpConnect_jpush(String connectUrl, String data){
        JSONObject jsonObject = new JSONObject();
        log.info("【app极光推送】参数:data={},connectUrl={}",data,connectUrl);
        HttpsURLConnection conn = null;
        try{
            URL url = new URL(connectUrl);
            conn = (HttpsURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Authorization","Basic " + jpush_token);
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            conn.setRequestProperty("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE);
            conn.setRequestProperty("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");

            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.write(data.getBytes());
            wr.flush();
            wr.close();
            int responseCode = conn.getResponseCode();
            InputStream is;

            if (responseCode >= 400){is = conn.getErrorStream();}
            else{ is = conn.getInputStream();}
            String result = new BufferedReader(new InputStreamReader(is))
                    .lines().parallel().collect(Collectors.joining(System.lineSeparator()));
            jsonObject = JSONObject.parseObject(result);
            jsonObject.put("responseCode",responseCode);
            log.info("【app极光推送】结果result={}",jsonObject);
        }catch (Exception e){
            log.error("【app极光推送】异常",e);
            jsonObject.put("responseCode","500");
        }finally {
            if(conn!=null){
                conn.disconnect();
            }
        }
        return jsonObject;
    }
}

本文地址:https://blog.csdn.net/qq_36992948/article/details/110129927