简体   繁体   English

聊天应用在线状态

[英]Chat application online status

I'm trying to make a chat app.我正在尝试制作一个聊天应用程序。 Everything is working except one thing.除了一件事,一切都在工作。 So I want the users to see if the person they chat whit is online.所以我想让用户看到他们聊天的人是否在线。 I made that work but now every time the person is going in on an other chat the online status blink.我完成了这项工作,但现在每次该人进行其他聊天时,在线状态都会闪烁。 I have found where the problem is but I dont know how to fix it.我找到了问题所在,但我不知道如何解决。

So when a person1 is in the chat it can see a green box where it says online if person2 is online.因此,当 person1 在聊天时,如果 person2 在线,它会看到一个绿色框,上面写着在线。 But if person2 is going in to a chat the green box will blink for person1.但是如果 person2 正在聊天,绿色框将为 person1 闪烁。

The problem is in onResume and onPause.问题出在 onResume 和 onPause 上。 So when person2 is going from mainActivity to chatActivity it will do onPause() on mainActivity and set online to 0 and the do onResume() on chatActivity where it sets it to 1.因此,当 person2 从 mainActivity 转到 chatActivity 时,它将在 mainActivity 上执行 onPause() 并将在线设置为 0,在 chatActivity 上执行 onResume() 并将其设置为 1。

I know that the order is ActivityA.onPause, ActivityB.onCreate, ActivityB.onStart, ActivityB.onResume, ActivityA.onStop.我知道顺序是ActivityA.onPause、ActivityB.onCreate、ActivityB.onStart、ActivityB.onResume、ActivityA.onStop。 So I dont want it to do onPause() when it going from one Activity to another Activity but still do it when you tab down or close the app.因此,我不希望它在从一个 Activity 转到另一个 Activity 时执行 onPause(),但在您按下标签或关闭应用程序时仍然执行此操作。 is that possible?那可能吗?

public class BaseActivity extends AppCompatActivity {

    private DocumentReference documentReference;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PreferenceManager preferenceManager = new          
            PreferenceManager(getApplicationContext());
        FirebaseFirestore database = FirebaseFirestore.getInstance();
        documentReference = database.collection(Constants.KEY_COLLECTION_USERS)
                .document(preferenceManager.getString(Constants.KEY_USER_ID));
    }

    @Override
    protected void onPause() {
        super.onPause();
        documentReference.update(Constants.KEY_AVAILABLE, 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        documentReference.update(Constants.KEY_AVAILABLE, 1);
    }
}
public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        preferenceManager = new PreferenceManager(getApplicationContext());
        init();
        loadUserDetails();
        getToken();
        setListeners();
        listenConversations();
    }

}
public class ChatActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChatBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setListeners();
        loadReceiverDetails();
        init();
        listenMessages();
    }

    @Override
    protected void onResume() {
        super.onResume();
        listenAvailabilityOfReceiver();
    }

}

Did fix it in a ugly way.确实以丑陋的方式修复了它。 I made variable that knows if the user is still in the app or not.我做了一个变量,知道用户是否仍在应用程序中。

@Override
protected void onPause() {
    super.onPause();
    if (!stillInApp) {
        DocumentReference documentReference;
        PreferenceManager preferenceManager = new         
            PreferenceManager(getApplicationContext());
        FirebaseFirestore database = FirebaseFirestore.getInstance();
        documentReference = database.collection(Constants.KEY_COLLECTION_USERS)
            .document(preferenceManager.getString(Constants.KEY_USER_ID));
        documentReference.update(Constants.KEY_AVAILABLE, 0);
    }

}

@Override
protected void onResume() {
    super.onResume();
    DocumentReference documentReference;
    PreferenceManager preferenceManager = new 
        PreferenceManager(getApplicationContext());
    FirebaseFirestore database = FirebaseFirestore.getInstance();
    documentReference = database.collection(Constants.KEY_COLLECTION_USERS)
        .document(preferenceManager.getString(Constants.KEY_USER_ID));
    documentReference.update(Constants.KEY_AVAILABLE, 1);
    stillInApp = false;
    listenAvailabilityOfReceiver();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM