@capacitor/background-runner 2.2.0 → 3.0.0
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 +2 -2
- package/Package.swift +45 -0
- package/README.md +12 -2
- package/android/build.gradle +12 -12
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/BackgroundRunnerPlugin.kt +1 -1
- 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/esm/definitions.js.map +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +2 -4
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +2 -4
- package/dist/plugin.js.map +1 -1
- package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/BackgroundRunner.swift +26 -11
- package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/CapacitorAPI/Geolocation.swift +21 -9
- package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/CapacitorAPI/Notifications.swift +2 -1
- package/ios/Sources/CapacitorBackgroundRunner/CapacitorBackgroundRunner.swift +308 -0
- package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/Context+CapacitorAPI.swift +8 -2
- package/ios/{Plugin → Sources}/RunnerEngine/Context.swift +1 -1
- package/ios/{Plugin → Sources}/RunnerEngine/JSCrypto.swift +3 -3
- package/ios/{Plugin → Sources}/RunnerEngine/JSError.swift +1 -1
- package/ios/{Plugin → Sources}/RunnerEngine/Runner.swift +1 -1
- package/ios/Tests/CapacitorBackgroundRunnerTests/CapacitorBackgroundRunnerTests.swift +21 -0
- package/ios/Tests/RunnerEngineTests/ContextTests.swift +393 -0
- package/ios/Tests/RunnerEngineTests/RunnerTests.swift +32 -0
- package/package.json +24 -22
- package/scripts/install_libs.js +4 -12
- package/ios/Plugin/BackgroundRunnerPlugin.h +0 -10
- package/ios/Plugin/BackgroundRunnerPlugin.m +0 -12
- package/ios/Plugin/BackgroundRunnerPlugin.swift +0 -228
- package/ios/Plugin/Info.plist +0 -24
- /package/ios/{Plugin/BackgroundRunnerErrors.swift → Sources/CapacitorBackgroundRunner/BackgroundRunnerPluginError.swift} +0 -0
- /package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/CapacitorAPI/App.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/CapacitorAPI/Device.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/CapacitorAPI/KV.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/CapacitorAPI/Reachability.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/CapacitorAPI/Watch.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorBackgroundRunner}/RunnerConfig.swift +0 -0
- /package/ios/{Plugin/WatchConnectivtySessionDelegate.swift → Sources/CapacitorBackgroundRunner/WatchConnectivitySessionDelegate.swift} +0 -0
- /package/ios/{Plugin → Sources}/RunnerEngine/Errors.swift +0 -0
- /package/ios/{Plugin → Sources}/RunnerEngine/JSConsole.swift +0 -0
- /package/ios/{Plugin → Sources}/RunnerEngine/JSFetch.swift +0 -0
- /package/ios/{Plugin → Sources}/RunnerEngine/JSResponse.swift +0 -0
- /package/ios/{Plugin → Sources}/RunnerEngine/JSTextDecoder.swift +0 -0
- /package/ios/{Plugin → Sources}/RunnerEngine/JSTextEncoder.swift +0 -0
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
import Foundation
|
|
2
|
-
import Capacitor
|
|
3
|
-
import WatchConnectivity
|
|
4
|
-
import UserNotifications
|
|
5
|
-
|
|
6
|
-
@objc(BackgroundRunnerPlugin)
|
|
7
|
-
public class BackgroundRunnerPlugin: CAPPlugin {
|
|
8
|
-
private let impl = BackgroundRunner()
|
|
9
|
-
|
|
10
|
-
override public func load() {
|
|
11
|
-
NotificationCenter.default.addObserver(
|
|
12
|
-
self,
|
|
13
|
-
selector: #selector(didEnterBackground),
|
|
14
|
-
name: UIApplication.didEnterBackgroundNotification,
|
|
15
|
-
object: nil
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
// Register as notification delegate
|
|
19
|
-
registerNotificationCategories()
|
|
20
|
-
|
|
21
|
-
initWatchConnectivity()
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
@objc override public func checkPermissions(_ call: CAPPluginCall) {
|
|
25
|
-
// check geolocation permissions
|
|
26
|
-
let geolocationState = CapacitorGeolocation.checkPermission()
|
|
27
|
-
|
|
28
|
-
// check notification permissions
|
|
29
|
-
let notificationState = CapacitorNotifications.checkPermission()
|
|
30
|
-
|
|
31
|
-
call.resolve([
|
|
32
|
-
"geolocation": geolocationState,
|
|
33
|
-
"notifications": notificationState
|
|
34
|
-
])
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
@objc override public func requestPermissions(_ call: CAPPluginCall) {
|
|
38
|
-
Task {
|
|
39
|
-
let permissions = call.getArray("apis") as? [String] ?? []
|
|
40
|
-
|
|
41
|
-
do {
|
|
42
|
-
if permissions.contains("notifications") {
|
|
43
|
-
try CapacitorNotifications.requestPermission()
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if permissions.contains("geolocation") {
|
|
47
|
-
let geolocation = CapacitorGeolocation()
|
|
48
|
-
print("geolocation requested...")
|
|
49
|
-
try await geolocation.requestPermission()
|
|
50
|
-
print("geolocation requested...done")
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
self.checkPermissions(call)
|
|
54
|
-
} catch {
|
|
55
|
-
call.reject("\(error)")
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
@objc func dispatchEvent(_ call: CAPPluginCall) {
|
|
61
|
-
do {
|
|
62
|
-
guard let runnerEvent = call.getString("event"), !runnerEvent.isEmpty else {
|
|
63
|
-
throw BackgroundRunnerPluginError.invalidArgument(reason: "event is missing or invalid")
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
let details = call.getObject("details", JSObject())
|
|
67
|
-
|
|
68
|
-
guard var config = impl.config else {
|
|
69
|
-
throw BackgroundRunnerPluginError.noRunnerConfig
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
config.event = runnerEvent
|
|
73
|
-
|
|
74
|
-
// swiftlint:disable:next unowned_variable_capture
|
|
75
|
-
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
|
76
|
-
do {
|
|
77
|
-
let result = try self.impl.execute(
|
|
78
|
-
config: config,
|
|
79
|
-
inputArgs: details as [String: Any],
|
|
80
|
-
callbackId: call.callbackId
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
if let result = result {
|
|
84
|
-
call.resolve(result)
|
|
85
|
-
return
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
call.resolve()
|
|
89
|
-
} catch {
|
|
90
|
-
call.reject("\(error)")
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
} catch {
|
|
94
|
-
call.reject("\(error)")
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
@objc func registerBackgroundTask(_ call: CAPPluginCall) {
|
|
99
|
-
call.resolve()
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
@objc func removeNotificationListeners(_ call: CAPPluginCall) {
|
|
103
|
-
notifyListeners("backgroundRunnerNotificationReceived", data: nil)
|
|
104
|
-
call.resolve()
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
@objc private func didEnterBackground() {
|
|
108
|
-
do {
|
|
109
|
-
try impl.scheduleBackgroundTasks()
|
|
110
|
-
} catch {
|
|
111
|
-
print("could not schedule background task: \(error)")
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
public static func registerBackgroundTask() {
|
|
116
|
-
do {
|
|
117
|
-
try BackgroundRunner.shared.registerBackgroundTask()
|
|
118
|
-
} catch {
|
|
119
|
-
print("could not register background task: \(error)")
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
public static func dispatchEvent(event: String, eventArgs: [AnyHashable: Any], completionHandler: ((Result<Bool, Error>) -> Void)) {
|
|
124
|
-
if event.isEmpty {
|
|
125
|
-
completionHandler(.failure(BackgroundRunnerPluginError.invalidArgument(reason: "event is missing or invalid")))
|
|
126
|
-
return
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
var args: [String: Any] = [:]
|
|
130
|
-
|
|
131
|
-
eventArgs.forEach { (key: AnyHashable, value: Any) in
|
|
132
|
-
if let strKey = key as? String {
|
|
133
|
-
args[strKey] = value
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
do {
|
|
138
|
-
try BackgroundRunner.shared.dispatchEvent(event: event, inputArgs: args)
|
|
139
|
-
completionHandler(.success(true))
|
|
140
|
-
} catch {
|
|
141
|
-
completionHandler(.failure(error))
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
public static func handleApplicationDidFinishLaunching(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
|
|
146
|
-
guard let launchOptions = launchOptions else {
|
|
147
|
-
return
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
var events: [String: [String: Any]?] = [:]
|
|
151
|
-
|
|
152
|
-
if launchOptions[.location] != nil {
|
|
153
|
-
// application was launched for a location event
|
|
154
|
-
var details: [String: Any] = [:]
|
|
155
|
-
details["locations"] = nil
|
|
156
|
-
|
|
157
|
-
if let location = CapacitorGeolocation.getCurrentKnownLocation() {
|
|
158
|
-
var details: [String: Any] = [:]
|
|
159
|
-
details["locations"] = [location]
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
events["currentLocation"] = details
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
print("emit events: \(events)")
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
private func initWatchConnectivity() {
|
|
170
|
-
if !WCSession.isSupported() {
|
|
171
|
-
return
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
guard let config = impl.config else {
|
|
175
|
-
return
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if !config.enableWatchConnectivity {
|
|
179
|
-
return
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
WCSession.default.delegate = self
|
|
183
|
-
WCSession.default.activate()
|
|
184
|
-
|
|
185
|
-
print("Watch Connectivity Enabled")
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
@objc func registerNotificationCategories() {
|
|
189
|
-
let notificationCenter = UNUserNotificationCenter.current()
|
|
190
|
-
|
|
191
|
-
// Set the plugin as the notification delegate
|
|
192
|
-
notificationCenter.delegate = self
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// MARK: - UNUserNotificationCenterDelegate
|
|
197
|
-
extension BackgroundRunnerPlugin: UNUserNotificationCenterDelegate {
|
|
198
|
-
// This method will be called when a notification is tapped
|
|
199
|
-
public func userNotificationCenter(_ center: UNUserNotificationCenter,
|
|
200
|
-
didReceive response: UNNotificationResponse,
|
|
201
|
-
withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
202
|
-
let actionTypeId = response.notification.request.content.categoryIdentifier
|
|
203
|
-
let notificationId = Int(response.notification.request.identifier) ?? -1
|
|
204
|
-
|
|
205
|
-
// Create event data
|
|
206
|
-
let eventData: [String: Any] = [
|
|
207
|
-
"actionTypeId": actionTypeId,
|
|
208
|
-
"notificationId": notificationId
|
|
209
|
-
]
|
|
210
|
-
|
|
211
|
-
// Notify listeners with the action data - the 'true' parameter is for retaining the event
|
|
212
|
-
notifyListeners("backgroundRunnerNotificationReceived", data: eventData, retainUntilConsumed: true)
|
|
213
|
-
|
|
214
|
-
completionHandler()
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// This is needed to present notifications when the app is in the foreground
|
|
218
|
-
public func userNotificationCenter(_ center: UNUserNotificationCenter,
|
|
219
|
-
willPresent notification: UNNotification,
|
|
220
|
-
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
|
221
|
-
// Show the notification even when the app is in foreground
|
|
222
|
-
if #available(iOS 14.0, *) {
|
|
223
|
-
completionHandler([.banner, .sound, .badge])
|
|
224
|
-
} else {
|
|
225
|
-
completionHandler([.alert, .sound, .badge])
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
package/ios/Plugin/Info.plist
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
-
<plist version="1.0">
|
|
4
|
-
<dict>
|
|
5
|
-
<key>CFBundleDevelopmentRegion</key>
|
|
6
|
-
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
|
7
|
-
<key>CFBundleExecutable</key>
|
|
8
|
-
<string>$(EXECUTABLE_NAME)</string>
|
|
9
|
-
<key>CFBundleIdentifier</key>
|
|
10
|
-
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
|
11
|
-
<key>CFBundleInfoDictionaryVersion</key>
|
|
12
|
-
<string>6.0</string>
|
|
13
|
-
<key>CFBundleName</key>
|
|
14
|
-
<string>$(PRODUCT_NAME)</string>
|
|
15
|
-
<key>CFBundlePackageType</key>
|
|
16
|
-
<string>FMWK</string>
|
|
17
|
-
<key>CFBundleShortVersionString</key>
|
|
18
|
-
<string>1.0</string>
|
|
19
|
-
<key>CFBundleVersion</key>
|
|
20
|
-
<string>$(CURRENT_PROJECT_VERSION)</string>
|
|
21
|
-
<key>NSPrincipalClass</key>
|
|
22
|
-
<string></string>
|
|
23
|
-
</dict>
|
|
24
|
-
</plist>
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|