极光推送java测试

1.RestTemplate支持Https请求

@Configuration
public class RestTemplateConf {
    @Bean
    public RestTemplate restTemplateHttps() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain,String authType)->true;
        SSLContext sslContext = SSLContexts.custom()
                .loadTrustMaterial(null,acceptingTrustStrategy)
                .build();
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }
}

2.测试发送消息

package com.zw.zwpp.rabbitmq;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;

@RestController
public class JPushController {
	@Autowired
	private RestTemplate restTemplateHttps;

	@RequestMapping("/jpushTest")
	public String jpushTest() {
		Map<String, Object> map = new HashMap<>();
		map.put("platform", "all");
		map.put("audience", "all");
		Map<String, Object> notificationMap = new HashMap<>();
		notificationMap.put("alert", "Hi, JPush!");
		map.put("notification", notificationMap);
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/json");
		String base64 = Base64.encodeBase64String("224d950fb1d0406d394251fc:3f2d288c6447b2ec12e15d05".getBytes());
		headers.add("Authorization", "Basic " + base64);
		String jsonString = JSONObject.toJSONString(map);
		System.out.println(jsonString);
		HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<Map<String, Object>>(map,
				headers);
		JSONObject respJSON = restTemplateHttps.postForObject("https://api.jpush.cn/v3/push", httpEntity,
				JSONObject.class);
		return respJSON.toJSONString();
	}

}

发布了47 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_37460672/article/details/100886055
今日推荐