简体   繁体   English

使用onNewIntent和活动生命周期来验证是否已看到消息

[英]Using onNewIntent and activity lifecycle to verify if a message has been seen

I have a push notification server to deliver messages to my Android app and I'd like to know the best way to notify the server when the message has been seen. 我有一个推送通知服务器,用于将消息传递到我的Android应用程序,我想知道在看到消息时通知服务器的最佳方法。

Currently i'm trying to send the ack message to the server as soon as the main activity of my app calls its onResume callback, but as I use a SingleTop main activity, and I treat some intents with the onNewIntent method, I cannot be sure if the onResume is due to an Intent or the app coming to the foreground. 目前,我正在尝试在应用程序的主要活动调用其onResume回调时将ack消息发送到服务器,但是由于我使用SingleTop主要活动,并且使用onNewIntent方法处理了一些意图,因此我不确定如果onResume是由于Intent或应用程序进入前台引起的。 I've mapped the callback flow in the three situations my activity can be when it receives an intent to start it, and they are: 我已经在三种情况下将回调流映射到我的活动收到启动意向时,它们是:

  • The activity is running on the foreground (top of the stack)(1) 活动在前台(堆栈顶部)上运行(1)

    Running -> onPause() -> onNewIntent(intent) -> onResume() 运行-> onPause()-> onNewIntent(intent)-> onResume()

  • The activity is paused on the foreground (screen of for example)(2) 活动在前景上暂停(例如,屏幕)(2)

    Paused -> onRestart() -> onStart() -> onResume() -> onPause() -> onNewIntent(intent) 暂停-> onRestart()-> onStart()-> onResume()-> onPause()-> onNewIntent(intent)

  • The activity is paused on the background (i'm in another activity, for example)(3) 该活动在后台暂停(例如,我在另一个活动中)(3)

    Paused on background -> onNewIntent(intent) -> onRestart() -> onStart() -> onResume() 在背景上暂停-> onNewIntent(intent)-> onRestart()-> onStart()-> onResume()

In the case i navigate to my activity without receiving an intent: 如果我导航到我的活动而没有收到意图:

  • The activity is paused and the user navigates to it (4) 活动已暂停,用户导航到该活动(4)

    Paused -> onRestart() -> onStart() -> onResume() 暂停-> onRestart()-> onStart()-> onResume()

In cases 1, 3 and 4 the user ends in a screen where he/she can actually see the message, so i want to send the ack message. 在情况1、3和4中,用户在屏幕上结束,他/她可以实际看到该消息,因此我想发送确认消息。

If there was a callback that is called just in the case the activity really become visible in the screen it'd be perfect, but I'm having trouble finding it. 如果有一个回调,只是在活动真正在屏幕上可见的情况下被调用,那将是完美的,但是我很难找到它。

Someone know how can I solve this? 有人知道我该如何解决?

I got a solution, maybe it isn't the best one, but is working for now. 我有一个解决方案,也许它不是最好的解决方案,但目前仍在工作。

I schedule the ack message every onResume of the target activity, and cancel it in case onPause is called to fast (what in most cases indicates that it was due to an external intent, not the activity being awake). 我在目标活动的每个onResume上安排ack消息,并在将onPause调用为fast的情况下将其取消(大多数情况下,这是由于外部意图引起的,而不是活动处于唤醒状态)。

onResume() onResume()

@Override
protected void onResume() {

    Log.d("MainActivity", "onResume");
    super.onResume();

    final SharedPreferences prefs = getSharedPreferences("lastMessageId", Context.MODE_PRIVATE);
    final Long messageId = prefs.getLong("lastMessageId", -1L);

    Runnable task = new Runnable() {

        @Override
        public void run() {
            if(messageId!=-1L){
                Request req = getAgent().notifyMessageSeen(messageId, ((ApplicationILocate)getApplication()).getUser(MainActivity.this), MainActivity.this);
                ((ApplicationILocate)getApplication()).addRequestToQueue(req);
                prefs.edit().putLong("lastMessageId", -1).commit();
            }
        }
    };
    future = scheduler.schedule(task, 1, TimeUnit.SECONDS);
    resumedTime = new Time();
    resumedTime.setToNow();
}

onPause() onPause()

protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.d("MainActivity", "onPause");

    pausedTime = new Time();
    pausedTime.setToNow();
    Log.d("MainActivity", "elapsed between resume and pause: "+(pausedTime.toMillis(false)-resumedTime.toMillis(false)));
    if(pausedTime.toMillis(false)-resumedTime.toMillis(false) < 100){
        future.cancel(true);
    }
}

So the message is receiving the ack every time it goes to the screen and the screen is not locked. 因此,消息每次进入屏幕都会收到确认,并且屏幕未锁定。 Any better solution is welcome. 任何更好的解决方案都是值得欢迎的。

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

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