Skip to content

Commit 2622c79

Browse files
authored
Merge pull request #1493 from syrepoman/main
Fix Android 4.4 and older devices fail to register to onesignal.com
2 parents f968db4 + a7933c9 commit 2622c79

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalRestClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import java.util.regex.Matcher;
4444
import java.util.regex.Pattern;
4545

46+
import javax.net.ssl.HttpsURLConnection;
47+
4648
class OneSignalRestClient {
4749
static abstract class ResponseHandler {
4850
void onSuccess(String response) {}
@@ -143,6 +145,13 @@ private static Thread startHTTPConnection(String url, String method, JSONObject
143145
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "OneSignalRestClient: Making request to: " + BASE_URL + url);
144146
con = newHttpURLConnection(url);
145147

148+
// https://github.com/OneSignal/OneSignal-Android-SDK/issues/1465
149+
// Android 4.4 and older devices fail to register to onesignal.com to due it's TLS1.2+ requirement
150+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1 && con instanceof HttpsURLConnection) {
151+
HttpsURLConnection conHttps = (HttpsURLConnection) con;
152+
conHttps.setSSLSocketFactory(new TLS12SocketFactory(conHttps.getSSLSocketFactory()));
153+
}
154+
146155
con.setUseCaches(false);
147156
con.setConnectTimeout(timeout);
148157
con.setReadTimeout(timeout);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.onesignal;
2+
3+
import java.io.IOException;
4+
import java.net.InetAddress;
5+
import java.net.Socket;
6+
7+
import javax.net.ssl.SSLSocket;
8+
import javax.net.ssl.SSLSocketFactory;
9+
10+
public class TLS12SocketFactory extends SSLSocketFactory {
11+
SSLSocketFactory sslSocketFactory;
12+
13+
public TLS12SocketFactory(SSLSocketFactory sslSocketFactory) {
14+
super();
15+
this.sslSocketFactory = sslSocketFactory;
16+
}
17+
18+
@Override
19+
public String[] getDefaultCipherSuites() {
20+
return sslSocketFactory.getDefaultCipherSuites();
21+
}
22+
23+
@Override
24+
public String[] getSupportedCipherSuites() {
25+
return sslSocketFactory.getSupportedCipherSuites();
26+
}
27+
28+
@Override
29+
public Socket createSocket() throws IOException {
30+
return enableTLS(sslSocketFactory.createSocket());
31+
}
32+
33+
@Override
34+
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
35+
return enableTLS(sslSocketFactory.createSocket(s, host, port, autoClose));
36+
}
37+
38+
@Override
39+
public Socket createSocket(String host, int port) throws IOException {
40+
return enableTLS(sslSocketFactory.createSocket(host, port));
41+
}
42+
43+
@Override
44+
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
45+
return enableTLS(sslSocketFactory.createSocket(host, port, localHost, localPort));
46+
}
47+
48+
@Override
49+
public Socket createSocket(InetAddress host, int port) throws IOException {
50+
return enableTLS(sslSocketFactory.createSocket(host, port));
51+
}
52+
53+
@Override
54+
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
55+
return enableTLS(sslSocketFactory.createSocket(address, port, localAddress, localPort));
56+
}
57+
58+
private Socket enableTLS(Socket socket) {
59+
if ((socket instanceof SSLSocket)) {
60+
((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.2"});
61+
}
62+
return socket;
63+
}
64+
}

0 commit comments

Comments
 (0)