@zapiack/react-native 0.0.0-stage → 0.1.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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +81 -2
- package/android/build.gradle +48 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/zapiack/reactnative/ZapiackPushModule.kt +110 -0
- package/android/src/main/java/com/zapiack/reactnative/ZapiackPushPackage.kt +12 -0
- package/dist/index.d.mts +53 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +107 -0
- package/dist/index.mjs +81 -0
- package/ios/Zapiack/Api.swift +130 -0
- package/ios/Zapiack/EventQueue.swift +95 -0
- package/ios/Zapiack/ImageFile.swift +22 -0
- package/ios/Zapiack/Models.swift +151 -0
- package/ios/Zapiack/Processing.swift +59 -0
- package/ios/Zapiack/PushClient.swift +132 -0
- package/ios/Zapiack/Storage.swift +86 -0
- package/ios/Zapiack/ZapiackPush.swift +119 -0
- package/ios/ZapiackPushModule.m +18 -0
- package/ios/ZapiackPushModule.swift +121 -0
- package/package.json +70 -4
- package/zapiack-react-native.podspec +17 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import UserNotifications
|
|
3
|
+
#if canImport(UIKit)
|
|
4
|
+
import UIKit
|
|
5
|
+
#endif
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/// Push notifications for an app: ask permission, register this install, and follow what happens after a tap.
|
|
9
|
+
public final class ZapiackPush {
|
|
10
|
+
public static let shared = ZapiackPush()
|
|
11
|
+
|
|
12
|
+
private var client: PushClient?
|
|
13
|
+
private var appVersion: String?
|
|
14
|
+
|
|
15
|
+
private init() {}
|
|
16
|
+
|
|
17
|
+
/// Call once at launch, before anything else. `appGroup` is the App Group shared with the notification extension.
|
|
18
|
+
public func configure(appKey: String, appGroup: String, baseUrl: URL = SharedState.defaultBaseUrl) {
|
|
19
|
+
guard appKey.hasPrefix("pk_"), let storage = SharedDefaultsStorage(appGroup: appGroup),
|
|
20
|
+
let container = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {
|
|
21
|
+
assertionFailure("ZapiackPush: the app key must start with pk_ and the App Group must be enabled for this target")
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
SharedState.save(appKey: appKey, baseUrl: baseUrl, in: storage)
|
|
25
|
+
guard let config = SharedState.load(from: storage) else { return }
|
|
26
|
+
client = PushClient(config: config, storage: storage, queue: EventQueue(directory: container.appendingPathComponent("zapiack-events", isDirectory: true)))
|
|
27
|
+
appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
|
|
28
|
+
Task { await self.syncRegistration() }
|
|
29
|
+
Task { await self.client?.flush() }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// The identifier that ties this install to the service.
|
|
33
|
+
public var installationId: String? { client?.config.installationId }
|
|
34
|
+
|
|
35
|
+
/// Shows the system permission prompt, then registers for push when it is allowed.
|
|
36
|
+
@discardableResult
|
|
37
|
+
public func requestPermission(options: UNAuthorizationOptions = [.alert, .badge, .sound]) async -> Bool {
|
|
38
|
+
let granted = (try? await UNUserNotificationCenter.current().requestAuthorization(options: options)) ?? false
|
|
39
|
+
if granted { await registerForRemoteNotifications() }
|
|
40
|
+
await syncRegistration()
|
|
41
|
+
return granted
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// Hand over the token from `application(_:didRegisterForRemoteNotificationsWithDeviceToken:)`.
|
|
45
|
+
public func didRegisterForRemoteNotifications(deviceToken: Data) {
|
|
46
|
+
client?.setDeviceToken(deviceToken)
|
|
47
|
+
Task { await self.syncRegistration() }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public func login(_ userId: String) {
|
|
51
|
+
client?.login(userId)
|
|
52
|
+
Task { await self.syncRegistration() }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public func logout() {
|
|
56
|
+
client?.logout()
|
|
57
|
+
Task { await self.syncRegistration() }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public func addTags(_ tags: [String: JSONValue]) {
|
|
61
|
+
client?.addTags(tags)
|
|
62
|
+
Task { await self.syncRegistration() }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public func removeTags(_ keys: [String]) {
|
|
66
|
+
client?.removeTags(keys)
|
|
67
|
+
Task { await self.syncRegistration() }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Call from `userNotificationCenter(_:didReceive:withCompletionHandler:)`. Returns what was tapped so the app can route, or
|
|
71
|
+
/// nil when the notification is not from Zapiack or was only dismissed.
|
|
72
|
+
@discardableResult
|
|
73
|
+
public func handle(_ response: UNNotificationResponse) -> Tap? {
|
|
74
|
+
guard let client else { return nil }
|
|
75
|
+
let tap = client.handleResponse(userInfo: response.notification.request.content.userInfo, actionIdentifier: response.actionIdentifier)
|
|
76
|
+
if tap != nil { Task { await client.flush() } }
|
|
77
|
+
return tap
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/// A screen the user reached, credited to the push they tapped while its window is open.
|
|
81
|
+
public func screen(_ name: String) { client?.screen(name) }
|
|
82
|
+
|
|
83
|
+
/// A named goal or event, credited to the push they tapped while its window is open.
|
|
84
|
+
/// Returns whether there was a tapped push to credit it to.
|
|
85
|
+
@discardableResult
|
|
86
|
+
public func track(_ name: String, properties: [String: JSONValue]? = nil) -> Bool {
|
|
87
|
+
let credited = client?.track(name, properties: properties) ?? false
|
|
88
|
+
Task { await self.client?.flush() }
|
|
89
|
+
return credited
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// Sends anything waiting. Call when the app comes to the foreground.
|
|
93
|
+
public func flush() async { await client?.flush() }
|
|
94
|
+
|
|
95
|
+
private func syncRegistration() async {
|
|
96
|
+
guard let client, client.deviceToken != nil else { return }
|
|
97
|
+
let settings = await UNUserNotificationCenter.current().notificationSettings()
|
|
98
|
+
let permission: String?
|
|
99
|
+
switch settings.authorizationStatus {
|
|
100
|
+
case .authorized, .ephemeral: permission = "granted"
|
|
101
|
+
case .provisional: permission = "provisional"
|
|
102
|
+
case .denied: permission = "denied"
|
|
103
|
+
default: permission = nil
|
|
104
|
+
}
|
|
105
|
+
#if DEBUG
|
|
106
|
+
let environment = "sandbox"
|
|
107
|
+
#else
|
|
108
|
+
let environment = "production"
|
|
109
|
+
#endif
|
|
110
|
+
try? await client.register(permission: permission, environment: environment, appVersion: appVersion)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@MainActor
|
|
114
|
+
private func registerForRemoteNotifications() {
|
|
115
|
+
#if canImport(UIKit) && !os(watchOS)
|
|
116
|
+
UIApplication.shared.registerForRemoteNotifications()
|
|
117
|
+
#endif
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#import <React/RCTBridgeModule.h>
|
|
2
|
+
#import <React/RCTEventEmitter.h>
|
|
3
|
+
|
|
4
|
+
@interface RCT_EXTERN_MODULE (ZapiackPushModule, RCTEventEmitter)
|
|
5
|
+
|
|
6
|
+
RCT_EXTERN_METHOD(configure : (NSDictionary *)options resolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
7
|
+
RCT_EXTERN_METHOD(requestPermission : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
8
|
+
RCT_EXTERN_METHOD(installationId : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
9
|
+
RCT_EXTERN_METHOD(login : (NSString *)userId resolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
10
|
+
RCT_EXTERN_METHOD(logout : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
11
|
+
RCT_EXTERN_METHOD(addTags : (NSDictionary *)tags resolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
12
|
+
RCT_EXTERN_METHOD(removeTags : (NSArray *)keys resolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
13
|
+
RCT_EXTERN_METHOD(screen : (NSString *)name resolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
14
|
+
RCT_EXTERN_METHOD(track : (NSString *)name properties : (NSDictionary *)properties resolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
15
|
+
RCT_EXTERN_METHOD(flush : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
16
|
+
RCT_EXTERN_METHOD(initialTap : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
|
|
17
|
+
|
|
18
|
+
@end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import React
|
|
3
|
+
import UserNotifications
|
|
4
|
+
|
|
5
|
+
/// Connects the JavaScript API to the iOS library.
|
|
6
|
+
@objc(ZapiackPushModule)
|
|
7
|
+
final class ZapiackPushModule: RCTEventEmitter, UNUserNotificationCenterDelegate {
|
|
8
|
+
private var hasListeners = false
|
|
9
|
+
/// The tap that opened the app, kept until JavaScript asks for it, because it is not listening yet when the app starts.
|
|
10
|
+
private static var initialTap: Tap?
|
|
11
|
+
|
|
12
|
+
override init() {
|
|
13
|
+
super.init()
|
|
14
|
+
UNUserNotificationCenter.current().delegate = self
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@objc override static func requiresMainQueueSetup() -> Bool { true }
|
|
18
|
+
|
|
19
|
+
override func supportedEvents() -> [String]! { ["zapiackTap"] }
|
|
20
|
+
override func startObserving() { hasListeners = true }
|
|
21
|
+
override func stopObserving() { hasListeners = false }
|
|
22
|
+
|
|
23
|
+
/// Call from the app delegate's `didRegisterForRemoteNotificationsWithDeviceToken`, which React Native does not forward.
|
|
24
|
+
@objc static func didRegisterForRemoteNotifications(deviceToken: Data) {
|
|
25
|
+
ZapiackPush.shared.didRegisterForRemoteNotifications(deviceToken: deviceToken)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@objc func configure(_ options: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
29
|
+
guard let key = options["appKey"] as? String, key.hasPrefix("pk_") else { return reject("invalid_key", "The app key must start with pk_", nil) }
|
|
30
|
+
guard let group = options["appGroup"] as? String, !group.isEmpty else {
|
|
31
|
+
return reject("invalid_group", "On iOS, appGroup is required: it is how the app and its notification extension share what they need", nil)
|
|
32
|
+
}
|
|
33
|
+
let base = (options["baseUrl"] as? String).flatMap(URL.init(string:)) ?? SharedState.defaultBaseUrl
|
|
34
|
+
ZapiackPush.shared.configure(appKey: key, appGroup: group, baseUrl: base)
|
|
35
|
+
resolve(nil)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@objc func requestPermission(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
39
|
+
Task { resolve(await ZapiackPush.shared.requestPermission()) }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@objc func installationId(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
43
|
+
resolve(ZapiackPush.shared.installationId)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@objc func login(_ userId: String, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
47
|
+
ZapiackPush.shared.login(userId)
|
|
48
|
+
resolve(nil)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@objc func logout(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
52
|
+
ZapiackPush.shared.logout()
|
|
53
|
+
resolve(nil)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@objc func addTags(_ tags: NSDictionary, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
57
|
+
ZapiackPush.shared.addTags(Self.values(tags))
|
|
58
|
+
resolve(nil)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@objc func removeTags(_ keys: NSArray, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
62
|
+
ZapiackPush.shared.removeTags(keys.compactMap { $0 as? String })
|
|
63
|
+
resolve(nil)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@objc func screen(_ name: String, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
67
|
+
ZapiackPush.shared.screen(name)
|
|
68
|
+
resolve(nil)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@objc func track(_ name: String, properties: NSDictionary?, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
72
|
+
resolve(ZapiackPush.shared.track(name, properties: properties.map(Self.values)))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@objc func flush(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
76
|
+
Task { await ZapiackPush.shared.flush(); resolve(nil) }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@objc func initialTap(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
|
|
80
|
+
resolve(Self.initialTap.map(Self.map))
|
|
81
|
+
Self.initialTap = nil
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// MARK: Taps and notifications in front
|
|
85
|
+
|
|
86
|
+
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
87
|
+
if let tap = ZapiackPush.shared.handle(response) {
|
|
88
|
+
if hasListeners { sendEvent(withName: "zapiackTap", body: Self.map(tap)) } else { Self.initialTap = tap }
|
|
89
|
+
}
|
|
90
|
+
completionHandler()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
|
94
|
+
if #available(iOS 14.0, *) { completionHandler([.banner, .list, .sound, .badge]) } else { completionHandler([.alert, .sound, .badge]) }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// MARK: Helpers
|
|
98
|
+
|
|
99
|
+
private static func values(_ raw: NSDictionary) -> [String: JSONValue] {
|
|
100
|
+
(raw as? [String: Any] ?? [:]).mapValues(json)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private static func json(_ value: Any) -> JSONValue {
|
|
104
|
+
switch value {
|
|
105
|
+
case let v as NSNumber where CFGetTypeID(v) == CFBooleanGetTypeID(): return .bool(v.boolValue)
|
|
106
|
+
case let v as NSNumber: return .number(v.doubleValue)
|
|
107
|
+
case let v as String: return .string(v)
|
|
108
|
+
case let v as [Any]: return .array(v.map(json))
|
|
109
|
+
case let v as [String: Any]: return .object(v.mapValues(json))
|
|
110
|
+
default: return .null
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private static func map(_ tap: Tap) -> [String: Any] {
|
|
115
|
+
var out: [String: Any] = ["messageId": tap.messageId, "deliveryId": tap.deliveryId, "sessionId": tap.sessionId]
|
|
116
|
+
if let a = tap.actionId { out["actionId"] = a }
|
|
117
|
+
if let e = tap.event { out["event"] = e }
|
|
118
|
+
if let l = tap.link { out["link"] = l.absoluteString }
|
|
119
|
+
return out
|
|
120
|
+
}
|
|
121
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zapiack/react-native",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Push notifications for React Native with Zapiack: register devices, follow taps and see what people do next.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Zapiack <support@zapiack.com>",
|
|
7
|
+
"homepage": "https://docs.zapiack.com/push/client-sdks",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/zapiack-hq/zapiack-react-native.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/zapiack-hq/zapiack-react-native/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"zapiack",
|
|
17
|
+
"react-native",
|
|
18
|
+
"push",
|
|
19
|
+
"notifications",
|
|
20
|
+
"fcm",
|
|
21
|
+
"apns"
|
|
22
|
+
],
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/index.d.mts",
|
|
30
|
+
"default": "./dist/index.mjs"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"android",
|
|
42
|
+
"!android/build",
|
|
43
|
+
"!android/.gradle",
|
|
44
|
+
"ios",
|
|
45
|
+
"zapiack-react-native.podspec",
|
|
46
|
+
"README.md",
|
|
47
|
+
"CHANGELOG.md",
|
|
48
|
+
"LICENSE"
|
|
49
|
+
],
|
|
50
|
+
"sideEffects": false,
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"typecheck": "tsc --noEmit",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"sync-ios": "sh tool/sync-ios.sh",
|
|
56
|
+
"prepublishOnly": "npm run sync-ios && npm run typecheck && npm run build && npm test"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"react-native": ">=0.71.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@types/react": "^19.0.0",
|
|
63
|
+
"react": "^19.0.0",
|
|
64
|
+
"react-native": "^0.79.0",
|
|
65
|
+
"tsup": "^9.0.0",
|
|
66
|
+
"typescript": "~5.9.0",
|
|
67
|
+
"vitest": "^5.0.5"
|
|
68
|
+
},
|
|
69
|
+
"publishConfig": {
|
|
70
|
+
"access": "public"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "zapiack-react-native"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = "https://zapiack.com"
|
|
10
|
+
s.license = { :type => "MIT", :file => "LICENSE" }
|
|
11
|
+
s.author = { "Zapiack" => "support@zapiack.com" }
|
|
12
|
+
s.platforms = { :ios => "13.0" }
|
|
13
|
+
s.source = { :git => "https://github.com/zapiack-hq/zapiack-react-native.git", :tag => "v#{s.version}" }
|
|
14
|
+
s.source_files = "ios/**/*.{h,m,swift}"
|
|
15
|
+
s.swift_version = "5.9"
|
|
16
|
+
s.dependency "React-Core"
|
|
17
|
+
end
|