简体   繁体   English

如何在 React Native 中运行后台任务?

[英]How can I run background tasks in React Native?

I've built a little iOS app in React Native that does location tracking, sending the lat/lng regularly to a server of the user's choosing.我在 React Native 中构建了一个小的 iOS 应用程序,它进行位置跟踪,定期将 lat/lng 发送到用户选择的服务器。 However this only works when the app is in the foreground.但是,这仅在应用程序处于前台时才有效。 How can I run this task in the background when the user is in other apps?当用户在其他应用程序中时,如何在后台运行此任务?

Currently, there is, unfortunately, no support for background tasks of any kind.不幸的是,目前不支持任何类型的后台任务。 The feature you are referring to would be a background timer.您所指的功能是后台计时器。 Such a timer is this product pain (a feature request) for react native, you may upvote it to show an increased demand for this feature.这样的计时器是 React Native 的产品痛点(功能请求) ,您可以对其进行投票以显示对该功能的需求增加。

EDIT 12/2016: There is still no real option.编辑 12/2016:仍然没有真正的选择。 You have the Headless JS API since RN 0.33, but it is only for android.自 RN 0.33 起,您就有Headless JS API,但它仅适用于 android。 Also your App will crash if this runs in the foreground so you have to be careful about using it.如果它在前台运行,您的应用程序也会崩溃,因此您必须小心使用它。 Thanks to @Feng for pointing that out.感谢@Feng 指出这一点。

现在有了react-native-background-task ,它是 Android 和 iOS 的单一 API。

I use this, and it seems to work: https://github.com/ocetnik/react-native-background-timer我使用这个,它似乎工作: https : //github.com/ocetnik/react-native-background-timer

Emit event periodically (even when app is in the background).定期发出事件(即使应用程序在后台)。

You can use the setInterval and setTimeout functions.您可以使用 setInterval 和 setTimeout 函数。 This API is identical to that of react-native and can be used to quickly replace existing timers with background timers.此 API 与 react-native 相同,可用于将现有计时器快速替换为后台计时器。

import BackgroundTimer from 'react-native-background-timer';

// Start a timer that runs continuous after X milliseconds
const intervalId = BackgroundTimer.setInterval(() => {
    // this will be executed every 200 ms
    // even when app is the background
    console.log('tic');
}, 200);

// Cancel the timer when you are done with it
BackgroundTimer.clearInterval(intervalId);

// Start a timer that runs once after X milliseconds
const timeoutId = BackgroundTimer.setTimeout(() => {
    // this will be executed once after 10 seconds
    // even when app is the background
    console.log('tac');
}, 10000);

// Cancel the timeout if necessary
BackgroundTimer.clearTimeout(timeoutId);

The React Native ecosystem has been moving at breakneck pace over the past few months, and a few plugins have popped up to address the pain of not being able to run code in the background.在过去的几个月里,React Native 生态系统一直在以惊人的速度发展,并且出现了一些插件来解决无法在后台运行代码的痛苦。

https://github.com/transistorsoft/react-native-background-fetch -- Wake up for 30 seconds periodically to run some arbitrary JS code. https://github.com/transistorsoft/react-native-background-fetch -- 定期唤醒 30 秒以运行一些任意 JS 代码。 Not suitable for high resolution geolocation, as the time between wake-ups will be 15min or more.不适合高分辨率地理定位,因为唤醒之间的时间将是 15 分钟或更长时间。

https://github.com/transistorsoft/react-native-background-geolocation -- A better fit for this situation, targeted specifically at geolocation in the background. https://github.com/transistorsoft/react-native-background-geolocation——更适合这种情况,专门针对后台的地理定位。

These libraries can help you to achieve your desired functionality:这些库可以帮助您实现所需的功能:

  1. react-native-background-job反应本机背景工作
  2. react-native-background-task 反应原生背景任务
  3. react-native-background-fetch 反应原生背景获取

Alternatively, you can use Headless JS from react-native.或者,您可以使用 react-native 中的Headless JS But its only available for Android.但它仅适用于Android。

With the use of endless task management system in android You can run the task in the background theater the app is in background or the app is in kill mode.通过在android中使用无尽的任务管理系统,您可以在应用程序处于后台或应用程序处于终止模式的后台影院中运行任务。

You can use native bridge to do that or You can use the package Example Explaned here:您可以使用本机桥接器来做到这一点,或者您可以使用此处解释的包示例:

React-native package to do the same WithOut bridging react-native-endless-background-service-without-notification React-native 包做同样的事情,没有桥接react-native-endless-background-service-without-notification

我们解决了这个问题,我在 Android 和 iOS 中编写了我们自己的模块,并使用事件通知前端

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

相关问题 React Native,如何让倒计时在后台运行 - React Native, how to make countdown run in background 如何在反应原生 1 秒后更改背景? - how can i change background after 1 second in react native? 我可以在 React Native 中使用 webview 作为屏幕背景吗? 如果是,如何? - Can I use webview as a screen background in react native? If yes, How? 如何在本机反应中渲染图像背景内的文本? - How can i render Text inside image background in react native? 如何在React Native中从字符串运行javascript逻辑? - How can I run javascript logic from a string in React Native? 如何在expo中运行后台任务 - How to run background tasks in expo 除了 react-native-background-task 之外,还有什么方法可以在 React Native 中执行后台任务吗? - Is there any way to do background tasks in React Native other than react-native-background-task? 有没有办法用我的例子用 react native expo 做后台任务? - Is there a way to do background tasks with react native expo with my example? 如何在 React Native 中并排放置在背景顶部和屏幕底部的两个按钮? - How can I position two buttons on top of the background side by side and at the bottom of the screen in React Native? 我可以检查后台运行的应用程序是否反应原生 - Can i check background running apps react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM