@pushpilotai/react-native 0.0.0-stage → 2.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.
@@ -0,0 +1,29 @@
1
+ import Foundation
2
+
3
+ /// Reads an APNs device token written as hex text. expo-notifications writes it in lowercase,
4
+ /// React Native Firebase's `getAPNSToken` in uppercase; both are accepted.
5
+ enum RNPushPilotToken {
6
+ /// The token's bytes, or nil when the text is empty, of odd length or not hex: an FCM token or a
7
+ /// Firebase Installation ID, which the iOS SDK no longer uses.
8
+ static func data(fromHex hex: String) -> Data? {
9
+ let digits = Array(hex.utf8)
10
+ guard !digits.isEmpty, digits.count % 2 == 0 else { return nil }
11
+ var bytes = [UInt8]()
12
+ bytes.reserveCapacity(digits.count / 2)
13
+ for i in stride(from: 0, to: digits.count, by: 2) {
14
+ guard let high = nibble(digits[i]), let low = nibble(digits[i + 1]) else { return nil }
15
+ bytes.append(high << 4 | low)
16
+ }
17
+ return Data(bytes)
18
+ }
19
+
20
+ /// The value of one ASCII hex digit, or nil for any other byte.
21
+ private static func nibble(_ c: UInt8) -> UInt8? {
22
+ switch c {
23
+ case 48...57: return c - 48 // 0-9
24
+ case 65...70: return c - 55 // A-F
25
+ case 97...102: return c - 87 // a-f
26
+ default: return nil
27
+ }
28
+ }
29
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ import { TurboModuleRegistry } from 'react-native';
4
+
5
+ // The native module's shape; codegen turns it into the Kotlin and Objective-C interfaces.
6
+
7
+ // `get`, not `getEnforcing`: a missing native module (Expo Go, not rebuilt) must not throw at import.
8
+ export default TurboModuleRegistry.get('PushPilot');
9
+ //# sourceMappingURL=NativePushPilot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["TurboModuleRegistry","get"],"sourceRoot":"../../src","sources":["NativePushPilot.ts"],"mappings":";;AAAA,SACEA,mBAAmB,QAGd,cAAc;;AAErB;;AAkBA;AACA,eAAeA,mBAAmB,CAACC,GAAG,CAAO,WAAW,CAAC","ignoreList":[]}
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+
3
+ import { NativeEventEmitter, Platform } from 'react-native';
4
+ import Native from "./NativePushPilot.js";
5
+
6
+ /** A custom attribute value; `null` removes the attribute. Pass dates as RFC 3339 strings. */
7
+
8
+ /** A tapped PushPilot push that opened the app. */
9
+
10
+ /** Options for `PushPilot.init`. */
11
+
12
+ // Runs one native call, logging instead of throwing so the SDK can never crash the app.
13
+ function call(name, run) {
14
+ try {
15
+ if (Native) {
16
+ run(Native);
17
+ } else {
18
+ console.warn(`[PushPilot] ${name} ignored: the native module is not linked. Rebuild the app (Expo Go is not supported).`);
19
+ }
20
+ } catch (e) {
21
+ console.warn(`[PushPilot] ${name} failed: ${String(e)}`);
22
+ }
23
+ }
24
+
25
+ // An APNs device token as hex: what iOS needs since 2.0. FCM tokens and FIDs contain other characters.
26
+ const APNS_HEX = /^(?:[0-9a-fA-F]{2})+$/;
27
+ // JavaScript's current link and tap handlers, if any.
28
+ const subscriptions = {};
29
+
30
+ // Replaces the handler for one event and tells native whether one is set now.
31
+ function setHandler(event, handler, enable) {
32
+ call(event === 'PushPilotLink' ? 'setLinkHandler' : 'setTapHandler', n => {
33
+ subscriptions[event]?.remove();
34
+ subscriptions[event] = handler ? new NativeEventEmitter(n).addListener(event, handler) : undefined;
35
+ enable(n, handler !== null);
36
+ });
37
+ }
38
+ const PushPilot = {
39
+ /** Starts the SDK with the app's public SDK key. Later calls are ignored; earlier calls are queued. */
40
+ init(sdkKey, options) {
41
+ call('init', n => n.initialize(sdkKey, options?.baseUrl ?? null));
42
+ },
43
+ /**
44
+ * Passes the push identifier; `null` clears it.
45
+ * - Android: the Firebase Installation ID from `register` / `onRegistered` where your Firebase
46
+ * library has it, otherwise the FCM token from `getToken` / `onTokenRefresh`. Pass only one kind.
47
+ * (Not PushPilot's own installation id.)
48
+ * - iOS: the APNs device token as hex, e.g. from expo-notifications' `getDevicePushTokenAsync()` or
49
+ * React Native Firebase's `getAPNSToken()`. Not needed when your AppDelegate forwards the token
50
+ * natively. Anything else (an FCM token or FID, the 1.x iOS path) is ignored with a warning.
51
+ */
52
+ setPushToken(token) {
53
+ if (Platform.OS === 'ios' && token !== null && !APNS_HEX.test(token)) {
54
+ console.warn('[PushPilot] setPushToken ignored: on iOS pass the APNs device token as hex, not an FCM token or Firebase Installation ID.');
55
+ return;
56
+ }
57
+ call('setPushToken', n => n.setPushToken(token));
58
+ },
59
+ /** Links this installation to your own user id after login; `null` clears it. */
60
+ setExternalUserId(id) {
61
+ call('setExternalUserId', n => n.setExternalUserId(id));
62
+ },
63
+ /** Merges custom attributes declared in the dashboard. */
64
+ setAttributes(attributes) {
65
+ call('setAttributes', n => n.setAttributes(attributes));
66
+ },
67
+ /** Replaces this installation's topic list. */
68
+ setTopics(topics) {
69
+ call('setTopics', n => n.setTopics(topics));
70
+ },
71
+ /** Clears the external user id; the device stays registered and keeps receiving pushes. */
72
+ logout() {
73
+ call('logout', n => n.logout());
74
+ },
75
+ /** Deletes this installation from PushPilot and clears all local SDK state. */
76
+ forget() {
77
+ call('forget', n => n.forget());
78
+ },
79
+ /**
80
+ * Receives the link of each tapped PushPilot push (the button's link, or the push's own) instead of
81
+ * the system opening it, like the native SDKs' `setLinkHandler`; `null` lets the system open links
82
+ * again. Set it before `init` to also get the tap that launched the app; each link arrives once.
83
+ */
84
+ setLinkHandler(handler) {
85
+ setHandler('PushPilotLink', handler, (n, on) => n.setLinkHandlerEnabled(on));
86
+ },
87
+ /**
88
+ * Receives the id and custom data of each tapped PushPilot push that opens the app; `null` stops it.
89
+ * Set it before `init` to also get the tap that launched the app; each tap arrives once.
90
+ */
91
+ setTapHandler(handler) {
92
+ setHandler('PushPilotTap', handler, (n, on) => n.setTapHandlerEnabled(on));
93
+ },
94
+ /** True when a received push's data came from PushPilot. Same rule as the native SDKs; no native call. */
95
+ handleMessage(data) {
96
+ return data?.source === 'pushpilot';
97
+ }
98
+ };
99
+ export default PushPilot;
100
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["NativeEventEmitter","Platform","Native","call","name","run","console","warn","e","String","APNS_HEX","subscriptions","setHandler","event","handler","enable","n","remove","addListener","undefined","PushPilot","init","sdkKey","options","initialize","baseUrl","setPushToken","token","OS","test","setExternalUserId","id","setAttributes","attributes","setTopics","topics","logout","forget","setLinkHandler","on","setLinkHandlerEnabled","setTapHandler","setTapHandlerEnabled","handleMessage","data","source"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":"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","ignoreList":[]}
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1,17 @@
1
+ import { type CodegenTypes, type TurboModule } from 'react-native';
2
+ export interface Spec extends TurboModule {
3
+ initialize(sdkKey: string, baseUrl: string | null): void;
4
+ setPushToken(token: string | null): void;
5
+ setExternalUserId(id: string | null): void;
6
+ setAttributes(attributes: CodegenTypes.UnsafeObject): void;
7
+ setTopics(topics: string[]): void;
8
+ logout(): void;
9
+ forget(): void;
10
+ setLinkHandlerEnabled(enabled: boolean): void;
11
+ setTapHandlerEnabled(enabled: boolean): void;
12
+ addListener(eventName: string): void;
13
+ removeListeners(count: number): void;
14
+ }
15
+ declare const _default: Spec | null | undefined;
16
+ export default _default;
17
+ //# sourceMappingURL=NativePushPilot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NativePushPilot.d.ts","sourceRoot":"","sources":["../../../src/NativePushPilot.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,WAAW,EACjB,MAAM,cAAc,CAAC;AAGtB,MAAM,WAAW,IAAK,SAAQ,WAAW;IAEvC,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IACzD,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IACzC,iBAAiB,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IAC3C,aAAa,CAAC,UAAU,EAAE,YAAY,CAAC,YAAY,GAAG,IAAI,CAAC;IAC3D,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAClC,MAAM,IAAI,IAAI,CAAC;IACf,MAAM,IAAI,IAAI,CAAC;IAEf,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAC9C,oBAAoB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAE7C,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC;;AAGD,wBAA0D"}
@@ -0,0 +1,55 @@
1
+ /** A custom attribute value; `null` removes the attribute. Pass dates as RFC 3339 strings. */
2
+ export type AttributeValue = string | number | boolean | null;
3
+ /** A tapped PushPilot push that opened the app. */
4
+ export interface TappedNotification {
5
+ /** PushPilot's id for the notification, the same one its events carry. */
6
+ notificationId: string;
7
+ /** The push's custom data; empty when it had none. */
8
+ data: Record<string, string>;
9
+ }
10
+ /** Options for `PushPilot.init`. */
11
+ export interface InitOptions {
12
+ /** Your own server, for self-hosted PushPilot. Defaults to https://pushpilot.ai. */
13
+ baseUrl?: string;
14
+ }
15
+ declare const PushPilot: {
16
+ /** Starts the SDK with the app's public SDK key. Later calls are ignored; earlier calls are queued. */
17
+ init(sdkKey: string, options?: InitOptions): void;
18
+ /**
19
+ * Passes the push identifier; `null` clears it.
20
+ * - Android: the Firebase Installation ID from `register` / `onRegistered` where your Firebase
21
+ * library has it, otherwise the FCM token from `getToken` / `onTokenRefresh`. Pass only one kind.
22
+ * (Not PushPilot's own installation id.)
23
+ * - iOS: the APNs device token as hex, e.g. from expo-notifications' `getDevicePushTokenAsync()` or
24
+ * React Native Firebase's `getAPNSToken()`. Not needed when your AppDelegate forwards the token
25
+ * natively. Anything else (an FCM token or FID, the 1.x iOS path) is ignored with a warning.
26
+ */
27
+ setPushToken(token: string | null): void;
28
+ /** Links this installation to your own user id after login; `null` clears it. */
29
+ setExternalUserId(id: string | null): void;
30
+ /** Merges custom attributes declared in the dashboard. */
31
+ setAttributes(attributes: Record<string, AttributeValue>): void;
32
+ /** Replaces this installation's topic list. */
33
+ setTopics(topics: string[]): void;
34
+ /** Clears the external user id; the device stays registered and keeps receiving pushes. */
35
+ logout(): void;
36
+ /** Deletes this installation from PushPilot and clears all local SDK state. */
37
+ forget(): void;
38
+ /**
39
+ * Receives the link of each tapped PushPilot push (the button's link, or the push's own) instead of
40
+ * the system opening it, like the native SDKs' `setLinkHandler`; `null` lets the system open links
41
+ * again. Set it before `init` to also get the tap that launched the app; each link arrives once.
42
+ */
43
+ setLinkHandler(handler: ((url: string) => void) | null): void;
44
+ /**
45
+ * Receives the id and custom data of each tapped PushPilot push that opens the app; `null` stops it.
46
+ * Set it before `init` to also get the tap that launched the app; each tap arrives once.
47
+ */
48
+ setTapHandler(handler: ((tap: TappedNotification) => void) | null): void;
49
+ /** True when a received push's data came from PushPilot. Same rule as the native SDKs; no native call. */
50
+ handleMessage(data: {
51
+ [key: string]: unknown;
52
+ } | null | undefined): boolean;
53
+ };
54
+ export default PushPilot;
55
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAOA,8FAA8F;AAC9F,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAE9D,mDAAmD;AACnD,MAAM,WAAW,kBAAkB;IACjC,0EAA0E;IAC1E,cAAc,EAAE,MAAM,CAAC;IACvB,sDAAsD;IACtD,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9B;AAED,oCAAoC;AACpC,MAAM,WAAW,WAAW;IAC1B,oFAAoF;IACpF,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAwCD,QAAA,MAAM,SAAS;IACb,uGAAuG;iBAC1F,MAAM,YAAY,WAAW,GAAG,IAAI;IAIjD;;;;;;;;OAQG;wBACiB,MAAM,GAAG,IAAI,GAAG,IAAI;IAUxC,iFAAiF;0BAC3D,MAAM,GAAG,IAAI,GAAG,IAAI;IAI1C,0DAA0D;8BAChC,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,GAAG,IAAI;IAI/D,+CAA+C;sBAC7B,MAAM,EAAE,GAAG,IAAI;IAIjC,2FAA2F;cACjF,IAAI;IAId,+EAA+E;cACrE,IAAI;IAId;;;;OAIG;4BACqB,CAAC,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,IAAI;IAM7D;;;OAGG;2BACoB,CAAC,CAAC,GAAG,EAAE,kBAAkB,KAAK,IAAI,CAAC,GAAG,IAAI,GAAG,IAAI;IAIxE,0GAA0G;wBACtF;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO;CAG5E,CAAC;AAEF,eAAe,SAAS,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,149 @@
1
1
  {
2
2
  "name": "@pushpilotai/react-native",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.0.0",
4
+ "description": "PushPilot push notifications for React Native",
5
+ "main": "./lib/module/index.js",
6
+ "types": "./lib/typescript/src/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "pushpilot-react-native-source": "./src/index.tsx",
10
+ "types": "./lib/typescript/src/index.d.ts",
11
+ "default": "./lib/module/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "lib",
18
+ "android",
19
+ "ios",
20
+ "cpp",
21
+ "*.podspec",
22
+ "react-native.config.js",
23
+ "!ios/build",
24
+ "!android/build",
25
+ "!android/src/test",
26
+ "!android/gradle",
27
+ "!android/gradlew",
28
+ "!android/gradlew.bat",
29
+ "!android/local.properties",
30
+ "!**/__tests__",
31
+ "!**/__fixtures__",
32
+ "!**/__mocks__",
33
+ "!**/.*"
34
+ ],
35
+ "scripts": {
36
+ "example": "yarn workspace @pushpilotai/react-native-example",
37
+ "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
38
+ "prepare": "bob build",
39
+ "typecheck": "tsc",
40
+ "lint": "eslint \"**/*.{js,ts,tsx}\"",
41
+ "test": "jest",
42
+ "test:ios": "swiftc -o \"${TMPDIR:-/tmp}/pushpilot-token-test\" ios/RNPushPilotToken.swift test/ios/main.swift && \"${TMPDIR:-/tmp}/pushpilot-token-test\""
43
+ },
44
+ "keywords": [
45
+ "react-native",
46
+ "ios",
47
+ "android",
48
+ "push-notifications",
49
+ "pushpilot"
50
+ ],
51
+ "author": "PushPilot <sdk@pushpilot.ai> (https://pushpilot.ai)",
52
+ "license": "Apache-2.0",
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/pushpilotai/pushpilot-react-native.git"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/pushpilotai/pushpilot-react-native/issues"
59
+ },
60
+ "homepage": "https://pushpilot.ai",
61
+ "publishConfig": {
62
+ "registry": "https://registry.npmjs.org/",
63
+ "access": "public"
64
+ },
65
+ "devDependencies": {
66
+ "@eslint/compat": "^2.1.1",
67
+ "@eslint/eslintrc": "^3.3.7",
68
+ "@eslint/js": "^9.39.5",
69
+ "@react-native/babel-preset": "0.88.0",
70
+ "@react-native/eslint-config": "0.88.0",
71
+ "@react-native/jest-preset": "0.88.0",
72
+ "@types/jest": "^29.5.14",
73
+ "@types/react": "^19.2.0",
74
+ "del-cli": "^8.0.1",
75
+ "eslint": "^9.39.5",
76
+ "eslint-config-prettier": "^10.1.8",
77
+ "eslint-plugin-ft-flow": "^3.0.11",
78
+ "eslint-plugin-prettier": "^5.5.7",
79
+ "jest": "^29.7.0",
80
+ "prettier": "^3.9.10",
81
+ "react": "19.3.0",
82
+ "react-native": "0.88.0",
83
+ "react-native-builder-bob": "^0.44.1",
84
+ "turbo": "^2.11.10",
85
+ "typescript": "^6.0.3"
86
+ },
87
+ "peerDependencies": {
88
+ "react": "*",
89
+ "react-native": "*"
90
+ },
91
+ "workspaces": [
92
+ "example"
93
+ ],
94
+ "packageManager": "yarn@4.18.1",
95
+ "react-native-builder-bob": {
96
+ "source": "src",
97
+ "output": "lib",
98
+ "targets": [
99
+ [
100
+ "module",
101
+ {
102
+ "esm": true
103
+ }
104
+ ],
105
+ [
106
+ "typescript",
107
+ {
108
+ "project": "tsconfig.build.json"
109
+ }
110
+ ]
111
+ ]
112
+ },
113
+ "codegenConfig": {
114
+ "name": "RNPushPilotSpec",
115
+ "type": "modules",
116
+ "jsSrcsDir": "src",
117
+ "android": {
118
+ "javaPackageName": "ai.pushpilot.reactnative"
119
+ },
120
+ "ios": {
121
+ "modulesProvider": {
122
+ "PushPilot": "RNPushPilot"
123
+ }
124
+ }
125
+ },
126
+ "prettier": {
127
+ "quoteProps": "consistent",
128
+ "singleQuote": true,
129
+ "tabWidth": 2,
130
+ "trailingComma": "es5",
131
+ "useTabs": false
132
+ },
133
+ "create-react-native-library": {
134
+ "type": "turbo-module",
135
+ "languages": "kotlin-objc",
136
+ "tools": [
137
+ "eslint",
138
+ "jest"
139
+ ],
140
+ "version": "0.64.0"
141
+ },
142
+ "jest": {
143
+ "preset": "@react-native/jest-preset",
144
+ "modulePathIgnorePatterns": [
145
+ "<rootDir>/example/node_modules",
146
+ "<rootDir>/lib/"
147
+ ]
148
+ }
149
+ }
@@ -0,0 +1,26 @@
1
+ import {
2
+ TurboModuleRegistry,
3
+ type CodegenTypes,
4
+ type TurboModule,
5
+ } from 'react-native';
6
+
7
+ // The native module's shape; codegen turns it into the Kotlin and Objective-C interfaces.
8
+ export interface Spec extends TurboModule {
9
+ // Named `initialize` because Objective-C reserves methods starting with `init`.
10
+ initialize(sdkKey: string, baseUrl: string | null): void;
11
+ setPushToken(token: string | null): void;
12
+ setExternalUserId(id: string | null): void;
13
+ setAttributes(attributes: CodegenTypes.UnsafeObject): void;
14
+ setTopics(topics: string[]): void;
15
+ logout(): void;
16
+ forget(): void;
17
+ // Whether JavaScript has a link handler / tap handler; native sends those events only while it does.
18
+ setLinkHandlerEnabled(enabled: boolean): void;
19
+ setTapHandlerEnabled(enabled: boolean): void;
20
+ // Event-emitter hooks for NativeEventEmitter (events PushPilotLink and PushPilotTap).
21
+ addListener(eventName: string): void;
22
+ removeListeners(count: number): void;
23
+ }
24
+
25
+ // `get`, not `getEnforcing`: a missing native module (Expo Go, not rebuilt) must not throw at import.
26
+ export default TurboModuleRegistry.get<Spec>('PushPilot');
package/src/index.tsx ADDED
@@ -0,0 +1,138 @@
1
+ import {
2
+ NativeEventEmitter,
3
+ Platform,
4
+ type EmitterSubscription,
5
+ } from 'react-native';
6
+ import Native, { type Spec } from './NativePushPilot';
7
+
8
+ /** A custom attribute value; `null` removes the attribute. Pass dates as RFC 3339 strings. */
9
+ export type AttributeValue = string | number | boolean | null;
10
+
11
+ /** A tapped PushPilot push that opened the app. */
12
+ export interface TappedNotification {
13
+ /** PushPilot's id for the notification, the same one its events carry. */
14
+ notificationId: string;
15
+ /** The push's custom data; empty when it had none. */
16
+ data: Record<string, string>;
17
+ }
18
+
19
+ /** Options for `PushPilot.init`. */
20
+ export interface InitOptions {
21
+ /** Your own server, for self-hosted PushPilot. Defaults to https://pushpilot.ai. */
22
+ baseUrl?: string;
23
+ }
24
+
25
+ // Runs one native call, logging instead of throwing so the SDK can never crash the app.
26
+ function call(name: string, run: (native: Spec) => void): void {
27
+ try {
28
+ if (Native) {
29
+ run(Native);
30
+ } else {
31
+ console.warn(
32
+ `[PushPilot] ${name} ignored: the native module is not linked. Rebuild the app (Expo Go is not supported).`
33
+ );
34
+ }
35
+ } catch (e) {
36
+ console.warn(`[PushPilot] ${name} failed: ${String(e)}`);
37
+ }
38
+ }
39
+
40
+ // An APNs device token as hex: what iOS needs since 2.0. FCM tokens and FIDs contain other characters.
41
+ const APNS_HEX = /^(?:[0-9a-fA-F]{2})+$/;
42
+
43
+ type Events = { PushPilotLink: [string]; PushPilotTap: [TappedNotification] };
44
+
45
+ // JavaScript's current link and tap handlers, if any.
46
+ const subscriptions: { [E in keyof Events]?: EmitterSubscription } = {};
47
+
48
+ // Replaces the handler for one event and tells native whether one is set now.
49
+ function setHandler<E extends keyof Events>(
50
+ event: E,
51
+ handler: ((...args: Events[E]) => void) | null,
52
+ enable: (native: Spec, enabled: boolean) => void
53
+ ): void {
54
+ call(event === 'PushPilotLink' ? 'setLinkHandler' : 'setTapHandler', (n) => {
55
+ subscriptions[event]?.remove();
56
+ subscriptions[event] = handler
57
+ ? new NativeEventEmitter<Events>(n).addListener(event, handler)
58
+ : undefined;
59
+ enable(n, handler !== null);
60
+ });
61
+ }
62
+
63
+ const PushPilot = {
64
+ /** Starts the SDK with the app's public SDK key. Later calls are ignored; earlier calls are queued. */
65
+ init(sdkKey: string, options?: InitOptions): void {
66
+ call('init', (n) => n.initialize(sdkKey, options?.baseUrl ?? null));
67
+ },
68
+
69
+ /**
70
+ * Passes the push identifier; `null` clears it.
71
+ * - Android: the Firebase Installation ID from `register` / `onRegistered` where your Firebase
72
+ * library has it, otherwise the FCM token from `getToken` / `onTokenRefresh`. Pass only one kind.
73
+ * (Not PushPilot's own installation id.)
74
+ * - iOS: the APNs device token as hex, e.g. from expo-notifications' `getDevicePushTokenAsync()` or
75
+ * React Native Firebase's `getAPNSToken()`. Not needed when your AppDelegate forwards the token
76
+ * natively. Anything else (an FCM token or FID, the 1.x iOS path) is ignored with a warning.
77
+ */
78
+ setPushToken(token: string | null): void {
79
+ if (Platform.OS === 'ios' && token !== null && !APNS_HEX.test(token)) {
80
+ console.warn(
81
+ '[PushPilot] setPushToken ignored: on iOS pass the APNs device token as hex, not an FCM token or Firebase Installation ID.'
82
+ );
83
+ return;
84
+ }
85
+ call('setPushToken', (n) => n.setPushToken(token));
86
+ },
87
+
88
+ /** Links this installation to your own user id after login; `null` clears it. */
89
+ setExternalUserId(id: string | null): void {
90
+ call('setExternalUserId', (n) => n.setExternalUserId(id));
91
+ },
92
+
93
+ /** Merges custom attributes declared in the dashboard. */
94
+ setAttributes(attributes: Record<string, AttributeValue>): void {
95
+ call('setAttributes', (n) => n.setAttributes(attributes));
96
+ },
97
+
98
+ /** Replaces this installation's topic list. */
99
+ setTopics(topics: string[]): void {
100
+ call('setTopics', (n) => n.setTopics(topics));
101
+ },
102
+
103
+ /** Clears the external user id; the device stays registered and keeps receiving pushes. */
104
+ logout(): void {
105
+ call('logout', (n) => n.logout());
106
+ },
107
+
108
+ /** Deletes this installation from PushPilot and clears all local SDK state. */
109
+ forget(): void {
110
+ call('forget', (n) => n.forget());
111
+ },
112
+
113
+ /**
114
+ * Receives the link of each tapped PushPilot push (the button's link, or the push's own) instead of
115
+ * the system opening it, like the native SDKs' `setLinkHandler`; `null` lets the system open links
116
+ * again. Set it before `init` to also get the tap that launched the app; each link arrives once.
117
+ */
118
+ setLinkHandler(handler: ((url: string) => void) | null): void {
119
+ setHandler('PushPilotLink', handler, (n, on) =>
120
+ n.setLinkHandlerEnabled(on)
121
+ );
122
+ },
123
+
124
+ /**
125
+ * Receives the id and custom data of each tapped PushPilot push that opens the app; `null` stops it.
126
+ * Set it before `init` to also get the tap that launched the app; each tap arrives once.
127
+ */
128
+ setTapHandler(handler: ((tap: TappedNotification) => void) | null): void {
129
+ setHandler('PushPilotTap', handler, (n, on) => n.setTapHandlerEnabled(on));
130
+ },
131
+
132
+ /** True when a received push's data came from PushPilot. Same rule as the native SDKs; no native call. */
133
+ handleMessage(data: { [key: string]: unknown } | null | undefined): boolean {
134
+ return data?.source === 'pushpilot';
135
+ },
136
+ };
137
+
138
+ export default PushPilot;