native-update-capacitor 0.0.0-stage → 4.3.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/AI-INTEGRATION-GUIDE.md +578 -0
- package/LICENSE +25 -0
- package/NativeUpdate.podspec +19 -0
- package/README.md +574 -2
- package/android/build.gradle +125 -0
- package/android/consumer-rules.pro +9 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/android/gradle.properties +17 -0
- package/android/manifest-additions.xml +28 -0
- package/android/proguard-rules.pro +26 -0
- package/android/settings.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +13 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppReviewPlugin.kt +159 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppUpdatePlugin.kt +314 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +406 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +48 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +343 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +236 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/JsonOptional.kt +26 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +1249 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +404 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NotificationActionReceiver.kt +107 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +240 -0
- package/android/variables.gradle +5 -0
- package/cli/commands/backend-create.js +623 -0
- package/cli/commands/bundle-create.js +145 -0
- package/cli/commands/bundle-sign.js +64 -0
- package/cli/commands/bundle-verify.js +58 -0
- package/cli/commands/init.js +146 -0
- package/cli/commands/keys-generate.js +58 -0
- package/cli/commands/monitor.js +89 -0
- package/cli/commands/remote-shared.js +262 -0
- package/cli/commands/remote.js +622 -0
- package/cli/commands/server-start.js +96 -0
- package/cli/index.js +605 -0
- package/cli/package.json +21 -0
- package/dist/esm/app-review/app-review-manager.d.ts +24 -0
- package/dist/esm/app-review/app-review-manager.js +194 -0
- package/dist/esm/app-review/index.d.ts +5 -0
- package/dist/esm/app-review/index.js +5 -0
- package/dist/esm/app-review/platform-review-handler.d.ts +20 -0
- package/dist/esm/app-review/platform-review-handler.js +137 -0
- package/dist/esm/app-review/review-conditions-checker.d.ts +22 -0
- package/dist/esm/app-review/review-conditions-checker.js +154 -0
- package/dist/esm/app-review/review-rate-limiter.d.ts +23 -0
- package/dist/esm/app-review/review-rate-limiter.js +163 -0
- package/dist/esm/app-review/types.d.ts +41 -0
- package/dist/esm/app-review/types.js +1 -0
- package/dist/esm/app-update/app-update-checker.d.ts +58 -0
- package/dist/esm/app-update/app-update-checker.js +227 -0
- package/dist/esm/app-update/app-update-installer.d.ts +20 -0
- package/dist/esm/app-update/app-update-installer.js +134 -0
- package/dist/esm/app-update/app-update-manager.d.ts +28 -0
- package/dist/esm/app-update/app-update-manager.js +198 -0
- package/dist/esm/app-update/app-update-notifier.d.ts +14 -0
- package/dist/esm/app-update/app-update-notifier.js +99 -0
- package/dist/esm/app-update/index.d.ts +6 -0
- package/dist/esm/app-update/index.js +6 -0
- package/dist/esm/app-update/platform-app-update.d.ts +19 -0
- package/dist/esm/app-update/platform-app-update.js +138 -0
- package/dist/esm/app-update/types.d.ts +58 -0
- package/dist/esm/app-update/types.js +11 -0
- package/dist/esm/background-update/background-scheduler.d.ts +63 -0
- package/dist/esm/background-update/background-scheduler.js +332 -0
- package/dist/esm/background-update/index.d.ts +3 -0
- package/dist/esm/background-update/index.js +2 -0
- package/dist/esm/background-update/notification-manager.d.ts +29 -0
- package/dist/esm/background-update/notification-manager.js +88 -0
- package/dist/esm/config/support.d.ts +9 -0
- package/dist/esm/config/support.js +9 -0
- package/dist/esm/core/analytics.d.ts +83 -0
- package/dist/esm/core/analytics.js +151 -0
- package/dist/esm/core/cache-manager.d.ts +72 -0
- package/dist/esm/core/cache-manager.js +274 -0
- package/dist/esm/core/config.d.ts +69 -0
- package/dist/esm/core/config.js +107 -0
- package/dist/esm/core/errors.d.ts +56 -0
- package/dist/esm/core/errors.js +85 -0
- package/dist/esm/core/event-emitter.d.ts +30 -0
- package/dist/esm/core/event-emitter.js +80 -0
- package/dist/esm/core/logger.d.ts +21 -0
- package/dist/esm/core/logger.js +108 -0
- package/dist/esm/core/performance.d.ts +53 -0
- package/dist/esm/core/performance.js +153 -0
- package/dist/esm/core/plugin-manager.d.ts +88 -0
- package/dist/esm/core/plugin-manager.js +224 -0
- package/dist/esm/core/security.d.ts +123 -0
- package/dist/esm/core/security.js +361 -0
- package/dist/esm/core/update-check.d.ts +121 -0
- package/dist/esm/core/update-check.js +127 -0
- package/dist/esm/definitions.d.ts +872 -0
- package/dist/esm/definitions.js +104 -0
- package/dist/esm/index.d.ts +14 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/live-update/bundle-manager.d.ts +102 -0
- package/dist/esm/live-update/bundle-manager.js +344 -0
- package/dist/esm/live-update/certificate-pinning.d.ts +38 -0
- package/dist/esm/live-update/certificate-pinning.js +77 -0
- package/dist/esm/live-update/delta-processor.d.ts +84 -0
- package/dist/esm/live-update/delta-processor.js +257 -0
- package/dist/esm/live-update/download-manager.d.ts +106 -0
- package/dist/esm/live-update/download-manager.js +412 -0
- package/dist/esm/live-update/rollout-checker.d.ts +86 -0
- package/dist/esm/live-update/rollout-checker.js +304 -0
- package/dist/esm/live-update/update-manager.d.ts +63 -0
- package/dist/esm/live-update/update-manager.js +321 -0
- package/dist/esm/live-update/version-manager.d.ts +93 -0
- package/dist/esm/live-update/version-manager.js +390 -0
- package/dist/esm/plugin.d.ts +3 -0
- package/dist/esm/plugin.js +848 -0
- package/dist/esm/security/crypto.d.ts +88 -0
- package/dist/esm/security/crypto.js +228 -0
- package/dist/esm/security/validator.d.ts +60 -0
- package/dist/esm/security/validator.js +142 -0
- package/dist/esm/types/app-identity.d.ts +36 -0
- package/dist/esm/types/app-identity.js +1 -0
- package/dist/esm/types/rollout.d.ts +91 -0
- package/dist/esm/types/rollout.js +25 -0
- package/dist/esm/web.d.ts +130 -0
- package/dist/esm/web.js +981 -0
- package/dist/plugin.cjs +1 -0
- package/dist/plugin.esm.js +1 -0
- package/dist/plugin.js +2 -0
- package/docs/CHANGELOG.md +993 -0
- package/docs/KNOWN_LIMITATIONS.md +235 -0
- package/docs/MIGRATION.md +391 -0
- package/docs/protocol/CLIENT-PROTOCOL.md +178 -0
- package/docs/server-requirements.md +211 -0
- package/ios/Plugin/AppReview/AppReviewPlugin.swift +158 -0
- package/ios/Plugin/AppUpdate/AppUpdatePlugin.swift +249 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +333 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +464 -0
- package/ios/Plugin/Info.plist +43 -0
- package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +1286 -0
- package/ios/Plugin/LiveUpdate/WebViewConfiguration.swift +45 -0
- package/ios/Plugin/NativeUpdatePlugin.m +53 -0
- package/ios/Plugin/NativeUpdatePlugin.swift +315 -0
- package/ios/Plugin/Security/SecurityManager.swift +289 -0
- package/package.json +135 -4
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
import UIKit
|
|
4
|
+
|
|
5
|
+
class AppUpdatePlugin {
|
|
6
|
+
private weak var plugin: CAPPlugin?
|
|
7
|
+
private var config: [String: Any]?
|
|
8
|
+
private let iTunesLookupURL = "https://itunes.apple.com/lookup"
|
|
9
|
+
private var eventListener: ((String, [String: Any]) -> Void)?
|
|
10
|
+
|
|
11
|
+
init(plugin: CAPPlugin) {
|
|
12
|
+
self.plugin = plugin
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
func setEventListener(_ listener: @escaping (String, [String: Any]) -> Void) {
|
|
16
|
+
self.eventListener = listener
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
func configure(_ config: [String: Any]) throws {
|
|
20
|
+
self.config = config
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func getAppUpdateInfo(_ call: CAPPluginCall) {
|
|
24
|
+
Task {
|
|
25
|
+
do {
|
|
26
|
+
let bundleId = Bundle.main.bundleIdentifier ?? ""
|
|
27
|
+
let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0.0"
|
|
28
|
+
|
|
29
|
+
// Check iTunes for app info
|
|
30
|
+
let appInfo = try await checkAppStoreVersion(bundleId: bundleId)
|
|
31
|
+
|
|
32
|
+
let storeVersion = appInfo["version"] as? String ?? "0.0.0"
|
|
33
|
+
let updateAvailable = isVersionNewer(storeVersion, than: currentVersion)
|
|
34
|
+
|
|
35
|
+
var result: [String: Any] = [
|
|
36
|
+
"updateAvailable": updateAvailable,
|
|
37
|
+
"currentVersion": currentVersion
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
if updateAvailable {
|
|
41
|
+
result["availableVersion"] = storeVersion
|
|
42
|
+
|
|
43
|
+
// Calculate staleness in days
|
|
44
|
+
if let releaseDate = appInfo["currentVersionReleaseDate"] as? String {
|
|
45
|
+
let formatter = ISO8601DateFormatter()
|
|
46
|
+
if let date = formatter.date(from: releaseDate) {
|
|
47
|
+
let days = Calendar.current.dateComponents([.day], from: date, to: Date()).day ?? 0
|
|
48
|
+
result["clientVersionStalenessDays"] = days
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Emit available event
|
|
53
|
+
var availableData: [String: Any] = [
|
|
54
|
+
"currentVersion": currentVersion,
|
|
55
|
+
"availableVersion": storeVersion
|
|
56
|
+
]
|
|
57
|
+
if let priority = result["updatePriority"] as? Int {
|
|
58
|
+
availableData["updatePriority"] = priority
|
|
59
|
+
}
|
|
60
|
+
eventListener?("appUpdateAvailable", availableData)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// iOS doesn't have the same update types as Android
|
|
64
|
+
result["immediateUpdateAllowed"] = false
|
|
65
|
+
result["flexibleUpdateAllowed"] = false
|
|
66
|
+
|
|
67
|
+
// Check minimum version requirement
|
|
68
|
+
if let minimumVersion = config?["minimumVersion"] as? String {
|
|
69
|
+
let needsUpdate = isVersionNewer(minimumVersion, than: currentVersion)
|
|
70
|
+
if needsUpdate {
|
|
71
|
+
result["updatePriority"] = 5 // High priority
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
call.resolve(result)
|
|
76
|
+
} catch {
|
|
77
|
+
call.reject("UPDATE_CHECK_FAILED", error.localizedDescription)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
func performImmediateUpdate(_ call: CAPPluginCall) {
|
|
83
|
+
// iOS doesn't support in-app updates like Android
|
|
84
|
+
// We'll open the App Store instead
|
|
85
|
+
openAppStore(call)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
func startFlexibleUpdate(_ call: CAPPluginCall) {
|
|
89
|
+
// iOS doesn't support flexible updates
|
|
90
|
+
call.reject("PLATFORM_NOT_SUPPORTED", "Flexible updates are not supported on iOS")
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
func completeFlexibleUpdate(_ call: CAPPluginCall) {
|
|
94
|
+
// iOS doesn't support flexible updates
|
|
95
|
+
call.reject("PLATFORM_NOT_SUPPORTED", "Flexible updates are not supported on iOS")
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
func openAppStore(_ call: CAPPluginCall) {
|
|
99
|
+
let appId = call.getString("appId") ?? getAppStoreId()
|
|
100
|
+
|
|
101
|
+
guard let appId = appId else {
|
|
102
|
+
call.reject("APP_ID_REQUIRED", "App ID is required to open App Store")
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let urlString = "itms-apps://apple.com/app/id\(appId)"
|
|
107
|
+
|
|
108
|
+
DispatchQueue.main.async {
|
|
109
|
+
if let url = URL(string: urlString),
|
|
110
|
+
UIApplication.shared.canOpenURL(url) {
|
|
111
|
+
UIApplication.shared.open(url, options: [:]) { success in
|
|
112
|
+
if success {
|
|
113
|
+
call.resolve()
|
|
114
|
+
} else {
|
|
115
|
+
// Fallback to web URL
|
|
116
|
+
if let webUrl = URL(string: "https://apps.apple.com/app/id\(appId)") {
|
|
117
|
+
UIApplication.shared.open(webUrl, options: [:]) { webSuccess in
|
|
118
|
+
if webSuccess {
|
|
119
|
+
call.resolve()
|
|
120
|
+
} else {
|
|
121
|
+
call.reject("OPEN_STORE_FAILED", "Failed to open App Store")
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
call.reject("OPEN_STORE_FAILED", "Failed to open App Store")
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} else {
|
|
130
|
+
call.reject("INVALID_URL", "Invalid App Store URL")
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// MARK: - Async Methods for Background Updates
|
|
136
|
+
|
|
137
|
+
func getAppUpdateInfoAsync() async -> AppUpdateInfo? {
|
|
138
|
+
do {
|
|
139
|
+
let bundleId = Bundle.main.bundleIdentifier ?? ""
|
|
140
|
+
let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0.0"
|
|
141
|
+
|
|
142
|
+
// Check iTunes for app info
|
|
143
|
+
let appInfo = try await checkAppStoreVersion(bundleId: bundleId)
|
|
144
|
+
|
|
145
|
+
let storeVersion = appInfo["version"] as? String ?? "0.0.0"
|
|
146
|
+
let updateAvailable = isVersionNewer(storeVersion, than: currentVersion)
|
|
147
|
+
|
|
148
|
+
return AppUpdateInfo(
|
|
149
|
+
updateAvailable: updateAvailable,
|
|
150
|
+
currentVersion: currentVersion,
|
|
151
|
+
availableVersion: updateAvailable ? storeVersion : nil
|
|
152
|
+
)
|
|
153
|
+
} catch {
|
|
154
|
+
NSLog("Failed to check app update: \(error.localizedDescription)")
|
|
155
|
+
return nil
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// MARK: - Private Methods
|
|
160
|
+
|
|
161
|
+
private func checkAppStoreVersion(bundleId: String) async throws -> [String: Any] {
|
|
162
|
+
guard let countryCode = Locale.current.regionCode else {
|
|
163
|
+
throw NSError(domain: "AppUpdatePlugin", code: 1, userInfo: [
|
|
164
|
+
NSLocalizedDescriptionKey: "Could not determine country code"
|
|
165
|
+
])
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
var components = URLComponents(string: iTunesLookupURL)!
|
|
169
|
+
components.queryItems = [
|
|
170
|
+
URLQueryItem(name: "bundleId", value: bundleId),
|
|
171
|
+
URLQueryItem(name: "country", value: countryCode)
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
guard let url = components.url else {
|
|
175
|
+
throw NSError(domain: "AppUpdatePlugin", code: 2, userInfo: [
|
|
176
|
+
NSLocalizedDescriptionKey: "Invalid iTunes lookup URL"
|
|
177
|
+
])
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let (data, _) = try await URLSession.shared.data(from: url)
|
|
181
|
+
|
|
182
|
+
guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
183
|
+
let results = json["results"] as? [[String: Any]],
|
|
184
|
+
let appInfo = results.first else {
|
|
185
|
+
throw NSError(domain: "AppUpdatePlugin", code: 3, userInfo: [
|
|
186
|
+
NSLocalizedDescriptionKey: "No app found in App Store"
|
|
187
|
+
])
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return appInfo
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private func isVersionNewer(_ version1: String, than version2: String) -> Bool {
|
|
194
|
+
let v1Components = version1.split(separator: ".").compactMap { Int($0) }
|
|
195
|
+
let v2Components = version2.split(separator: ".").compactMap { Int($0) }
|
|
196
|
+
|
|
197
|
+
let maxCount = max(v1Components.count, v2Components.count)
|
|
198
|
+
|
|
199
|
+
for i in 0..<maxCount {
|
|
200
|
+
let v1 = i < v1Components.count ? v1Components[i] : 0
|
|
201
|
+
let v2 = i < v2Components.count ? v2Components[i] : 0
|
|
202
|
+
|
|
203
|
+
if v1 > v2 {
|
|
204
|
+
return true
|
|
205
|
+
} else if v1 < v2 {
|
|
206
|
+
return false
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return false
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private func getAppStoreId() -> String? {
|
|
214
|
+
// Try to get from config first
|
|
215
|
+
if let storeUrl = (config?["storeUrl"] as? [String: Any])?["ios"] as? String {
|
|
216
|
+
// Extract ID from URL like https://apps.apple.com/app/id123456789
|
|
217
|
+
let pattern = #"id(\d+)"#
|
|
218
|
+
if let regex = try? NSRegularExpression(pattern: pattern),
|
|
219
|
+
let match = regex.firstMatch(in: storeUrl, range: NSRange(storeUrl.startIndex..., in: storeUrl)) {
|
|
220
|
+
let range = Range(match.range(at: 1), in: storeUrl)!
|
|
221
|
+
return String(storeUrl[range])
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Try to get from Info.plist
|
|
226
|
+
return Bundle.main.infoDictionary?["AppStoreId"] as? String
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// MARK: - Data Models
|
|
231
|
+
|
|
232
|
+
public struct AppUpdateInfo {
|
|
233
|
+
public let updateAvailable: Bool
|
|
234
|
+
public let currentVersion: String
|
|
235
|
+
public let availableVersion: String?
|
|
236
|
+
|
|
237
|
+
public func toDictionary() -> [String: Any] {
|
|
238
|
+
var obj: [String: Any] = [
|
|
239
|
+
"updateAvailable": updateAvailable,
|
|
240
|
+
"currentVersion": currentVersion
|
|
241
|
+
]
|
|
242
|
+
|
|
243
|
+
if let availableVersion = availableVersion {
|
|
244
|
+
obj["availableVersion"] = availableVersion
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return obj
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
import UserNotifications
|
|
4
|
+
|
|
5
|
+
class BackgroundNotificationManager: NSObject {
|
|
6
|
+
|
|
7
|
+
private weak var plugin: BackgroundUpdatePlugin?
|
|
8
|
+
private var preferences: NotificationPreferences
|
|
9
|
+
private let notificationCenter = UNUserNotificationCenter.current()
|
|
10
|
+
|
|
11
|
+
init(plugin: BackgroundUpdatePlugin) {
|
|
12
|
+
self.plugin = plugin
|
|
13
|
+
self.preferences = NotificationPreferences.default()
|
|
14
|
+
super.init()
|
|
15
|
+
|
|
16
|
+
// Set notification delegate
|
|
17
|
+
notificationCenter.delegate = self
|
|
18
|
+
|
|
19
|
+
// Setup notification categories
|
|
20
|
+
setupNotificationCategories()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func setPreferences(_ preferencesData: [String: Any]) {
|
|
24
|
+
preferences = NotificationPreferences.from(preferencesData)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func getPermissionStatus(completion: @escaping (NotificationPermissionStatus) -> Void) {
|
|
28
|
+
notificationCenter.getNotificationSettings { settings in
|
|
29
|
+
let status = NotificationPermissionStatus(
|
|
30
|
+
granted: settings.authorizationStatus == .authorized,
|
|
31
|
+
canRequest: settings.authorizationStatus == .notDetermined,
|
|
32
|
+
shouldShowRationale: settings.authorizationStatus == .denied
|
|
33
|
+
)
|
|
34
|
+
completion(status)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func requestPermissions(completion: @escaping (Bool) -> Void) {
|
|
39
|
+
notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
|
|
40
|
+
if let error = error {
|
|
41
|
+
NSLog("Notification permission error: \(error.localizedDescription)")
|
|
42
|
+
}
|
|
43
|
+
completion(granted)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func sendUpdateNotification(appUpdate: AppUpdateInfo?, liveUpdate: LatestVersion?) async -> Bool {
|
|
48
|
+
// Check permissions first
|
|
49
|
+
let settings = await notificationCenter.notificationSettings()
|
|
50
|
+
guard settings.authorizationStatus == .authorized else {
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let content = createNotificationContent(appUpdate: appUpdate, liveUpdate: liveUpdate)
|
|
55
|
+
let identifier = "update_notification_\(Int(Date().timeIntervalSince1970))"
|
|
56
|
+
|
|
57
|
+
let request = UNNotificationRequest(
|
|
58
|
+
identifier: identifier,
|
|
59
|
+
content: content,
|
|
60
|
+
trigger: nil // Immediate delivery
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
do {
|
|
64
|
+
try await notificationCenter.add(request)
|
|
65
|
+
|
|
66
|
+
// Notify listeners
|
|
67
|
+
await MainActor.run {
|
|
68
|
+
self.plugin?.notifyListeners("backgroundUpdateNotification", data: [
|
|
69
|
+
"type": appUpdate?.updateAvailable == true ? "app_update" : "live_update",
|
|
70
|
+
"updateAvailable": true,
|
|
71
|
+
"version": appUpdate?.availableVersion ?? liveUpdate?.version ?? "unknown",
|
|
72
|
+
"action": "shown"
|
|
73
|
+
])
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return true
|
|
77
|
+
} catch {
|
|
78
|
+
NSLog("Failed to send notification: \(error.localizedDescription)")
|
|
79
|
+
return false
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
func cancelNotification(identifier: String) {
|
|
84
|
+
notificationCenter.removePendingNotificationRequests(withIdentifiers: [identifier])
|
|
85
|
+
notificationCenter.removeDeliveredNotifications(withIdentifiers: [identifier])
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private func createNotificationContent(appUpdate: AppUpdateInfo?, liveUpdate: LatestVersion?) -> UNMutableNotificationContent {
|
|
89
|
+
let content = UNMutableNotificationContent()
|
|
90
|
+
|
|
91
|
+
// Determine title and body
|
|
92
|
+
var title = preferences.title ?? "App Update Available"
|
|
93
|
+
var body = preferences.description ?? "A new version of the app is available"
|
|
94
|
+
|
|
95
|
+
if let appUpdate = appUpdate, appUpdate.updateAvailable,
|
|
96
|
+
let liveUpdate = liveUpdate, liveUpdate.available {
|
|
97
|
+
title = "App Updates Available"
|
|
98
|
+
body = "App version \(appUpdate.availableVersion ?? "unknown") and content updates are available"
|
|
99
|
+
} else if let appUpdate = appUpdate, appUpdate.updateAvailable {
|
|
100
|
+
title = "App Update Available"
|
|
101
|
+
body = "Version \(appUpdate.availableVersion ?? "unknown") is available"
|
|
102
|
+
} else if let liveUpdate = liveUpdate, liveUpdate.available {
|
|
103
|
+
title = "Content Update Available"
|
|
104
|
+
body = "New content version \(liveUpdate.version ?? "unknown") is available"
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
content.title = title
|
|
108
|
+
content.body = body
|
|
109
|
+
|
|
110
|
+
// Set badge
|
|
111
|
+
content.badge = 1
|
|
112
|
+
|
|
113
|
+
// Set sound
|
|
114
|
+
if preferences.soundEnabled {
|
|
115
|
+
content.sound = .default
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Set category for actions
|
|
119
|
+
if preferences.showActions {
|
|
120
|
+
content.categoryIdentifier = "UPDATE_CATEGORY"
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Set user info
|
|
124
|
+
content.userInfo = [
|
|
125
|
+
"type": "update_available",
|
|
126
|
+
"appUpdate": appUpdate?.toDictionary() ?? [:],
|
|
127
|
+
"liveUpdate": liveUpdate?.toDictionary() ?? [:]
|
|
128
|
+
]
|
|
129
|
+
|
|
130
|
+
return content
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private func setupNotificationCategories() {
|
|
134
|
+
guard preferences.showActions else { return }
|
|
135
|
+
|
|
136
|
+
// Create actions
|
|
137
|
+
let updateNowAction = UNNotificationAction(
|
|
138
|
+
identifier: "UPDATE_NOW",
|
|
139
|
+
title: preferences.actionLabels?.updateNow ?? "Update Now",
|
|
140
|
+
options: [.foreground]
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
let updateLaterAction = UNNotificationAction(
|
|
144
|
+
identifier: "UPDATE_LATER",
|
|
145
|
+
title: preferences.actionLabels?.updateLater ?? "Later",
|
|
146
|
+
options: []
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
let dismissAction = UNNotificationAction(
|
|
150
|
+
identifier: "DISMISS",
|
|
151
|
+
title: preferences.actionLabels?.dismiss ?? "Dismiss",
|
|
152
|
+
options: []
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
// Create category
|
|
156
|
+
let updateCategory = UNNotificationCategory(
|
|
157
|
+
identifier: "UPDATE_CATEGORY",
|
|
158
|
+
actions: [updateNowAction, updateLaterAction, dismissAction],
|
|
159
|
+
intentIdentifiers: [],
|
|
160
|
+
options: []
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
// Set categories
|
|
164
|
+
notificationCenter.setNotificationCategories([updateCategory])
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// MARK: - UNUserNotificationCenterDelegate
|
|
169
|
+
|
|
170
|
+
extension BackgroundNotificationManager: UNUserNotificationCenterDelegate {
|
|
171
|
+
|
|
172
|
+
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
|
173
|
+
// Show notification even when app is in foreground
|
|
174
|
+
if #available(iOS 14.0, *) {
|
|
175
|
+
completionHandler([.banner, .sound, .badge])
|
|
176
|
+
} else {
|
|
177
|
+
completionHandler([.alert, .sound, .badge])
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
182
|
+
|
|
183
|
+
let userInfo = response.notification.request.content.userInfo
|
|
184
|
+
|
|
185
|
+
DispatchQueue.main.async {
|
|
186
|
+
self.plugin?.notifyListeners("backgroundUpdateNotification", data: [
|
|
187
|
+
"type": userInfo["type"] as? String ?? "unknown",
|
|
188
|
+
"updateAvailable": true,
|
|
189
|
+
"action": self.mapActionIdentifier(response.actionIdentifier)
|
|
190
|
+
])
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Handle different actions
|
|
194
|
+
switch response.actionIdentifier {
|
|
195
|
+
case "UPDATE_NOW":
|
|
196
|
+
// Trigger immediate update
|
|
197
|
+
self.handleUpdateNowAction(userInfo: userInfo)
|
|
198
|
+
case "UPDATE_LATER":
|
|
199
|
+
// Schedule reminder or do nothing
|
|
200
|
+
break
|
|
201
|
+
case "DISMISS":
|
|
202
|
+
// Remove notification
|
|
203
|
+
break
|
|
204
|
+
default:
|
|
205
|
+
// Default tap action - open app
|
|
206
|
+
self.handleDefaultTapAction(userInfo: userInfo)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
completionHandler()
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private func mapActionIdentifier(_ identifier: String) -> String {
|
|
213
|
+
switch identifier {
|
|
214
|
+
case "UPDATE_NOW":
|
|
215
|
+
return "update_now"
|
|
216
|
+
case "UPDATE_LATER":
|
|
217
|
+
return "update_later"
|
|
218
|
+
case "DISMISS":
|
|
219
|
+
return "dismiss"
|
|
220
|
+
case UNNotificationDefaultActionIdentifier:
|
|
221
|
+
return "tapped"
|
|
222
|
+
default:
|
|
223
|
+
return "unknown"
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private func handleUpdateNowAction(userInfo: [AnyHashable: Any]) {
|
|
228
|
+
// Here you would typically trigger the update process
|
|
229
|
+
// This could involve calling the app update or live update functionality
|
|
230
|
+
NSLog("Update now action triggered")
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private func handleDefaultTapAction(userInfo: [AnyHashable: Any]) {
|
|
234
|
+
// App was opened via notification tap
|
|
235
|
+
NSLog("Notification tapped - app opened")
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// MARK: - Data Models
|
|
240
|
+
|
|
241
|
+
struct NotificationPreferences {
|
|
242
|
+
let title: String?
|
|
243
|
+
let description: String?
|
|
244
|
+
let iconName: String?
|
|
245
|
+
let soundEnabled: Bool
|
|
246
|
+
let vibrationEnabled: Bool
|
|
247
|
+
let showActions: Bool
|
|
248
|
+
let actionLabels: ActionLabels?
|
|
249
|
+
let channelId: String?
|
|
250
|
+
let channelName: String?
|
|
251
|
+
let priority: NotificationPriority
|
|
252
|
+
|
|
253
|
+
static func `default`() -> NotificationPreferences {
|
|
254
|
+
return NotificationPreferences(
|
|
255
|
+
title: "App Update Available",
|
|
256
|
+
description: "A new version of the app is available",
|
|
257
|
+
iconName: nil,
|
|
258
|
+
soundEnabled: true,
|
|
259
|
+
vibrationEnabled: true,
|
|
260
|
+
showActions: true,
|
|
261
|
+
actionLabels: ActionLabels.default(),
|
|
262
|
+
channelId: "capacitor_native_update",
|
|
263
|
+
channelName: "App Updates",
|
|
264
|
+
priority: .default
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
static func from(_ obj: [String: Any]) -> NotificationPreferences {
|
|
269
|
+
let actionLabelsObj = obj["actionLabels"] as? [String: Any]
|
|
270
|
+
|
|
271
|
+
return NotificationPreferences(
|
|
272
|
+
title: obj["title"] as? String,
|
|
273
|
+
description: obj["description"] as? String,
|
|
274
|
+
iconName: obj["iconName"] as? String,
|
|
275
|
+
soundEnabled: obj["soundEnabled"] as? Bool ?? true,
|
|
276
|
+
vibrationEnabled: obj["vibrationEnabled"] as? Bool ?? true,
|
|
277
|
+
showActions: obj["showActions"] as? Bool ?? true,
|
|
278
|
+
actionLabels: actionLabelsObj != nil ? ActionLabels.from(actionLabelsObj!) : ActionLabels.default(),
|
|
279
|
+
channelId: obj["channelId"] as? String,
|
|
280
|
+
channelName: obj["channelName"] as? String,
|
|
281
|
+
priority: NotificationPriority(rawValue: obj["priority"] as? String ?? "default") ?? .default
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
struct ActionLabels {
|
|
287
|
+
let updateNow: String?
|
|
288
|
+
let updateLater: String?
|
|
289
|
+
let dismiss: String?
|
|
290
|
+
|
|
291
|
+
static func `default`() -> ActionLabels {
|
|
292
|
+
return ActionLabels(
|
|
293
|
+
updateNow: "Update Now",
|
|
294
|
+
updateLater: "Later",
|
|
295
|
+
dismiss: "Dismiss"
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
static func from(_ obj: [String: Any]) -> ActionLabels {
|
|
300
|
+
return ActionLabels(
|
|
301
|
+
updateNow: obj["updateNow"] as? String,
|
|
302
|
+
updateLater: obj["updateLater"] as? String,
|
|
303
|
+
dismiss: obj["dismiss"] as? String
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
enum NotificationPriority: String, CaseIterable {
|
|
309
|
+
case min = "min"
|
|
310
|
+
case low = "low"
|
|
311
|
+
case `default` = "default"
|
|
312
|
+
case high = "high"
|
|
313
|
+
case max = "max"
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
struct NotificationPermissionStatus {
|
|
317
|
+
let granted: Bool
|
|
318
|
+
let canRequest: Bool
|
|
319
|
+
let shouldShowRationale: Bool?
|
|
320
|
+
|
|
321
|
+
func toDictionary() -> [String: Any] {
|
|
322
|
+
var obj: [String: Any] = [
|
|
323
|
+
"granted": granted,
|
|
324
|
+
"canRequest": canRequest
|
|
325
|
+
]
|
|
326
|
+
|
|
327
|
+
if let shouldShowRationale = shouldShowRationale {
|
|
328
|
+
obj["shouldShowRationale"] = shouldShowRationale
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return obj
|
|
332
|
+
}
|
|
333
|
+
}
|