简体   繁体   English

带有 setTimeout 和推送通知的后台应用程序

[英]App in background w/ setTimeout and push notification

We're running into an issue where Push Notification code is being executed before the setTimeout handler is executed when the app is coming into the foreground.我们遇到了一个问题,即当应用程序进入前台时,在执行 setTimeout 处理程序之前执行推送通知代码。 Here is the sequence of events:以下是事件的顺序:

  1. User launches the app.用户启动应用程序。 setTimeout(this.logout, 10000) gets called. setTimeout(this.logout, 10000)被调用。
  2. App is put into the background.应用程序被置于后台。 Timer continues to count down.计时器继续倒计时。
  3. Wait for 10 seconds to pass.等待 10 秒钟通过。 Because the app is in the background, this.logout callback will not be invoked until the app is bought back into the foreground.因为应用在后台, this.logout直到应用被买回前台才会调用this.logout回调。
  4. Push notification is sent to device.推送通知发送到设备。
  5. Now, user taps on the Push Notification, which opens the app.现在,用户点击推送通知,打开应用程序。
  6. Push Notification code is executed执行推送通知代码
  7. this.logout code is executed immediately. this.logout代码立即执行。

The result is that the user is kicked out of the app before he can view the Push Notification.结果是用户在查看推送通知之前就被踢出了应用程序。

Is there a way to somehow re-arrange the events in the Event Queue so that this.logout is executed first and Push Notification handling code executed second?有没有办法以某种方式重新排列事件队列中的事件,以便先执行 this.logout,然后执行推送通知处理代码?

Edit: We're using the Expo Push Notifications SDK编辑:我们正在使用 Expo Push Notifications SDK

Sample Code示例代码

componentDidMount() {
  this.notifications = Notifications.addListener(this.handleNotification);
  this.timeout = setTimeout(this.logout, 10000);
}

componentWillUnmount() {
  Notifications.remove(this.notifications);
}

this.handleNotification = () => { ... }
this.logout = () => { ... }

我通过关闭 setTimeout 计时器并使用时间戳 (Date.now() - startTime) 来查找经过的时间来解决它。

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

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