Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Sending notifications via Apple’s new HTTP/2 API

Hi,


i try to send a push notification via apns to a smart phone (apple).

The response status code indicates that the message was sent successfully and i also get a push notification. The problem is i always get two (!) push notifications to the phone for every single request I send!

I can't see the problem and hope somebody can help :).


I use jetty's libraries.

I orientated on this code:

https://judepereira.com/blog/sending-notifications-via-apples-new-http2-api-using-jetty-9-3-6/


To gradle dependencies I included

compile 'org.eclipse.jetty.http2:http2-client:9.4.10.v20180503'
compile 'org.eclipse.jetty:jetty-http:9.4.10.v20180503'
compile 'org.eclipse.jetty.http2:http2-http-client-transport:9.4.10.v20180503'


I could not handle to add the ALPN library to my boot classpath :/, so I included to my code

transport.setUseALPN(false);

and it works i.e. I did not get the error anymore. Maybe this is the problem?


Here's the code:


private ContentResponse sendMessage(String jsonMessage, String token, byte[] apnsCertificate, char[] apnCertificatePassword)  {

HTTP2Client http2Client = new HTTP2Client();
HttpClientTransportOverHTTP2 transport = new HttpClientTransportOverHTTP2(http2Client);
transport.setUseALPN(false);
ContentResponse response = null;

try {
http2Client.start();

KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
keyStore.load(new ByteArrayInputStream(apnsCertificate), apnCertificatePassword);

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, apnCertificatePassword);

SslContextFactory ssl = new SslContextFactory(true); //todo
ssl.setKeyStore(keyStore);
ssl.setKeyStorePassword("");

HttpClient httpClient = new HttpClient(transport, ssl);
httpClient.setFollowRedirects(false);
httpClient.start();

Request req = httpClient.newRequest(“https://api.development.push.apple.com”)
.method(HttpMethod.POST)
.path(“/3/device/”+ token)
.content(new StringContentProvider("{ \"aps\" : { \"alert\" : \"Hello\" } }"));

response = req.send();
httpClient.stop();
} catch (Exception e) {
LOG.error(“Message could not be sent: ” + e.toString());
}
return response;
}


thx

Mel





 

Back to the top