@capacitor/background-runner 2.3.1 → 4.0.0-dev.28jul2026
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapacitorBackgroundRunner.podspec +1 -1
- package/Package.swift +2 -3
- package/README.md +9 -9
- package/android/build.gradle +13 -17
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Device.kt +1 -1
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Notifications.kt +2 -10
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/TimedNotificationPublisher.kt +1 -0
- package/android/src/main/libs/android-js-engine-release.aar +0 -0
- package/dist/docs-api.json +1 -1
- package/dist/docs.json +1 -1
- package/dist/esm/definitions.d.ts +2 -2
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorBackgroundRunner/CapacitorBackgroundRunner.swift +252 -225
- package/package.json +12 -13
|
@@ -11,7 +11,7 @@ Pod::Spec.new do |s|
|
|
|
11
11
|
s.author = package['author']
|
|
12
12
|
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
13
|
s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
-
s.ios.deployment_target
|
|
14
|
+
s.ios.deployment_target = '16.0'
|
|
15
15
|
s.dependency 'Capacitor'
|
|
16
16
|
s.swift_version = '5.1'
|
|
17
17
|
end
|
package/Package.swift
CHANGED
|
@@ -5,7 +5,7 @@ import PackageDescription
|
|
|
5
5
|
|
|
6
6
|
let package = Package(
|
|
7
7
|
name: "CapacitorBackgroundRunner",
|
|
8
|
-
platforms: [.iOS(.
|
|
8
|
+
platforms: [.iOS(.v16), .macOS(.v13)],
|
|
9
9
|
products: [
|
|
10
10
|
// Products define the executables and libraries a package produces, making them visible to other packages.
|
|
11
11
|
.library(
|
|
@@ -14,7 +14,7 @@ let package = Package(
|
|
|
14
14
|
)
|
|
15
15
|
],
|
|
16
16
|
dependencies: [
|
|
17
|
-
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "
|
|
17
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "9.0.0-alpha.5")
|
|
18
18
|
],
|
|
19
19
|
targets: [
|
|
20
20
|
// Targets are the basic building blocks of a package, defining a module or a test suite.
|
|
@@ -23,7 +23,6 @@ let package = Package(
|
|
|
23
23
|
name: "CapacitorBackgroundRunner",
|
|
24
24
|
dependencies: [
|
|
25
25
|
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
26
|
-
.product(name: "Cordova", package: "capacitor-swift-pm"),
|
|
27
26
|
"RunnerEngine"
|
|
28
27
|
],
|
|
29
28
|
path: "ios/Sources/CapacitorBackgroundRunner"
|
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Background Runner provides an event-based standalone JavaScript environment for
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @capacitor/background-runner
|
|
8
|
+
npm install @capacitor/background-runner
|
|
9
9
|
npx cap sync
|
|
10
10
|
```
|
|
11
11
|
|
|
@@ -189,14 +189,14 @@ On load, Background Runner will automatically register a
|
|
|
189
189
|
background task that will be scheduled and run once your app is
|
|
190
190
|
backgrounded.
|
|
191
191
|
|
|
192
|
-
| Prop | Type | Description
|
|
193
|
-
| --------------- | -------------------- |
|
|
194
|
-
| **`label`** | <code>string</code> | The name of the runner, used in logs.
|
|
195
|
-
| **`src`** | <code>string</code> | The path to the runner JavaScript file, relative to the app bundle.
|
|
196
|
-
| **`event`** | <code>string</code> | The name of the event that will be called when the OS executes the background task.
|
|
197
|
-
| **`repeat`** | <code>boolean</code> | If background task should repeat based on the interval set in `interval`.
|
|
198
|
-
| **`interval`** | <code>number</code> | The number of minutes after the
|
|
199
|
-
| **`autoStart`** | <code>boolean</code> | Automatically register and schedule background task on app load.
|
|
192
|
+
| Prop | Type | Description | Since |
|
|
193
|
+
| --------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
|
|
194
|
+
| **`label`** | <code>string</code> | The name of the runner, used in logs. | 1.0.0 |
|
|
195
|
+
| **`src`** | <code>string</code> | The path to the runner JavaScript file, relative to the app bundle. | 1.0.0 |
|
|
196
|
+
| **`event`** | <code>string</code> | The name of the event that will be called when the OS executes the background task. | 1.0.0 |
|
|
197
|
+
| **`repeat`** | <code>boolean</code> | If background task should repeat based on the interval set in `interval`. | 1.0.0 |
|
|
198
|
+
| **`interval`** | <code>number</code> | The number of minutes after the app is put into the background in which the background task should begin. If `repeat` is true, this also specifies the number of minutes between each execution. | 1.0.0 |
|
|
199
|
+
| **`autoStart`** | <code>boolean</code> | Automatically register and schedule background task on app load. | 1.0.0 |
|
|
200
200
|
|
|
201
201
|
### Examples
|
|
202
202
|
|
package/android/build.gradle
CHANGED
|
@@ -1,35 +1,31 @@
|
|
|
1
1
|
ext {
|
|
2
2
|
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
-
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.
|
|
4
|
-
|
|
5
|
-
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.
|
|
6
|
-
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.
|
|
7
|
-
workManagerVersion = project.hasProperty('workManagerVersion') ? rootProject.ext.workManagerVersion : '2.
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
4
|
+
androidxCoreVersion = project.hasProperty('androidxCoreVersion') ? rootProject.ext.androidxCoreVersion : '1.19.0'
|
|
5
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
|
|
6
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
7
|
+
workManagerVersion = project.hasProperty('workManagerVersion') ? rootProject.ext.workManagerVersion : '2.11.0'
|
|
8
8
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
buildscript {
|
|
12
|
-
ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '1.9.25'
|
|
13
12
|
repositories {
|
|
14
13
|
google()
|
|
15
14
|
mavenCentral()
|
|
16
15
|
}
|
|
17
16
|
dependencies {
|
|
18
|
-
classpath 'com.android.tools.build:gradle:
|
|
19
|
-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
17
|
+
classpath 'com.android.tools.build:gradle:9.2.1'
|
|
20
18
|
|
|
21
19
|
}
|
|
22
20
|
}
|
|
23
21
|
|
|
24
22
|
apply plugin: 'com.android.library'
|
|
25
|
-
apply plugin: 'org.jetbrains.kotlin.android'
|
|
26
23
|
|
|
27
24
|
android {
|
|
28
|
-
namespace "com.capacitorjs.plugins.backgroundrunner"
|
|
29
|
-
compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion :
|
|
25
|
+
namespace = "com.capacitorjs.plugins.backgroundrunner"
|
|
26
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 37
|
|
30
27
|
defaultConfig {
|
|
31
|
-
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion :
|
|
32
|
-
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35
|
|
28
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 26
|
|
33
29
|
versionCode 1
|
|
34
30
|
versionName "1.0"
|
|
35
31
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
@@ -37,11 +33,11 @@ android {
|
|
|
37
33
|
buildTypes {
|
|
38
34
|
release {
|
|
39
35
|
minifyEnabled false
|
|
40
|
-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
36
|
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
39
|
lintOptions {
|
|
44
|
-
abortOnError false
|
|
40
|
+
abortOnError = false
|
|
45
41
|
}
|
|
46
42
|
compileOptions {
|
|
47
43
|
sourceCompatibility JavaVersion.VERSION_21
|
|
@@ -61,10 +57,10 @@ repositories {
|
|
|
61
57
|
dependencies {
|
|
62
58
|
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
63
59
|
implementation project(':capacitor-android')
|
|
64
|
-
implementation
|
|
60
|
+
implementation ":android-js-engine-release@aar"
|
|
65
61
|
implementation "androidx.work:work-runtime-ktx:$workManagerVersion"
|
|
66
62
|
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
67
|
-
implementation "androidx.core:core
|
|
63
|
+
implementation "androidx.core:core:$androidxCoreVersion"
|
|
68
64
|
testImplementation "junit:junit:$junitVersion"
|
|
69
65
|
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
70
66
|
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
@@ -80,7 +80,7 @@ class Device (context: android.content.Context) : DeviceAPI {
|
|
|
80
80
|
return false
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
@
|
|
83
|
+
@Suppress("DEPRECATION")
|
|
84
84
|
private fun getAndParseNetworkInfo(): NetworkStatus {
|
|
85
85
|
val networkStatus = NetworkStatus()
|
|
86
86
|
val networkInfo = connectivityManager.activeNetworkInfo
|
|
@@ -211,17 +211,9 @@ class Notifications(context: Context) : NotificationsAPI {
|
|
|
211
211
|
PendingIntent.getBroadcast(context, request.id, intent, flags)
|
|
212
212
|
|
|
213
213
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !alarmManager.canScheduleExactAlarms()) {
|
|
214
|
-
|
|
215
|
-
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, request.scheduleAt.time, pendingIntent);
|
|
216
|
-
} else {
|
|
217
|
-
alarmManager.set(AlarmManager.RTC, request.scheduleAt.time, pendingIntent);
|
|
218
|
-
}
|
|
214
|
+
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, request.scheduleAt.time, pendingIntent)
|
|
219
215
|
} else {
|
|
220
|
-
|
|
221
|
-
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, request.scheduleAt.time, pendingIntent);
|
|
222
|
-
} else {
|
|
223
|
-
alarmManager.setExact(AlarmManager.RTC, request.scheduleAt.time, pendingIntent);
|
|
224
|
-
}
|
|
216
|
+
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, request.scheduleAt.time, pendingIntent)
|
|
225
217
|
}
|
|
226
218
|
}
|
|
227
219
|
}
|
package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/TimedNotificationPublisher.kt
CHANGED
|
@@ -34,6 +34,7 @@ class TimedNotificationPublisher : BroadcastReceiver() {
|
|
|
34
34
|
notificationManager.notify(id, notification)
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
@Suppress("DEPRECATION")
|
|
37
38
|
private fun getParcelableExtraLegacy(intent: Intent): android.app.Notification? {
|
|
38
39
|
return intent.getParcelableExtra(NOTIFICATION_KEY)
|
|
39
40
|
}
|
|
Binary file
|
package/dist/docs-api.json
CHANGED
|
@@ -948,7 +948,7 @@
|
|
|
948
948
|
"name": "example"
|
|
949
949
|
}
|
|
950
950
|
],
|
|
951
|
-
"docs": "The number of minutes after the
|
|
951
|
+
"docs": "The number of minutes after the app is put into the background in which the background task should begin.\n\nIf `repeat` is true, this also specifies the number of minutes between each execution.",
|
|
952
952
|
"complexTypes": [],
|
|
953
953
|
"type": "number"
|
|
954
954
|
},
|
package/dist/docs.json
CHANGED
|
@@ -369,7 +369,7 @@
|
|
|
369
369
|
"name": "example"
|
|
370
370
|
}
|
|
371
371
|
],
|
|
372
|
-
"docs": "The number of minutes after the
|
|
372
|
+
"docs": "The number of minutes after the app is put into the background in which the background task should begin.\n\nIf `repeat` is true, this also specifies the number of minutes between each execution.",
|
|
373
373
|
"complexTypes": [],
|
|
374
374
|
"type": "number"
|
|
375
375
|
},
|
|
@@ -25,7 +25,7 @@ export interface BackgroundRunnerConfig {
|
|
|
25
25
|
*/
|
|
26
26
|
repeat: boolean;
|
|
27
27
|
/**
|
|
28
|
-
* The number of minutes after the
|
|
28
|
+
* The number of minutes after the app is put into the background in which the background task should begin.
|
|
29
29
|
*
|
|
30
30
|
* If `repeat` is true, this also specifies the number of minutes between each execution.
|
|
31
31
|
*
|
|
@@ -76,7 +76,7 @@ declare module '@capacitor/cli' {
|
|
|
76
76
|
*/
|
|
77
77
|
repeat: boolean;
|
|
78
78
|
/**
|
|
79
|
-
* The number of minutes after the
|
|
79
|
+
* The number of minutes after the app is put into the background in which the background task should begin.
|
|
80
80
|
*
|
|
81
81
|
* If `repeat` is true, this also specifies the number of minutes between each execution.
|
|
82
82
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAAA,wCAAwC","sourcesContent":["/// <reference types=\"@capacitor/cli\" />\n\nimport type { PermissionState, PluginListenerHandle } from '@capacitor/core';\n\nexport interface BackgroundRunnerConfig {\n /**\n * The name of the runner, used in logs.\n *\n * @since 1.0.0\n */\n label: string;\n /**\n * The path to the runner JavaScript file, relative to the app bundle.\n *\n * @since 1.0.0\n */\n src: string;\n /**\n * The name of the event that will be called when the OS executes the background task.\n *\n * @since 1.0.0\n */\n event: string;\n /**\n * If background task should repeat based on the interval set in `interval`.\n *\n * @since 1.0.0\n */\n repeat: boolean;\n /**\n * The number of minutes after the
|
|
1
|
+
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAAA,wCAAwC","sourcesContent":["/// <reference types=\"@capacitor/cli\" />\n\nimport type { PermissionState, PluginListenerHandle } from '@capacitor/core';\n\nexport interface BackgroundRunnerConfig {\n /**\n * The name of the runner, used in logs.\n *\n * @since 1.0.0\n */\n label: string;\n /**\n * The path to the runner JavaScript file, relative to the app bundle.\n *\n * @since 1.0.0\n */\n src: string;\n /**\n * The name of the event that will be called when the OS executes the background task.\n *\n * @since 1.0.0\n */\n event: string;\n /**\n * If background task should repeat based on the interval set in `interval`.\n *\n * @since 1.0.0\n */\n repeat: boolean;\n /**\n * The number of minutes after the app is put into the background in which the background task should begin.\n *\n * If `repeat` is true, this also specifies the number of minutes between each execution.\n *\n * @since 1.0.0\n */\n interval: number;\n /**\n * Automatically register and schedule background task on app load.\n *\n * @since 1.0.0\n */\n autoStart?: boolean;\n}\n\ndeclare module '@capacitor/cli' {\n export interface PluginsConfig {\n /**\n * On load, Background Runner will automatically register a\n * background task that will be scheduled and run once your app is\n * backgrounded.\n */\n BackgroundRunner?: {\n /**\n * The name of the runner, used in logs.\n *\n * @since 1.0.0\n * @example \"com.example.background.task\"\n */\n label: string;\n /**\n * The path to the runner JavaScript file, relative to the app bundle.\n *\n * @since 1.0.0\n * @example \"runners/background.js\"\n */\n src: string;\n /**\n * The name of the event that will be called when the OS executes the background task.\n *\n * @since 1.0.0\n * @example \"myCustomEvent\"\n */\n event: string;\n /**\n * If background task should repeat based on the interval set in `interval`.\n *\n * @since 1.0.0\n * @example true\n */\n repeat: boolean;\n /**\n * The number of minutes after the app is put into the background in which the background task should begin.\n *\n * If `repeat` is true, this also specifies the number of minutes between each execution.\n *\n * @since 1.0.0\n * @example 15\n */\n interval: number;\n /**\n * Automatically register and schedule background task on app load.\n *\n * @since 1.0.0\n * @example true\n */\n autoStart?: boolean;\n };\n }\n}\n\nexport type API = 'geolocation' | 'notifications';\n\nexport interface PermissionStatus {\n geolocation: PermissionState;\n notifications: PermissionState;\n}\n\nexport interface DispatchEventOptions {\n /**\n * The runner label to dispatch the event to\n *\n * @since 1.0.0\n */\n label: string;\n /**\n * The name of the registered event listener.\n *\n * @since 1.0.0\n */\n event: string;\n details: { [key: string]: any };\n}\n\nexport interface RequestPermissionOptions {\n apis: API[];\n}\n\nexport interface RegisterBackgroundTaskOptions {\n runner: BackgroundRunnerConfig;\n}\n\nexport interface NotificationActionEvent {\n actionTypeId: string;\n notificationId: number;\n}\n\nexport interface BackgroundRunnerPlugin {\n /**\n * Check permissions for the various Capacitor device APIs.\n *\n * @since 1.0.0\n */\n checkPermissions(): Promise<PermissionStatus>;\n /**\n * Request permission to display local notifications.\n *\n * @since 1.0.0\n */\n requestPermissions(options: RequestPermissionOptions): Promise<PermissionStatus>;\n /**\n * Dispatches an event to the configured runner.\n * @param options\n * @since 1.0.0\n */\n dispatchEvent<T = void>(options: DispatchEventOptions): Promise<T>;\n /**\n * Add a listener for notification actions.\n *\n * @since 2.1.1\n */\n addListener(\n eventName: 'backgroundRunnerNotificationReceived',\n listenerFunc: (event: NotificationActionEvent) => void,\n ): Promise<PluginListenerHandle>;\n /**\n * Remove notification action listeners for this plugin.\n *\n * @since 2.1.1\n */\n removeNotificationListeners(): Promise<void>;\n}\n\ndeclare module '@capacitor/cli' {\n export interface PluginsConfig {\n BackgroundRunner?: BackgroundRunnerConfig;\n }\n}\n"]}
|
|
@@ -1,281 +1,308 @@
|
|
|
1
|
-
import Foundation
|
|
2
1
|
import Capacitor
|
|
3
|
-
import
|
|
2
|
+
import Foundation
|
|
4
3
|
import UserNotifications
|
|
4
|
+
import WatchConnectivity
|
|
5
5
|
|
|
6
6
|
@objc(BackgroundRunnerPlugin)
|
|
7
7
|
public class BackgroundRunnerPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
8
|
+
public let identifier = "CapacitorBackgroundRunnerPlugin"
|
|
9
|
+
public let jsName = "CapacitorBackgroundRunner"
|
|
10
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
11
|
+
CAPPluginMethod(name: "checkPermissions", returnType: CAPPluginReturnPromise),
|
|
12
|
+
CAPPluginMethod(name: "requestPermissions", returnType: CAPPluginReturnPromise),
|
|
13
|
+
CAPPluginMethod(name: "dispatchEvent", returnType: CAPPluginReturnPromise),
|
|
14
|
+
CAPPluginMethod(name: "registerBackgroundTask", returnType: CAPPluginReturnPromise),
|
|
15
|
+
CAPPluginMethod(name: "removeNotificationListeners", returnType: CAPPluginReturnPromise),
|
|
16
|
+
]
|
|
17
|
+
private let impl = BackgroundRunner()
|
|
18
|
+
private weak var originalDelegate: UNUserNotificationCenterDelegate?
|
|
19
|
+
|
|
20
|
+
override public func load() {
|
|
21
|
+
NotificationCenter.default.addObserver(
|
|
22
|
+
self,
|
|
23
|
+
selector: #selector(didEnterBackground),
|
|
24
|
+
name: UIApplication.didEnterBackgroundNotification,
|
|
25
|
+
object: nil
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
// Register as notification delegate
|
|
29
|
+
registerNotificationCategories()
|
|
30
|
+
|
|
31
|
+
initWatchConnectivity()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@objc override public func checkPermissions(_ call: CAPPluginCall) {
|
|
35
|
+
// check geolocation permissions
|
|
36
|
+
let geolocationState = CapacitorGeolocation.checkPermission()
|
|
37
|
+
|
|
38
|
+
// check notification permissions
|
|
39
|
+
let notificationState = CapacitorNotifications.checkPermission()
|
|
40
|
+
|
|
41
|
+
call.resolve([
|
|
42
|
+
"geolocation": geolocationState,
|
|
43
|
+
"notifications": notificationState,
|
|
44
|
+
])
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@objc override public func requestPermissions(_ call: CAPPluginCall) {
|
|
48
|
+
Task {
|
|
49
|
+
let permissions = call.getArray("apis") as? [String] ?? []
|
|
50
|
+
|
|
51
|
+
do {
|
|
52
|
+
if permissions.contains("notifications") {
|
|
53
|
+
try CapacitorNotifications.requestPermission()
|
|
54
|
+
}
|
|
27
55
|
|
|
28
|
-
|
|
29
|
-
|
|
56
|
+
if permissions.contains("geolocation") {
|
|
57
|
+
let geolocation = CapacitorGeolocation()
|
|
58
|
+
print("geolocation requested...")
|
|
59
|
+
try await geolocation.requestPermission()
|
|
60
|
+
print("geolocation requested...done")
|
|
61
|
+
}
|
|
30
62
|
|
|
31
|
-
|
|
63
|
+
self.checkPermissions(call)
|
|
64
|
+
} catch {
|
|
65
|
+
call.reject("\(error)")
|
|
66
|
+
}
|
|
32
67
|
}
|
|
68
|
+
}
|
|
33
69
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
70
|
+
@objc func dispatchEvent(_ call: CAPPluginCall) {
|
|
71
|
+
do {
|
|
72
|
+
guard let runnerEvent = call.getString("event"), !runnerEvent.isEmpty else {
|
|
73
|
+
throw BackgroundRunnerPluginError.invalidArgument(reason: "event is missing or invalid")
|
|
74
|
+
}
|
|
37
75
|
|
|
38
|
-
|
|
39
|
-
let notificationState = CapacitorNotifications.checkPermission()
|
|
76
|
+
let details = call.getObject("details", JSObject())
|
|
40
77
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
])
|
|
45
|
-
}
|
|
78
|
+
guard var config = impl.config else {
|
|
79
|
+
throw BackgroundRunnerPluginError.noRunnerConfig
|
|
80
|
+
}
|
|
46
81
|
|
|
47
|
-
|
|
48
|
-
Task {
|
|
49
|
-
let permissions = call.getArray("apis") as? [String] ?? []
|
|
50
|
-
|
|
51
|
-
do {
|
|
52
|
-
if permissions.contains("notifications") {
|
|
53
|
-
try CapacitorNotifications.requestPermission()
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if permissions.contains("geolocation") {
|
|
57
|
-
let geolocation = CapacitorGeolocation()
|
|
58
|
-
print("geolocation requested...")
|
|
59
|
-
try await geolocation.requestPermission()
|
|
60
|
-
print("geolocation requested...done")
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
self.checkPermissions(call)
|
|
64
|
-
} catch {
|
|
65
|
-
call.reject("\(error)")
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
82
|
+
config.event = runnerEvent
|
|
69
83
|
|
|
70
|
-
|
|
84
|
+
// swiftlint:disable:next unowned_variable_capture
|
|
85
|
+
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
|
71
86
|
do {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// swiftlint:disable:next unowned_variable_capture
|
|
85
|
-
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
|
86
|
-
do {
|
|
87
|
-
let result = try self.impl.execute(
|
|
88
|
-
config: config,
|
|
89
|
-
inputArgs: details as [String: Any],
|
|
90
|
-
callbackId: call.callbackId
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
if let result = result {
|
|
94
|
-
call.resolve(result)
|
|
95
|
-
return
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
call.resolve()
|
|
99
|
-
} catch {
|
|
100
|
-
call.reject("\(error)")
|
|
101
|
-
}
|
|
102
|
-
}
|
|
87
|
+
let result = try self.impl.execute(
|
|
88
|
+
config: config,
|
|
89
|
+
inputArgs: details as [String: Any],
|
|
90
|
+
callbackId: call.callbackId
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
if let result = result {
|
|
94
|
+
call.resolve(result)
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
call.resolve()
|
|
103
99
|
} catch {
|
|
104
|
-
|
|
100
|
+
call.reject("\(error)")
|
|
105
101
|
}
|
|
102
|
+
}
|
|
103
|
+
} catch {
|
|
104
|
+
call.reject("\(error)")
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@objc func registerBackgroundTask(_ call: CAPPluginCall) {
|
|
109
|
+
call.resolve()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@objc func removeNotificationListeners(_ call: CAPPluginCall) {
|
|
113
|
+
notifyListeners("backgroundRunnerNotificationReceived", data: nil)
|
|
114
|
+
call.resolve()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@objc private func didEnterBackground() {
|
|
118
|
+
do {
|
|
119
|
+
try impl.scheduleBackgroundTasks()
|
|
120
|
+
} catch {
|
|
121
|
+
print("could not schedule background task: \(error)")
|
|
106
122
|
}
|
|
123
|
+
}
|
|
107
124
|
|
|
108
|
-
|
|
109
|
-
|
|
125
|
+
public static func registerBackgroundTask() {
|
|
126
|
+
do {
|
|
127
|
+
try BackgroundRunner.shared.registerBackgroundTask()
|
|
128
|
+
} catch {
|
|
129
|
+
print("could not register background task: \(error)")
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public static func dispatchEvent(
|
|
134
|
+
event: String, eventArgs: [AnyHashable: Any], completionHandler: ((Result<Bool, Error>) -> Void)
|
|
135
|
+
) {
|
|
136
|
+
if event.isEmpty {
|
|
137
|
+
completionHandler(
|
|
138
|
+
.failure(BackgroundRunnerPluginError.invalidArgument(reason: "event is missing or invalid"))
|
|
139
|
+
)
|
|
140
|
+
return
|
|
110
141
|
}
|
|
111
142
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
143
|
+
var args: [String: Any] = [:]
|
|
144
|
+
|
|
145
|
+
eventArgs.forEach { (key: AnyHashable, value: Any) in
|
|
146
|
+
if let strKey = key as? String {
|
|
147
|
+
args[strKey] = value
|
|
148
|
+
}
|
|
115
149
|
}
|
|
116
150
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
151
|
+
do {
|
|
152
|
+
try BackgroundRunner.shared.dispatchEvent(event: event, inputArgs: args)
|
|
153
|
+
completionHandler(.success(true))
|
|
154
|
+
} catch {
|
|
155
|
+
completionHandler(.failure(error))
|
|
123
156
|
}
|
|
157
|
+
}
|
|
124
158
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
159
|
+
public static func handleApplicationDidFinishLaunching(
|
|
160
|
+
launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
|
161
|
+
) {
|
|
162
|
+
guard let launchOptions = launchOptions else {
|
|
163
|
+
return
|
|
131
164
|
}
|
|
132
165
|
|
|
133
|
-
|
|
134
|
-
if event.isEmpty {
|
|
135
|
-
completionHandler(.failure(BackgroundRunnerPluginError.invalidArgument(reason: "event is missing or invalid")))
|
|
136
|
-
return
|
|
137
|
-
}
|
|
166
|
+
var events: [String: [String: Any]?] = [:]
|
|
138
167
|
|
|
139
|
-
|
|
168
|
+
if launchOptions[.location] != nil {
|
|
169
|
+
// application was launched for a location event
|
|
170
|
+
var details: [String: Any] = [:]
|
|
171
|
+
details["locations"] = nil
|
|
140
172
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
173
|
+
if let location = CapacitorGeolocation.getCurrentKnownLocation() {
|
|
174
|
+
var details: [String: Any] = [:]
|
|
175
|
+
details["locations"] = [location]
|
|
176
|
+
}
|
|
146
177
|
|
|
147
|
-
|
|
148
|
-
try BackgroundRunner.shared.dispatchEvent(event: event, inputArgs: args)
|
|
149
|
-
completionHandler(.success(true))
|
|
150
|
-
} catch {
|
|
151
|
-
completionHandler(.failure(error))
|
|
152
|
-
}
|
|
178
|
+
events["currentLocation"] = details
|
|
153
179
|
}
|
|
154
180
|
|
|
155
|
-
|
|
156
|
-
guard let launchOptions = launchOptions else {
|
|
157
|
-
return
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
var events: [String: [String: Any]?] = [:]
|
|
161
|
-
|
|
162
|
-
if launchOptions[.location] != nil {
|
|
163
|
-
// application was launched for a location event
|
|
164
|
-
var details: [String: Any] = [:]
|
|
165
|
-
details["locations"] = nil
|
|
181
|
+
print("emit events: \(events)")
|
|
166
182
|
|
|
167
|
-
|
|
168
|
-
var details: [String: Any] = [:]
|
|
169
|
-
details["locations"] = [location]
|
|
170
|
-
}
|
|
183
|
+
}
|
|
171
184
|
|
|
172
|
-
|
|
173
|
-
|
|
185
|
+
private func initWatchConnectivity() {
|
|
186
|
+
if !WCSession.isSupported() {
|
|
187
|
+
return
|
|
188
|
+
}
|
|
174
189
|
|
|
175
|
-
|
|
190
|
+
guard let config = impl.config else {
|
|
191
|
+
return
|
|
192
|
+
}
|
|
176
193
|
|
|
194
|
+
if !config.enableWatchConnectivity {
|
|
195
|
+
return
|
|
177
196
|
}
|
|
178
197
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
return
|
|
182
|
-
}
|
|
198
|
+
WCSession.default.delegate = self
|
|
199
|
+
WCSession.default.activate()
|
|
183
200
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if !config.enableWatchConnectivity {
|
|
189
|
-
return
|
|
190
|
-
}
|
|
201
|
+
print("Watch Connectivity Enabled")
|
|
202
|
+
}
|
|
191
203
|
|
|
192
|
-
|
|
193
|
-
|
|
204
|
+
@objc func registerNotificationCategories() {
|
|
205
|
+
let notificationCenter = UNUserNotificationCenter.current()
|
|
194
206
|
|
|
195
|
-
|
|
207
|
+
// Capture the original delegate if it's not self
|
|
208
|
+
if notificationCenter.delegate !== self {
|
|
209
|
+
originalDelegate = notificationCenter.delegate
|
|
196
210
|
}
|
|
197
211
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
// Capture the original delegate if it's not self
|
|
202
|
-
if notificationCenter.delegate !== self {
|
|
203
|
-
originalDelegate = notificationCenter.delegate
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// Set the plugin as the notification delegate
|
|
207
|
-
notificationCenter.delegate = self
|
|
208
|
-
}
|
|
212
|
+
// Set the plugin as the notification delegate
|
|
213
|
+
notificationCenter.delegate = self
|
|
214
|
+
}
|
|
209
215
|
}
|
|
210
216
|
|
|
211
217
|
// MARK: - UNUserNotificationCenterDelegate
|
|
212
218
|
extension BackgroundRunnerPlugin: UNUserNotificationCenterDelegate {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
219
|
+
// This method will be called when a notification is tapped
|
|
220
|
+
public func userNotificationCenter(
|
|
221
|
+
_ center: UNUserNotificationCenter,
|
|
222
|
+
didReceive response: UNNotificationResponse,
|
|
223
|
+
withCompletionHandler completionHandler: @escaping () -> Void
|
|
224
|
+
) {
|
|
225
|
+
let userInfo = response.notification.request.content.userInfo
|
|
226
|
+
|
|
227
|
+
if isBackgroundRunnerNotification(userInfo) {
|
|
228
|
+
handleBackgroundRunnerNotification(response, completionHandler: completionHandler)
|
|
229
|
+
} else {
|
|
230
|
+
forwardToOriginalDelegate(
|
|
231
|
+
center, didReceive: response, withCompletionHandler: completionHandler)
|
|
224
232
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// This is needed to present notifications when the app is in the foreground
|
|
236
|
+
public func userNotificationCenter(
|
|
237
|
+
_ center: UNUserNotificationCenter,
|
|
238
|
+
willPresent notification: UNNotification,
|
|
239
|
+
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
|
|
240
|
+
) {
|
|
241
|
+
let userInfo = notification.request.content.userInfo
|
|
242
|
+
|
|
243
|
+
if isBackgroundRunnerNotification(userInfo) {
|
|
244
|
+
handleBackgroundRunnerPresentation(completionHandler: completionHandler)
|
|
245
|
+
} else {
|
|
246
|
+
forwardToOriginalDelegate(
|
|
247
|
+
center, willPresent: notification, withCompletionHandler: completionHandler)
|
|
237
248
|
}
|
|
249
|
+
}
|
|
238
250
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
private func isBackgroundRunnerNotification(_ userInfo: [AnyHashable: Any]) -> Bool {
|
|
242
|
-
return (userInfo["source"] as? String) == "capacitor-background-runner"
|
|
243
|
-
}
|
|
251
|
+
// MARK: - Helper Methods
|
|
244
252
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
253
|
+
private func isBackgroundRunnerNotification(_ userInfo: [AnyHashable: Any]) -> Bool {
|
|
254
|
+
return (userInfo["source"] as? String) == "capacitor-background-runner"
|
|
255
|
+
}
|
|
248
256
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
257
|
+
private func handleBackgroundRunnerNotification(
|
|
258
|
+
_ response: UNNotificationResponse, completionHandler: @escaping () -> Void
|
|
259
|
+
) {
|
|
260
|
+
let actionTypeId = response.notification.request.content.categoryIdentifier
|
|
261
|
+
let notificationId = Int(response.notification.request.identifier) ?? -1
|
|
253
262
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
263
|
+
let eventData: [String: Any] = [
|
|
264
|
+
"actionTypeId": actionTypeId,
|
|
265
|
+
"notificationId": notificationId,
|
|
266
|
+
]
|
|
257
267
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
268
|
+
notifyListeners(
|
|
269
|
+
"backgroundRunnerNotificationReceived", data: eventData, retainUntilConsumed: true)
|
|
270
|
+
completionHandler()
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private func forwardToOriginalDelegate(
|
|
274
|
+
_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
|
|
275
|
+
withCompletionHandler completionHandler: @escaping () -> Void
|
|
276
|
+
) {
|
|
277
|
+
if let originalDelegate = originalDelegate,
|
|
278
|
+
originalDelegate.responds(
|
|
279
|
+
to: #selector(userNotificationCenter(_:didReceive:withCompletionHandler:)))
|
|
280
|
+
{
|
|
281
|
+
originalDelegate.userNotificationCenter?(
|
|
282
|
+
center, didReceive: response, withCompletionHandler: completionHandler)
|
|
283
|
+
} else {
|
|
284
|
+
completionHandler()
|
|
264
285
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private func handleBackgroundRunnerPresentation(
|
|
289
|
+
completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
|
|
290
|
+
) {
|
|
291
|
+
completionHandler([.banner, .sound, .badge])
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private func forwardToOriginalDelegate(
|
|
295
|
+
_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
|
|
296
|
+
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
|
|
297
|
+
) {
|
|
298
|
+
if let originalDelegate = originalDelegate,
|
|
299
|
+
originalDelegate.responds(
|
|
300
|
+
to: #selector(userNotificationCenter(_:willPresent:withCompletionHandler:)))
|
|
301
|
+
{
|
|
302
|
+
originalDelegate.userNotificationCenter?(
|
|
303
|
+
center, willPresent: notification, withCompletionHandler: completionHandler)
|
|
304
|
+
} else {
|
|
305
|
+
handleBackgroundRunnerPresentation(completionHandler: completionHandler)
|
|
280
306
|
}
|
|
307
|
+
}
|
|
281
308
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capacitor/background-runner",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-dev.28jul2026",
|
|
4
4
|
"description": "Capacitor Background Runner",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
],
|
|
33
33
|
"scripts": {
|
|
34
34
|
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
|
|
35
|
-
"verify:ios": "xcodebuild -scheme CapacitorBackgroundRunner -destination generic/platform=iOS && xcodebuild test -scheme CapacitorBackgroundRunner -destination 'platform=iOS Simulator,OS=
|
|
35
|
+
"verify:ios": "xcodebuild -scheme CapacitorBackgroundRunner -destination generic/platform=iOS && xcodebuild test -scheme CapacitorBackgroundRunner -destination 'platform=iOS Simulator,OS=26.0.1,name=iPhone 17 Pro'",
|
|
36
36
|
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
|
37
37
|
"verify:web": "npm run build",
|
|
38
38
|
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"docgen": "docgen --api BackgroundRunnerPlugin --output-readme README.md --output-json dist/docs.json",
|
|
44
44
|
"docgen-api": "docgen --api CapacitorAPI --output-readme API.md --output-json dist/docs-api.json && node ./scripts/combine_docs.js",
|
|
45
45
|
"build-sdk": "./scripts/build-native-sdks.sh",
|
|
46
|
-
"build": "npm run clean && npm run docgen && npm run docgen-api && tsc && rollup -c rollup.config.
|
|
46
|
+
"build": "npm run clean && npm run docgen && npm run docgen-api && tsc && rollup -c rollup.config.mjs --bundleConfigAsCjs && npm run build-sdk && ./scripts/copy-native-sdks.sh && npm pack && mv ./capacitor-background-runner-*.tgz ../../ && ./scripts/copy-readme.sh",
|
|
47
47
|
"pack": "npm run build && ./scripts/copy-native-sdks.sh && npm pack && mv ./capacitor-background-runner-*.tgz ../../",
|
|
48
48
|
"clean": "rimraf ./dist",
|
|
49
49
|
"watch": "tsc --watch",
|
|
@@ -51,24 +51,24 @@
|
|
|
51
51
|
"postinstall": "node ./scripts/install_libs.js"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@capacitor/android": "
|
|
55
|
-
"@capacitor/cli": "
|
|
56
|
-
"@capacitor/core": "
|
|
57
|
-
"@capacitor/docgen": "0.3.
|
|
58
|
-
"@capacitor/ios": "
|
|
54
|
+
"@capacitor/android": "next",
|
|
55
|
+
"@capacitor/cli": "next",
|
|
56
|
+
"@capacitor/core": "next",
|
|
57
|
+
"@capacitor/docgen": "^0.3.1",
|
|
58
|
+
"@capacitor/ios": "next",
|
|
59
59
|
"@ionic/eslint-config": "^0.4.0",
|
|
60
60
|
"@ionic/prettier-config": "^4.0.0",
|
|
61
61
|
"@ionic/swiftlint-config": "^2.0.0",
|
|
62
62
|
"eslint": "^8.57.1",
|
|
63
63
|
"prettier": "^3.6.2",
|
|
64
64
|
"prettier-plugin-java": "^2.7.7",
|
|
65
|
-
"rimraf": "^6.1.
|
|
66
|
-
"rollup": "^4.53.
|
|
65
|
+
"rimraf": "^6.1.0",
|
|
66
|
+
"rollup": "^4.53.2",
|
|
67
67
|
"swiftlint": "^2.0.0",
|
|
68
68
|
"typescript": "^5.9.3"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
|
-
"@capacitor/core": ">=
|
|
71
|
+
"@capacitor/core": ">=9.0.0-alpha.5"
|
|
72
72
|
},
|
|
73
73
|
"prettier": "@ionic/prettier-config",
|
|
74
74
|
"swiftlint": "@ionic/swiftlint-config",
|
|
@@ -82,6 +82,5 @@
|
|
|
82
82
|
"android": {
|
|
83
83
|
"src": "android"
|
|
84
84
|
}
|
|
85
|
-
}
|
|
86
|
-
"gitHead": "894e33fd34e7906e17d03a62602f3c5ab4101100"
|
|
85
|
+
}
|
|
87
86
|
}
|