real time notifications over websocket #1000

Merged
akinwale merged 2 commits from notification-websocket into master 2020-09-09 15:16:23 +02:00
3 changed files with 43 additions and 0 deletions
Showing only changes of commit 5edbe763b1 - Show all commits

View file

@ -117,6 +117,8 @@ dependencies {
implementation 'com.arthenica:mobile-ffmpeg-full-gpl:4.3.1.LTS' implementation 'com.arthenica:mobile-ffmpeg-full-gpl:4.3.1.LTS'
implementation 'org.java-websocket:Java-WebSocket:1.5.1'
compileOnly 'org.projectlombok:lombok:1.18.10' compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10' annotationProcessor 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

View file

@ -98,6 +98,8 @@ import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -107,6 +109,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.URI;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -199,6 +202,7 @@ import io.lbry.lbrysdk.ServiceHelper;
import io.lbry.lbrysdk.Utils; import io.lbry.lbrysdk.Utils;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.SneakyThrows;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
public class MainActivity extends AppCompatActivity implements SdkStatusListener { public class MainActivity extends AppCompatActivity implements SdkStatusListener {
@ -240,6 +244,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
@Setter @Setter
private BackPressInterceptor backPressInterceptor; private BackPressInterceptor backPressInterceptor;
private WebSocketClient webSocketClient;
@Getter @Getter
private String firebaseMessagingToken; private String firebaseMessagingToken;
@ -1007,6 +1012,9 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
if (receivedStopService || !isServiceRunning(this, LbrynetService.class)) { if (receivedStopService || !isServiceRunning(this, LbrynetService.class)) {
notificationManager.cancelAll(); notificationManager.cancelAll();
} }
if (webSocketClient != null) {
webSocketClient.close();
}
if (dbHelper != null) { if (dbHelper != null) {
dbHelper.close(); dbHelper.close();
} }
@ -1072,10 +1080,41 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
@SneakyThrows
private void checkWebSocketClient() {
if ((webSocketClient == null || webSocketClient.isClosed()) && !Helper.isNullOrEmpty(Lbryio.AUTH_TOKEN)) {
webSocketClient = new WebSocketClient(new URI(String.format("%s%s", Lbryio.WS_CONNECTION_BASE_URL, Lbryio.AUTH_TOKEN))) {
@Override
public void onOpen(ServerHandshake handshakedata) {
}
@Override
public void onMessage(String message) {
loadRemoteNotifications(false);
}
@Override
public void onClose(int code, String reason, boolean remote) {
}
@Override
public void onError(Exception ex) {
}
};
webSocketClient.connect();
}
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
checkPurchases(); checkPurchases();
checkWebSocketClient();
enteringPIPMode = false; enteringPIPMode = false;
applyNavbarSigninPadding(); applyNavbarSigninPadding();
@ -2649,6 +2688,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
loadRemoteNotifications(false); loadRemoteNotifications(false);
checkUrlIntent(getIntent()); checkUrlIntent(getIntent());
checkWebSocketClient();
LbryAnalytics.logEvent(LbryAnalytics.EVENT_APP_LAUNCH); LbryAnalytics.logEvent(LbryAnalytics.EVENT_APP_LAUNCH);
appStarted = true; appStarted = true;
} }

View file

@ -57,6 +57,7 @@ public final class Lbryio {
public static final String TAG = "Lbryio"; public static final String TAG = "Lbryio";
public static final String CONNECTION_STRING = "https://api.lbry.com"; public static final String CONNECTION_STRING = "https://api.lbry.com";
public static final String WS_CONNECTION_BASE_URL = "wss://api.lbry.com/subscribe?auth_token=";
public static final String AUTH_TOKEN_PARAM = "auth_token"; public static final String AUTH_TOKEN_PARAM = "auth_token";
public static List<Subscription> subscriptions = new ArrayList<>(); public static List<Subscription> subscriptions = new ArrayList<>();
public static List<Claim> cacheResolvedSubscriptions = new ArrayList<>(); public static List<Claim> cacheResolvedSubscriptions = new ArrayList<>();