Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

SignalR Java Client not working in Android 12 #119

Open
johnifegwu opened this issue Mar 23, 2022 · 0 comments
Open

SignalR Java Client not working in Android 12 #119

johnifegwu opened this issue Mar 23, 2022 · 0 comments

Comments

@johnifegwu
Copy link

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();
}

}

`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant