@capacitor/background-runner
Background Runnerは、Webビューの外でJavaScriptコードを実行するためのイベントベースのスタンドアロンJavaScript環境を提供します。
Install
npm install @capacitor/background-runner
npx cap sync
Background Runnerは、使用前にユーザーからのパーミッションが必要な様々なデバイスAPIをサポートしています。
iOS
On iOS you must enable the Background Modes capability.

Once added, you must enable the Background fetch and Background processing modes at a minimum to enable the ability to register and schedule your background tasks.
If you will be making use of Geolocation or Push Notifications, enable Location updates or Remote notifications respectively.

After enabling the Background Modes capability, add the following to your app's AppDelegate.swift:
At the top of the file, under import Capacitor add:
import CapacitorBackgroundRunner
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ....
BackgroundRunnerPlugin.registerBackgroundTask()
BackgroundRunnerPlugin.handleApplicationDidFinishLaunching(launchOptions: launchOptions)
// ....
return true
}
To allow the Background Runner to handle remote notifications, add the following:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// ....
BackgroundRunnerPlugin.dispatchEvent(event: "remoteNotification", eventArgs: userInfo) { result in
switch result {
case .success:
completionHandler(.newData)
case .failure:
completionHandler(.failed)
}
}
}
Geolocation
Apple requires privacy descriptions to be specified in Info.plist for location information:
NSLocationAlwaysUsageDescription(Privacy - Location Always Usage Description)NSLocationWhenInUseUsageDescription(Privacy - Location When In Use Usage Description)
Read about Configuring Info.plist in the iOS Guide for more information on setting iOS permissions in Xcode
Android
Insert the following line to android/app/build.gradle:
...
repositories {
flatDir{
dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
+ dirs '../../node_modules/@capacitor/background-runner/android/src/main/libs', 'libs'
}
}
...
If you are upgrading from 1.0.5 with an existing Android project, be sure to delete the android-js-engine-release.aar from android/src/main/libs.
Geolocation
This API requires the following permissions be added to your AndroidManifest.xml:
<!-- Geolocation API -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
The first two permissions ask for location data, both fine and coarse, and the last line is optional but necessary if your app requires GPS to function. You may leave it out, though keep in mind that this may mean your app is installed on devices lacking GPS hardware.
Local Notifications
Android 13 requires a permission check in order to send notifications. You are required to call checkPermissions() and requestPermissions() accordingly.
On Android 12 and older it won't show a prompt and will just return as granted.
Starting on Android 12, scheduled notifications won't be exact unless this permission is added to your AndroidManifest.xml:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
Note that even if the permission is present, users can still disable exact notifications from the app settings.
Read about Setting Permissions in the Android Guide for more information on setting Android permissions.