Socket.IO with battery reset service (Android)

Good afternoon.

I have a question regarding best battery management practices.

Don’t offer either GCM or Firebase before , as I am not interested in this, and I will explain the reason after.

So here is the script.

I have a social network deployed from my own.

I almost do not think that Viber , WhatsApp or Telegram uses any Google services, such as GCM or Firebase , plus I used both of them, and I am very dissatisfied with their delays and the price I get, so the best option for me was to save all notifications under the hood of Socket.IO.

So, each step:

• I created an always-running service that works no matter what.

• The service has a Socket.IO lib attached to it, and the service maintains a constant connection to the server through Socket.IO.

• The service processes all Socket.IO logic, which is as follows: full application notifications - group notifications, friend requests, message notifications, and all of them include all 10 types of notifications that can be inside my application.

• Everything was perfect if I did not see that my Samsung S7 Edge complains about using my application. I, although it was one of the typical Samsung, complains, but when I saw the use of the battery, I was shocked ... My application used MORE BATTERIES THAN ANDROID OS , which was a big hit for me.

:

.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    initSocketFully();
    return START_STICKY;
}

  private void initSocketFully() {
    sharedHelper = new SharedHelper(this);
    currentUserId = sharedHelper.getUserId();
    destroySocket();
    try {
        mSocket = IO.socket(SOCKET_URL);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    initSocket();

}

 private void initSocket() {
    mSocket.on(EVENT_CONNECT, onSocketConnected);
    mSocket.on(EVENT_CONNECT_ERROR, onSocketConnectionError);
    mSocket.on(EVENT_DISCONNECT, onSocketDisconnected);
    mSocket.on(EVENT_NEW_MESSAGE, onNewMessageReceived);
    mSocket.on(EVENT_STOPPED_TYPING, onUserStoppedTyping);
    mSocket.on(EVENT_TYPING, onUserTyping);
    mSocket.on(EVENT_MESSAGE_SENT, onMessageSent);
    mSocket.on(EVENT_CONNECT_TIMEOUT, onSocketTimeOut);
    mSocket.on(EVENT_ONLINE_STATUS, onOnlineStatusReceived);
    mSocket.on(EVENT_ON_GAME_CREATED, onMafiaGameCreated);
    mSocket.on(EVENT_ON_COMMENT_ADDED, onCommentAdded);
    mSocket.on(EVENT_ON_FRIEND_REQUEST_ACCEPTED, onFriendRequestAccepted);
    mSocket.on(EVENT_ON_FRIEND_REQUEST_DECLINED, onFriendRequestDeclined);
    mSocket.on(EVENT_ON_POST_LIKED, onPostLiked);
    mSocket.on(EVENT_ON_POST_MADE, onPostMade);
    mSocket.on(EVENT_ON_FRIEND_REQUESTED, onFriendRequested);

    mSocket.connect();
}

onDestroy :

 private void destroySocket() {
    if (mSocket != null) {
        mSocket.disconnect();
        mSocket.off(EVENT_CONNECT, onSocketConnected);
        mSocket.off(EVENT_ON_FRIEND_REQUEST_ACCEPTED, onFriendRequestAccepted);
        mSocket.off(EVENT_CONNECT_ERROR, onSocketConnectionError);
        mSocket.off(EVENT_ON_FRIEND_REQUEST_DECLINED, onFriendRequestDeclined);
        mSocket.off(EVENT_DISCONNECT, onSocketDisconnected);
        mSocket.off(EVENT_NEW_MESSAGE, onNewMessageReceived);
        mSocket.off(EVENT_STOPPED_TYPING, onUserStoppedTyping);
        mSocket.off(EVENT_ON_GAME_CREATED, onMafiaGameCreated);
        mSocket.off(EVENT_TYPING, onUserTyping);
        mSocket.off(EVENT_MESSAGE_SENT, onMessageSent);
        mSocket.off(EVENT_CONNECT_TIMEOUT, onSocketTimeOut);
        mSocket.off(EVENT_ONLINE_STATUS, onOnlineStatusReceived);
        mSocket.off(EVENT_ON_COMMENT_ADDED, onCommentAdded);
        mSocket.off(EVENT_ON_POST_LIKED, onPostLiked);
        mSocket.off(EVENT_ON_POST_MADE, onPostMade);
        mSocket.off(EVENT_ON_FRIEND_REQUESTED, onFriendRequested);
    }
}

, 10 , , , .

:

, Android, , , - , firebase GCM, Socket , ...

, ?

+4
1

, , . , , GCM (FCM), SocketIO FCM. SocketIO, , , . , FCM , .

0

Source: https://habr.com/ru/post/1674889/


All Articles