@app-swiss-knife/react-native-sdk 0.5.0 → 0.5.1
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/CHANGELOG.md +88 -0
- package/README.md +57 -12
- package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +17 -4
- package/android/vendor/GENERATED.md +1 -0
- package/android/vendor/java/com/appswissknife/SwissKnife.kt +4 -0
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +1 -1
- package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +47 -2
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +185 -33
- package/android/vendor/java/com/appswissknife/purchases/ProductKindHint.kt +8 -0
- package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +10 -0
- package/app.plugin.js +61 -5
- package/ios/SwissKnifeJSON.swift +24 -2
- package/ios/vendor/GENERATED.md +1 -0
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/Services/OfferingsService.swift +37 -5
- package/ios/vendor/SwissKnifeiOS/Types.swift +31 -1
- package/ios-notification-service/SwissKnifeNotificationService.podspec +37 -0
- package/ios-notification-service/vendor/GENERATED.md +11 -0
- package/ios-notification-service/vendor/SwissKnifeNotificationService/SwissKnifeNotificationServiceExtension.swift +177 -0
- package/package.json +3 -2
|
@@ -158,6 +158,28 @@ public struct StoreProductDiscount: Sendable, Codable, Equatable {
|
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
+
// MARK: - StoreProductType
|
|
162
|
+
|
|
163
|
+
/// What kind of in-app product a `StoreProduct` is: StoreKit 2's
|
|
164
|
+
/// `Product.ProductType`, without importing StoreKit. Raw values are the
|
|
165
|
+
/// backend's product-type vocabulary (`auto_renewable`, `non_renewing`,
|
|
166
|
+
/// `consumable`, `non_consumable`).
|
|
167
|
+
public enum StoreProductType: String, Sendable, Codable, CaseIterable {
|
|
168
|
+
case autoRenewableSubscription = "auto_renewable"
|
|
169
|
+
case nonRenewableSubscription = "non_renewing"
|
|
170
|
+
case consumable
|
|
171
|
+
case nonConsumable = "non_consumable"
|
|
172
|
+
/// Not known: a StoreKit type this SDK does not recognise, or a one-time
|
|
173
|
+
/// product described only by the backend, which does not say whether it
|
|
174
|
+
/// is consumable.
|
|
175
|
+
case unknown
|
|
176
|
+
|
|
177
|
+
/// True only for an auto-renewable subscription (RevenueCat's
|
|
178
|
+
/// `ProductCategory.subscription`). A non-renewing subscription is a
|
|
179
|
+
/// one-time purchase with no billing period, as consumables are.
|
|
180
|
+
public var isSubscription: Bool { self == .autoRenewableSubscription }
|
|
181
|
+
}
|
|
182
|
+
|
|
161
183
|
// MARK: - StoreProduct
|
|
162
184
|
|
|
163
185
|
/// A single product as returned by the App Store. We populate this directly
|
|
@@ -173,9 +195,14 @@ public struct StoreProduct: Sendable, Equatable {
|
|
|
173
195
|
/// Pre-formatted display price (e.g. `"$9.99"`) — passes through StoreKit's
|
|
174
196
|
/// localized formatter so we don't re-derive the locale ourselves.
|
|
175
197
|
public let displayPrice: String
|
|
198
|
+
/// The SDK sets this only for an auto-renewable subscription.
|
|
176
199
|
public let subscriptionPeriod: SubscriptionPeriod?
|
|
200
|
+
/// The SDK sets this only for an auto-renewable subscription.
|
|
177
201
|
public let introductoryDiscount: StoreProductDiscount?
|
|
178
202
|
public let discounts: [StoreProductDiscount]
|
|
203
|
+
/// StoreKit's product type (`Product.type`), or what the backend says for
|
|
204
|
+
/// a product StoreKit did not return.
|
|
205
|
+
public let productType: StoreProductType
|
|
179
206
|
|
|
180
207
|
/// Number formatter pre-configured for this product's currency. Recreating
|
|
181
208
|
/// it on demand is cheap; we surface it so consumers can format derived
|
|
@@ -196,7 +223,9 @@ public struct StoreProduct: Sendable, Equatable {
|
|
|
196
223
|
displayPrice: String,
|
|
197
224
|
subscriptionPeriod: SubscriptionPeriod?,
|
|
198
225
|
introductoryDiscount: StoreProductDiscount?,
|
|
199
|
-
discounts: [StoreProductDiscount]
|
|
226
|
+
discounts: [StoreProductDiscount],
|
|
227
|
+
// Defaulted so code that builds a StoreProduct by hand keeps compiling.
|
|
228
|
+
productType: StoreProductType = .unknown
|
|
200
229
|
) {
|
|
201
230
|
self.productIdentifier = productIdentifier
|
|
202
231
|
self.localizedTitle = localizedTitle
|
|
@@ -207,6 +236,7 @@ public struct StoreProduct: Sendable, Equatable {
|
|
|
207
236
|
self.subscriptionPeriod = subscriptionPeriod
|
|
208
237
|
self.introductoryDiscount = introductoryDiscount
|
|
209
238
|
self.discounts = discounts
|
|
239
|
+
self.productType = productType
|
|
210
240
|
}
|
|
211
241
|
}
|
|
212
242
|
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# packages/react-native-sdk/ios-notification-service/SwissKnifeNotificationService.podspec
|
|
2
|
+
#
|
|
3
|
+
# Notification Service Extension base class for swiss-push rich notifications,
|
|
4
|
+
# shipped in the npm package so a React Native app can add it from
|
|
5
|
+
# node_modules. In the app's Podfile, inside the extension's target:
|
|
6
|
+
#
|
|
7
|
+
# target 'YourNotificationService' do
|
|
8
|
+
# pod 'SwissKnifeNotificationService', :path => '../node_modules/@app-swiss-knife/react-native-sdk/ios-notification-service'
|
|
9
|
+
# end
|
|
10
|
+
#
|
|
11
|
+
# Foundation + UserNotifications only, so the extension stays under its memory
|
|
12
|
+
# cap. The sources under vendor/ are copied from
|
|
13
|
+
# packages/swiss-knife-ios/Sources/SwissKnifeNotificationService by
|
|
14
|
+
# scripts/vendor-native.mjs at build/prepack time (gitignored here).
|
|
15
|
+
#
|
|
16
|
+
# This podspec lives in its own folder on purpose. React Native and Expo
|
|
17
|
+
# autolinking link the FIRST *.podspec at the package root into the app target
|
|
18
|
+
# (Expo sorts them, and "SwissKnifeNotificationService" sorts before
|
|
19
|
+
# "SwissKnifePurchases"), so a second podspec at the root would replace the
|
|
20
|
+
# SDK's pod in every app. Keep exactly one podspec at the root. For the same
|
|
21
|
+
# reason, the folder is not under ios/, which SwissKnifePurchases.podspec
|
|
22
|
+
# compiles whole (`ios/**`): the app pod must not gain this extension class.
|
|
23
|
+
Pod::Spec.new do |s|
|
|
24
|
+
s.name = "SwissKnifeNotificationService"
|
|
25
|
+
s.version = "0.5.1"
|
|
26
|
+
s.summary = "Swiss Knife push: UNNotificationServiceExtension base class (image attachments, received tracking)."
|
|
27
|
+
s.homepage = "https://github.com/Remneys/swiss-knife"
|
|
28
|
+
s.license = "MIT"
|
|
29
|
+
s.authors = "App Swiss Knife"
|
|
30
|
+
s.platforms = { :ios => "15.0" }
|
|
31
|
+
s.source = { :git => "https://github.com/Remneys/swiss-knife.git", :tag => "#{s.version}" }
|
|
32
|
+
s.source_files = "vendor/SwissKnifeNotificationService/**/*.swift"
|
|
33
|
+
s.swift_version = "5.9"
|
|
34
|
+
s.frameworks = "UserNotifications"
|
|
35
|
+
# App extensions may only link extension-safe API.
|
|
36
|
+
s.pod_target_xcconfig = { "APPLICATION_EXTENSION_API_ONLY" => "YES" }
|
|
37
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
GENERATED — DO NOT EDIT.
|
|
2
|
+
|
|
3
|
+
Copied verbatim by packages/react-native-sdk/scripts/vendor-native.mjs from the
|
|
4
|
+
standalone native SDK sources in the monorepo. Edit those instead:
|
|
5
|
+
|
|
6
|
+
packages/swiss-knife-ios/Sources/SwissKnifeiOS/**
|
|
7
|
+
packages/swiss-knife-ios/Sources/SwissKnifeNotificationService/**
|
|
8
|
+
packages/swiss-knife-android/src/main/{java,res}/**
|
|
9
|
+
|
|
10
|
+
This directory is gitignored and regenerated on every `build` / `prepack`.
|
|
11
|
+
It ships inside the npm tarball so the package is self-contained.
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// packages/swiss-knife-ios/Sources/SwissKnifeNotificationService/SwissKnifeNotificationServiceExtension.swift
|
|
2
|
+
//
|
|
3
|
+
// Drop-in `UNNotificationServiceExtension` for swiss-push rich notifications.
|
|
4
|
+
//
|
|
5
|
+
// This target is deliberately SELF-CONTAINED — it imports only Foundation and
|
|
6
|
+
// UserNotifications and does NOT link the main `SwissKnifeiOS` library, so the
|
|
7
|
+
// extension binary stays tiny (extensions have a hard ~24 MB memory cap).
|
|
8
|
+
//
|
|
9
|
+
// Usage (in your Notification Service Extension target):
|
|
10
|
+
//
|
|
11
|
+
// import SwissKnifeNotificationService
|
|
12
|
+
// final class NotificationService: SwissKnifeNotificationServiceExtension {
|
|
13
|
+
// // OPTIONAL — enables `received` tracking from the extension:
|
|
14
|
+
// override var trackingConfiguration: TrackingConfiguration? {
|
|
15
|
+
// .init(apiKey: "sk_live_...", baseURL: URL(string: "https://api.swiss-knife.dev")!)
|
|
16
|
+
// }
|
|
17
|
+
// }
|
|
18
|
+
//
|
|
19
|
+
// It downloads `sk.img` and attaches it, inside the OS time budget.
|
|
20
|
+
|
|
21
|
+
import Foundation
|
|
22
|
+
import UserNotifications
|
|
23
|
+
|
|
24
|
+
open class SwissKnifeNotificationServiceExtension: UNNotificationServiceExtension {
|
|
25
|
+
|
|
26
|
+
public struct TrackingConfiguration: Sendable {
|
|
27
|
+
public let apiKey: String
|
|
28
|
+
public let baseURL: URL
|
|
29
|
+
public init(apiKey: String, baseURL: URL) {
|
|
30
|
+
self.apiKey = apiKey
|
|
31
|
+
self.baseURL = baseURL
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// Override to also record `received` for every delivery (the main app
|
|
36
|
+
/// only sees foreground deliveries). nil (default) = no network call
|
|
37
|
+
/// besides the image download.
|
|
38
|
+
open var trackingConfiguration: TrackingConfiguration? { nil }
|
|
39
|
+
|
|
40
|
+
/// Seconds allowed for the image download. iOS gives the extension ~30s;
|
|
41
|
+
/// keep a safety margin.
|
|
42
|
+
open var imageTimeout: TimeInterval { 20 }
|
|
43
|
+
|
|
44
|
+
// `didReceive` work (a Task) and `serviceExtensionTimeWillExpire` (system
|
|
45
|
+
// thread) race to deliver; the handler must be called exactly once, and the
|
|
46
|
+
// content must not be mutated while it is being handed over.
|
|
47
|
+
private let state = NSLock()
|
|
48
|
+
private var contentHandler: ((UNNotificationContent) -> Void)?
|
|
49
|
+
private var bestAttempt: UNMutableNotificationContent?
|
|
50
|
+
private var work: Task<Void, Never>?
|
|
51
|
+
|
|
52
|
+
open override func didReceive(
|
|
53
|
+
_ request: UNNotificationRequest,
|
|
54
|
+
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
|
|
55
|
+
) {
|
|
56
|
+
let content = (request.content.mutableCopy() as? UNMutableNotificationContent) ?? UNMutableNotificationContent()
|
|
57
|
+
setUp(handler: contentHandler, content: content)
|
|
58
|
+
|
|
59
|
+
let meta = SKPushMeta.parse(request.content.userInfo)
|
|
60
|
+
guard let meta else { return finish(attaching: nil) }
|
|
61
|
+
|
|
62
|
+
work = Task { [weak self] in
|
|
63
|
+
guard let self else { return }
|
|
64
|
+
async let attachment: UNNotificationAttachment? = {
|
|
65
|
+
guard let img = meta.imageURL else { return nil }
|
|
66
|
+
return await SKImageAttachment.download(img, timeout: self.imageTimeout)
|
|
67
|
+
}()
|
|
68
|
+
async let tracked: Void = {
|
|
69
|
+
if let cfg = self.trackingConfiguration {
|
|
70
|
+
await SKReceivedReporter.report(meta, config: cfg)
|
|
71
|
+
}
|
|
72
|
+
}()
|
|
73
|
+
let a = await attachment
|
|
74
|
+
_ = await tracked
|
|
75
|
+
self.finish(attaching: a)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
open override func serviceExtensionTimeWillExpire() {
|
|
80
|
+
work?.cancel()
|
|
81
|
+
finish(attaching: nil)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private func setUp(handler: @escaping (UNNotificationContent) -> Void, content: UNMutableNotificationContent) {
|
|
85
|
+
state.lock(); defer { state.unlock() }
|
|
86
|
+
contentHandler = handler
|
|
87
|
+
bestAttempt = content
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Delivers the content once. The first caller wins; later callers no-op.
|
|
91
|
+
private func finish(attaching attachment: UNNotificationAttachment?) {
|
|
92
|
+
state.lock()
|
|
93
|
+
guard let handler = contentHandler, let content = bestAttempt else { state.unlock(); return }
|
|
94
|
+
contentHandler = nil
|
|
95
|
+
if let attachment { content.attachments = [attachment] }
|
|
96
|
+
state.unlock()
|
|
97
|
+
handler(content)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// MARK: - Payload
|
|
102
|
+
|
|
103
|
+
internal struct SKPushMeta: Equatable {
|
|
104
|
+
let messageId: String
|
|
105
|
+
let deliveryId: String
|
|
106
|
+
let imageURL: URL?
|
|
107
|
+
|
|
108
|
+
static func parse(_ userInfo: [AnyHashable: Any]) -> SKPushMeta? {
|
|
109
|
+
guard let sk = userInfo["sk"] as? [String: Any],
|
|
110
|
+
let m = sk["m"] as? String, !m.isEmpty,
|
|
111
|
+
let d = sk["d"] as? String, !d.isEmpty
|
|
112
|
+
else { return nil }
|
|
113
|
+
let img = (sk["img"] as? String).flatMap { $0.isEmpty ? nil : URL(string: $0) }
|
|
114
|
+
return SKPushMeta(messageId: m, deliveryId: d, imageURL: img.flatMap { $0.scheme == "https" ? $0 : nil })
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// MARK: - Image download
|
|
119
|
+
|
|
120
|
+
internal enum SKImageAttachment {
|
|
121
|
+
|
|
122
|
+
static func fileExtension(mimeType: String?, url: URL) -> String {
|
|
123
|
+
switch mimeType?.lowercased().split(separator: ";").first.map(String.init) {
|
|
124
|
+
case "image/jpeg", "image/jpg": return "jpg"
|
|
125
|
+
case "image/png": return "png"
|
|
126
|
+
case "image/gif": return "gif"
|
|
127
|
+
case "image/heic": return "heic"
|
|
128
|
+
case "image/webp": return "webp"
|
|
129
|
+
default:
|
|
130
|
+
let ext = url.pathExtension.lowercased()
|
|
131
|
+
return ext.isEmpty ? "jpg" : ext
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static func download(_ url: URL, timeout: TimeInterval) async -> UNNotificationAttachment? {
|
|
136
|
+
var request = URLRequest(url: url)
|
|
137
|
+
request.timeoutInterval = timeout
|
|
138
|
+
do {
|
|
139
|
+
let (tmp, response) = try await URLSession.shared.download(for: request)
|
|
140
|
+
if let http = response as? HTTPURLResponse, !(200...299).contains(http.statusCode) { return nil }
|
|
141
|
+
let ext = fileExtension(mimeType: response.mimeType, url: url)
|
|
142
|
+
let dest = FileManager.default.temporaryDirectory
|
|
143
|
+
.appendingPathComponent("sk-\(UUID().uuidString).\(ext)")
|
|
144
|
+
try FileManager.default.moveItem(at: tmp, to: dest)
|
|
145
|
+
return try UNNotificationAttachment(identifier: "sk-image", url: dest, options: nil)
|
|
146
|
+
} catch {
|
|
147
|
+
return nil
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// MARK: - `received` reporting
|
|
153
|
+
|
|
154
|
+
internal enum SKReceivedReporter {
|
|
155
|
+
static func body(_ meta: SKPushMeta, now: Date = Date()) -> Data? {
|
|
156
|
+
let iso = ISO8601DateFormatter()
|
|
157
|
+
iso.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
158
|
+
let event: [String: Any] = [
|
|
159
|
+
"delivery_id": meta.deliveryId,
|
|
160
|
+
"message_id": meta.messageId,
|
|
161
|
+
"type": "received",
|
|
162
|
+
"occurred_at": iso.string(from: now),
|
|
163
|
+
]
|
|
164
|
+
return try? JSONSerialization.data(withJSONObject: ["events": [event]])
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static func report(_ meta: SKPushMeta, config: SwissKnifeNotificationServiceExtension.TrackingConfiguration) async {
|
|
168
|
+
guard let body = body(meta) else { return }
|
|
169
|
+
var request = URLRequest(url: config.baseURL.appendingPathComponent("api/sdk/push/events"))
|
|
170
|
+
request.httpMethod = "POST"
|
|
171
|
+
request.httpBody = body
|
|
172
|
+
request.timeoutInterval = 10
|
|
173
|
+
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
174
|
+
request.setValue(config.apiKey, forHTTPHeaderField: "X-API-Key")
|
|
175
|
+
_ = try? await URLSession.shared.data(for: request)
|
|
176
|
+
}
|
|
177
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-swiss-knife/react-native-sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "React Native SDK for App Swiss Knife: subscriptions and in-app purchases, analytics, crash reporting, performance monitoring and push notifications. The Purchases API keeps react-native-purchases names for the methods it implements.",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./lib/commonjs/index.js",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"lib",
|
|
39
39
|
"android",
|
|
40
40
|
"ios",
|
|
41
|
+
"ios-notification-service",
|
|
41
42
|
"SwissKnifePurchases.podspec",
|
|
42
43
|
"react-native.config.js",
|
|
43
44
|
"app.plugin.js",
|
|
@@ -118,7 +119,7 @@
|
|
|
118
119
|
"typecheck": "tsc --noEmit",
|
|
119
120
|
"test": "vitest run",
|
|
120
121
|
"check:hermes": "node scripts/hermes-boot-check.mjs",
|
|
121
|
-
"clean": "node -e \"for (const d of ['lib','ios/vendor','android/vendor','android/vendor-rustore']) require('fs').rmSync(d,{recursive:true,force:true})\"",
|
|
122
|
+
"clean": "node -e \"for (const d of ['lib','ios/vendor','ios-notification-service/vendor','android/vendor','android/vendor-rustore']) require('fs').rmSync(d,{recursive:true,force:true})\"",
|
|
122
123
|
"vendor:native": "node scripts/vendor-native.mjs",
|
|
123
124
|
"build": "node scripts/vendor-native.mjs && bob build"
|
|
124
125
|
}
|