Open johnifegwu opened 2 years ago
Hi,
I implemented SignalR Java Client in my Android Project. It works well on Android Versions 6 to 11 but fails on Android 12, I'm not sure what the problem is, but here is my code:
` import android.content.Context;
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.microsoft.signalr.HubConnection; import com.microsoft.signalr.HubConnectionBuilder; import com.microsoft.signalr.OnClosedCallback;
import org.json.JSONObject;
import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit;
import timber.log.Timber;
public class SignalRChatService {
Timer timer = new Timer(); public void startTimer() { timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { // Your code here Timer_Elapsed(); } }, 1000, 2000); } private void Timer_Elapsed() { try { module.checkNetwork(context); if(!Connected) { Init(); } } catch (Exception e) { Connected = false; } } private HubConnection _connection; public boolean Connected; public String instanceName; public int instanceProfileId; private boolean connecting; private SignalRMessageReceivedEvents signalRMessageReceivedEvents; private final Gson gson; private final Context context; public SignalRChatService(Context context, String instanceName, int instanceProfileId, SignalRMessageReceivedEvents signalRMessageReceivedEvents) { this.instanceName = instanceName; this.instanceProfileId = instanceProfileId; this.connecting = false; this.context = context; this.signalRMessageReceivedEvents = signalRMessageReceivedEvents; gson = new GsonBuilder().setPrettyPrinting().create(); } public void Stop() { try { _connection.stop(); } catch(Exception ignored) {} } public void Init() { if (connecting) { return; } try { connecting = true; _connection = HubConnectionBuilder.create("https://" + instanceName.trim().toLowerCase() + ".com").build(); try { _connection.start(); Connected = true; } catch (Exception e) { Connected = false; Timber.e("SignalR Push connect: %s", e.getLocalizedMessage()); return; } _connection.on("Message", (message) -> { try { // Separate this code affterwads JSONObject messageObject = new JSONObject(message); String Messagetype = (String) messageObject.get("messageType"); HandleWebSocketMessage(Messagetype, message); } catch (Exception ignored) { } }, String.class); _connection.onClosed(new OnClosedCallback() { @Override public void invoke(Exception exception) { handleClosed(); } }); } catch (Exception ignored) { } finally { connecting = false; startTimer(); } } private void handleClosed() { try { TimeUnit.MILLISECONDS.sleep(100); } catch (Exception ignored) { } Init(); }
}
`
Hi,
I implemented SignalR Java Client in my Android Project. It works well on Android Versions 6 to 11 but fails on Android 12, I'm not sure what the problem is, but here is my code:
` import android.content.Context;
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.microsoft.signalr.HubConnection; import com.microsoft.signalr.HubConnectionBuilder; import com.microsoft.signalr.OnClosedCallback;
import org.json.JSONObject;
import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit;
import timber.log.Timber;
public class SignalRChatService {
}
`