android - 如何像前台服务一样启动socket.io来保持连接和监听消息

标签 android socket.io android-service foreground-service

我正在使用此源应用程序与其他设备聊天。但是如何让它像服务一样启动,以便我可以启动前台服务。 我需要在 Service 中重写 MainFragment 和 LoginActivity 吗?

socket.io 应用 socket.io-android-chat

我在类 SocketService 中尝试过类似的东西,即使应用程序关闭,我还需要在应用程序服务中包含其他内容以获取通知消息。

public class SocketService extends Service {
    private Socket mSocket;
    public static final String TAG = SocketService.class.getSimpleName();
    private static final String NOTIFICATION_CHANNEL_ID_DEFAULT = "App running in background";
    String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "on created", Toast.LENGTH_SHORT).show();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setGroup(GROUP_KEY_WORK_EMAIL);
        Notification notification = builder.build();
        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        // Set big text style.
        builder.setStyle(bigTextStyle);
        startForeground(3, notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "start command", Toast.LENGTH_SHORT).show();
        try {
            mSocket = IO.socket(Constants.CHAT_SERVER_URL);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        mSocket.on("newMessageReceived", onNewMessage);
        mSocket.connect();
        return START_STICKY;
    }

    private Emitter.Listener onNewMessage = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            JSONObject data = (JSONObject) args[0];
            String username;
            String message;
            try {
                username = data.getString("username");
                message = data.getString("message");
            } catch (JSONException e) {
                Log.e(TAG, e.getMessage());
                return;
            }

            Log.d(TAG, "call: new message ");
            setNotificationMessage(message, username);
        }
    };

    public void setNotificationMessage(CharSequence message, CharSequence title) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentTitle(title);
        builder.setContentText(message);
        NotificationManagerCompat nm = NotificationManagerCompat.from(this);
        nm.notify(3, builder.build());
    }
}

最佳答案

当您的应用程序处于后台时,您不应该使用前台服务来接收通知消息

相反,你应该使用 firebase push notification

然而,如果您仍然需要在前台服务中使用套接字连接

只需创建一个单例类来处理所有套接字连接并将其用作您的前台服务,如下所示

 public class SocketManger {


        private  static SocketManger socketManger;

        Socket socket;
        Callback<Boolean> onConnect;

        public void init(Callback<Boolean> onConnect){
            this.onConnect = onConnect;
            connectToSocket();
            listenToPublicEvents();
        }

        private void connectToSocket(){
            try{
                IO.Options opts = new IO.Options();
                //optional parameter for authentication
                opts.query = "token=" + YOUR_TOKEN;
                opts.forceNew = true;
                opts.reconnection = true;
                opts.reconnectionDelay = 1000;
                socket = IO.socket(YOUR_URL, opts);
                socket.connect();

            }
            catch(URISyntaxException e){

                throw new RuntimeException(e);
            }
        }

        private void listenToPublicEvents(){
            socket.on(Socket.EVENT_CONNECT, args -> {
                if(onConnect!=null)
                    onConnect.onResult(true);
                    } );

            socket.on(Socket.EVENT_DISCONNECT, args ->{
                    if(onConnect!=null)
                    onConnect.onResult(false);
                   });
        }


        public void emit(String event, JSONObject data, Ack ack){
            socket.emit(event, new JSONObject[]{data}, ack);
        }


        public void on(String event, Emitter.Listener em){
            socket.on(event, em);
        }


        public static SocketManger getSocketManger() {
            if(socketManger == null){
                socketManger = new SocketManger();
            }
            return socketManger;
        }


        public boolean isConnected(){
            return socket!=null && socket.connected();
        }

        public void onDestroy() {
            onConnect = null;
            socket.disconnect();
        }

    public interface Callback<T> {
      void onResult(T t);
     }

    }

并将此代码添加到您的前台服务

SocketManager socketManger = SocketManger.getSocketManger();

    @Override
    public void onCreate() {
      socketManger.init(this::onSocketEvent);
   }

 public void onSocketEvent(boolean connect){
    //your code when the socket connection connect or disconnect
    }

并确保在服务被销毁时断开套接字

     @Override
    public void onDestroy() {
        socketManger.onDestroy()
        super.onDestroy();
    }

关于android - 如何像前台服务一样启动socket.io来保持连接和监听消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041326/

相关文章:

android - 从服务启动 Activity 并在按下主页按钮后停止之前的 Activity

android - 在 AndroidManifest.xml 中使用 gradle task change activity 属性

android - 从详细 View 返回时,Flutter TabBar 会重置索引

android - future 已经完成的 flutter 错误

mysql - NodeJS - 处理 100 多个并发连接的内存不足

javascript - 为 socket.io 禁用 jsonpolling?

Android如何从服务中的特定类获取上下文

android - 用于长时间后台操作的广播接收器或服务?

Android WSDL/SOAP 服务客户端

node.js - Socket.io 根据 http 请求断开连接