Java windows通知_Java客户端框架/ API来调用Windows手机推送通知服务 (Java client framework / API to call Windows phone p...

I've developed a solution with Spring and Apache Commons HTTP Client for sending RAW messages using WNS (Windows Notification Push Service)

Insert this dependencies on your pom.xml:

org.apache.httpcomponents

httpclient

4.2.1

org.codehaus.jackson

jackson-mapper-asl

1.9.12

And the following lines on applicationContext.xml

https://login.live.com/accesstoken.srf

*Remember to fill client_id and client_secret

In my project, I've divided the implementation in four classes, which I'm going to describe below

OAuthToken is used to store and format the access token:

public class OAuthToken {

private String token;

private String tokenType;

public OAuthToken(String token, String tokenType) {

super();

this.token = token;

this.tokenType = tokenType;

}

public String getAuthorization() {

return StringUtils.capitalize(tokenType) + " " + token;

}

}

The HttpClientFactory (shown bellow) is used to create a new client whenever you send a message. If you reuse the same HttpClient to send a message, WNS time-out after the second message sent. I don't know why that happens, but it solved when I stopped reusing the client.

@Service

public class HttpClientFactory {

private static final int TIMEOUT = 20 * 1000;

public HttpClient create() {

SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();

httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT);

httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT);

httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(NumberUtils.INTEGER_ZERO, false));

return httpClient;

}

}

This is a base class, as you wish, you can pull down all code to WindowsPushNotificationMediator

public abstract class AbstractCloudMediator {

private static final Logger LOG = Logger.getLogger(AbstractCloudMediator.class.getSimpleName());

@Autowired

private HttpClientFactory clientFactory;

@Autowired

protected ObjectMapper mapper;

public abstract boolean sendMessage(Jogador destinatario, Action mensagem);

protected String postToString(HttpPost post) throws IOException, ClientProtocolException {

HttpResponse response = executeMethod(post);

return responseToString(response);

}

protected String responseToString(HttpResponse response) throws IOException {

InputStream conteudoResposta = response.getEntity().getContent();

try {

return IOUtils.toString(conteudoResposta);

} finally {

IOUtils.closeQuietly(conteudoResposta);

}

}

protected HttpResponse executeMethod(HttpPost post) throws IOException, ClientProtocolException {

LOG.info("posting to... " + post);

return clientFactory.create().execute(post);

}

}

The next class should do the main work, but remember to create a cloudMessagingDAO to retrieve and store your access token.

You should replace the class Jogador for another class that contains the client URL, used to send a message to a Windows Phone device.

@Service(SharedConstants.WINDOWS_CLOUD_BEAN)

public class WindowsPushNotificationMediator extends AbstractCloudMediator { // NO_UCD (test only)

private static final Logger LOG = Logger.getLogger(WindowsPushNotificationMediator.class.getName());

private static final String KEY_ACCESS_TOKEN = "access_token";

private static final String KEY_TOKEN_TYPE = "token_type";

@Resource(name = "authenticateWNSHeaders")

private Map authenticateWNSHeaders;

@Resource(name = "authenticateWNSPostParams")

private Map authenticateWNSPostParams;

@Resource(name = "sendMessageWNSHeaders")

private Map sendMessageWNSHeaders;

@Autowired

@Qualifier("authenticateUrlPostWNS")

private String authenticateUrlPostWNS;

@Autowired

private CloudMessagingDAO cloudMessagingDAO;

private OAuthToken oathToken;

@Override

public boolean sendMessage(Jogador destinatario, Action mensagem) {

try {

OAuthToken token = getToken();

String jsonString = mapper.writeValueAsString(mensagem);

StringEntity entity = new StringEntity(jsonString, Consts.UTF_8);

return sendMessage(destinatario, entity, token);

} catch (IOException e) {

LOG.log(Level.SEVERE, e.getMessage(), e);

throw new RuntimeException(e);

}

}

private boolean sendMessage(Jogador destinatario, HttpEntity entity, OAuthToken token) throws IOException {

HttpPost post = new HttpPost(destinatario.getCloudMessagingInfo());// this is the client url

post.addHeader("Authorization", token.getAuthorization());

addPostHeaders(post, sendMessageWNSHeaders);

post.setEntity(entity);

HttpResponse response = executeMethod(post);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {

return sendMessage(destinatario, entity, getNewToken());

}

Header[] allHeaders = response.getAllHeaders();

StringBuilder builder = new StringBuilder();

for (Header header : allHeaders) {

builder.append(header.getName() + ": " + header.getValue());

builder.append('\n');

}

LOG.info(builder.toString());

return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;

}

private void addPostHeaders(HttpPost post, Map postHeaders) {

for (String key : postHeaders.keySet()) {

post.addHeader(key, postHeaders.get(key));

}

}

private OAuthToken getToken() throws IOException {

if (oathToken == null) {

//You should store your access token, so you can reuse it until it expires

String token = cloudMessagingDAO.getValue(KEY_ACCESS_TOKEN);

String tokenType = cloudMessagingDAO.getValue(KEY_TOKEN_TYPE);

if (StringUtils.isNotBlank(token) && StringUtils.isNotBlank(tokenType)) {

return oathToken = new OAuthToken(token, tokenType);

}

return getNewToken();

}

return oathToken;

}

private OAuthToken getNewToken() throws IOException {

HttpPost post = new HttpPost(authenticateUrlPostWNS);

addPostHeaders(post, authenticateWNSHeaders);

List params = new ArrayList<>();

for (String key : authenticateWNSPostParams.keySet()) {

params.add(new BasicNameValuePair(key, authenticateWNSPostParams.get(key)));

}

post.setEntity(new UrlEncodedFormEntity(params));

HttpResponse response = executeMethod(post);

String conteudo = responseToString(response);

LOG.info(conteudo);

if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

throw new NegocioException("Falha ao autenticar no serviço: " + conteudo);

}

@SuppressWarnings("unchecked")

Map resultMap = mapper.readValue(conteudo, HashMap.class);

cloudMessagingDAO.setValues(resultMap);

return oathToken = new OAuthToken(resultMap.get(KEY_ACCESS_TOKEN), resultMap.get(KEY_TOKEN_TYPE));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值