@pushpilotai/react-native 0.0.0-stage → 2.2.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,152 @@
1
+ package ai.pushpilot.reactnative
2
+
3
+ import ai.pushpilot.sdk.PushPilot
4
+ import ai.pushpilot.sdk.TappedNotification
5
+ import android.app.Activity
6
+ import android.content.Intent
7
+ import android.util.Log
8
+ import android.view.Choreographer
9
+ import com.facebook.react.bridge.ActivityEventListener
10
+ import com.facebook.react.bridge.Arguments
11
+ import com.facebook.react.bridge.LifecycleEventListener
12
+ import com.facebook.react.bridge.Promise
13
+ import com.facebook.react.bridge.ReactApplicationContext
14
+ import com.facebook.react.bridge.ReadableArray
15
+ import com.facebook.react.bridge.ReadableMap
16
+ import com.facebook.react.bridge.UiThreadUtil
17
+ import com.facebook.react.modules.core.DeviceEventManagerModule
18
+ import com.facebook.react.uimanager.UIManagerHelper
19
+
20
+ /** Passes each JavaScript call straight to the native PushPilot SDK, and tapped pushes back to JavaScript. */
21
+ class PushPilotModule(reactContext: ReactApplicationContext) :
22
+ NativePushPilotSpec(reactContext), PushPilotTaps.Sink, ActivityEventListener, LifecycleEventListener {
23
+
24
+ @Volatile override var wantsLinks = false
25
+ @Volatile override var wantsTaps = false
26
+
27
+ init {
28
+ PushPilotTaps.start(reactContext)
29
+ reactContext.addActivityEventListener(this)
30
+ reactContext.addLifecycleEventListener(this)
31
+ }
32
+
33
+ /** Starts the SDK (a null base URL means its default server); then taps that waited for JavaScript go to it. */
34
+ override fun initialize(sdkKey: String, baseUrl: String?) {
35
+ PushPilot.init(reactApplicationContext.applicationContext, sdkKey, baseUrl)
36
+ // JavaScript's init runs after the activity resumed, which the SDK's lifecycle watcher missed.
37
+ reactApplicationContext.currentActivity?.let(PushPilot::setCurrentActivity)
38
+ PushPilotTaps.opened(reactApplicationContext.currentActivity?.intent)
39
+ PushPilotTaps.attach(this)
40
+ }
41
+
42
+ /** Same as [initialize]: the App Group is for iOS live activities and has no use on Android. */
43
+ override fun initializeWithAppGroup(sdkKey: String, baseUrl: String?, appGroup: String) =
44
+ initialize(sdkKey, baseUrl)
45
+
46
+ /** Android needs no URL handling for live activities (the SDK opens their links), so the URL comes back unchanged. */
47
+ override fun handleLiveActivityURL(url: String, promise: Promise) = promise.resolve(url)
48
+
49
+ /** Forwards the Firebase Installation ID or FCM token, or null to clear it. */
50
+ override fun setPushToken(token: String?) = PushPilot.setPushToken(token)
51
+
52
+ /** Forwards the app's user id, or null to clear it. */
53
+ override fun setExternalUserId(id: String?) = PushPilot.setExternalUserId(id)
54
+
55
+ /** Forwards the attributes as a plain map; the SDK checks the value types. */
56
+ override fun setAttributes(attributes: ReadableMap) = PushPilot.setAttributes(attributes.toHashMap())
57
+
58
+ /** Forwards the topic list; the SDK validates the names. */
59
+ override fun setTopics(topics: ReadableArray) =
60
+ PushPilot.setTopics(topics.toArrayList().filterIsInstance<String>())
61
+
62
+ /** Clears the external user id. */
63
+ override fun logout() = PushPilot.logout()
64
+
65
+ /** Deletes this installation and clears local SDK state. */
66
+ override fun forget() = PushPilot.forget()
67
+
68
+ /** Shows a cached in-app message on the main thread, where the SDK requires it, and resolves whether one is on screen. */
69
+ override fun showInApp(name: String, promise: Promise) {
70
+ UiThreadUtil.runOnUiThread { promise.resolve(PushPilot.showInApp(name)) }
71
+ }
72
+
73
+ /** Looks the view up by its React tag and names it for coach marks; a tag with no native view is logged and ignored. */
74
+ override fun markView(viewTag: Double, name: String) {
75
+ onNextFrame {
76
+ val tag = viewTag.toInt()
77
+ val view = UIManagerHelper.getUIManagerForReactTag(reactApplicationContext, tag)?.resolveView(tag)
78
+ if (view != null) PushPilot.markView(view, name)
79
+ else Log.w(TAG, "markView ignored: no native view for that ref (is it mounted, with collapsable={false}?)")
80
+ }
81
+ }
82
+
83
+ /** Forgets the named view; on the same queue as [markView], so an earlier mark never lands after it. */
84
+ override fun unmarkView(name: String) {
85
+ onNextFrame { PushPilot.unmarkView(name) }
86
+ }
87
+
88
+ /**
89
+ * Runs [block] on the UI thread in the next frame: Fabric mounts a commit's views from a frame callback it
90
+ * keeps posted ahead of this one, so a ref marked in an effect right after render has its native view.
91
+ */
92
+ private fun onNextFrame(block: () -> Unit) {
93
+ UiThreadUtil.runOnUiThread { Choreographer.getInstance().postFrameCallback { block() } }
94
+ }
95
+
96
+ /** JavaScript set (true) or cleared its link handler; the SDK's links come here only while one is set. */
97
+ override fun setLinkHandlerEnabled(enabled: Boolean) {
98
+ wantsLinks = enabled
99
+ PushPilotTaps.linkHandlerChanged(enabled)
100
+ }
101
+
102
+ /** JavaScript set (true) or cleared its tap handler. */
103
+ override fun setTapHandlerEnabled(enabled: Boolean) {
104
+ wantsTaps = enabled
105
+ }
106
+
107
+ /** Sends a tapped link to JavaScript's link handler. */
108
+ override fun sendLink(link: String) = emit("PushPilotLink", link)
109
+
110
+ /** Sends a tapped push's id and data to JavaScript's tap handler. */
111
+ override fun sendTap(tap: TappedNotification) {
112
+ val data = Arguments.createMap().apply { tap.data.forEach { (k, v) -> putString(k, v) } }
113
+ emit("PushPilotTap", Arguments.createMap().apply {
114
+ putString("notificationId", tap.notificationId)
115
+ putMap("data", data)
116
+ })
117
+ }
118
+
119
+ /** A tap on a push while the app was running opens the launcher activity with a new intent. */
120
+ override fun onNewIntent(intent: Intent) = PushPilotTaps.opened(intent)
121
+
122
+ /** Catches a tap that recreated the activity (a non-singleTask launcher) instead of sending a new intent. */
123
+ override fun onHostResume() = PushPilotTaps.opened(reactApplicationContext.currentActivity?.intent)
124
+
125
+ override fun onHostPause() = Unit
126
+ override fun onHostDestroy() = Unit
127
+ override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) = Unit
128
+
129
+ /** JavaScript is going away (reload or exit): taps wait for the next run. */
130
+ override fun invalidate() {
131
+ PushPilotTaps.detach(this)
132
+ reactApplicationContext.removeActivityEventListener(this)
133
+ reactApplicationContext.removeLifecycleEventListener(this)
134
+ super.invalidate()
135
+ }
136
+
137
+ /** NativeEventEmitter's bookkeeping; the handler flags above decide what is sent. */
138
+ override fun addListener(eventName: String) = Unit
139
+
140
+ /** NativeEventEmitter's bookkeeping. */
141
+ override fun removeListeners(count: Double) = Unit
142
+
143
+ /** Emits through React Native's device event emitter, which NativeEventEmitter listens to. */
144
+ private fun emit(name: String, payload: Any) {
145
+ reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java).emit(name, payload)
146
+ }
147
+
148
+ companion object {
149
+ const val NAME = NativePushPilotSpec.NAME
150
+ private const val TAG = "PushPilot"
151
+ }
152
+ }
@@ -0,0 +1,28 @@
1
+ package ai.pushpilot.reactnative
2
+
3
+ import com.facebook.react.BaseReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.module.model.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+
9
+ /** Registers the PushPilot module with React Native; found by autolinking. */
10
+ class PushPilotPackage : BaseReactPackage() {
11
+ /** Creates the module when React Native asks for it by name. */
12
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
13
+ if (name == PushPilotModule.NAME) PushPilotModule(reactContext) else null
14
+
15
+ /** Describes the module so React Native can load it lazily. */
16
+ override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
17
+ mapOf(
18
+ PushPilotModule.NAME to ReactModuleInfo(
19
+ name = PushPilotModule.NAME,
20
+ className = PushPilotModule.NAME,
21
+ canOverrideExistingModule = false,
22
+ needsEagerInit = false,
23
+ isCxxModule = false,
24
+ isTurboModule = true
25
+ )
26
+ )
27
+ }
28
+ }
@@ -0,0 +1,31 @@
1
+ package ai.pushpilot.reactnative
2
+
3
+ import android.content.ContentProvider
4
+ import android.content.ContentValues
5
+ import android.database.Cursor
6
+ import android.net.Uri
7
+
8
+ /**
9
+ * Runs when the app's process starts, before the `Application` and before the SDK handles a tap,
10
+ * so the tap that launches a killed app reaches [PushPilotTaps] while JavaScript is not loaded yet.
11
+ * The SDK starts itself from its own saved key; this only installs the link handler.
12
+ */
13
+ class PushPilotStartupProvider : ContentProvider() {
14
+
15
+ /** Never fails app startup. */
16
+ override fun onCreate(): Boolean {
17
+ try {
18
+ context?.let { PushPilotTaps.start(it) }
19
+ } catch (e: Exception) {
20
+ // Without the handler the SDK opens links itself, which is its default.
21
+ }
22
+ return true
23
+ }
24
+
25
+ // Not a real provider: it holds no data.
26
+ override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? = null
27
+ override fun getType(uri: Uri): String? = null
28
+ override fun insert(uri: Uri, values: ContentValues?): Uri? = null
29
+ override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?) = 0
30
+ override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?) = 0
31
+ }
@@ -0,0 +1,95 @@
1
+ package ai.pushpilot.reactnative
2
+
3
+ import ai.pushpilot.sdk.PushPilot
4
+ import ai.pushpilot.sdk.TappedNotification
5
+ import android.content.ActivityNotFoundException
6
+ import android.content.Context
7
+ import android.content.Intent
8
+ import android.net.Uri
9
+
10
+ /**
11
+ * Gets the links and data of tapped pushes to JavaScript exactly once, holding them while
12
+ * JavaScript is not running yet (a tap that launched a killed app, or a reload).
13
+ */
14
+ internal object PushPilotTaps {
15
+ private const val PREFS = "ai.pushpilot.reactnative"
16
+ private const val LINK_HANDLER = "link_handler"
17
+
18
+ /** Where events go once JavaScript has started; null until then and after a reload. */
19
+ interface Sink {
20
+ /** True when JavaScript has a link handler. */
21
+ val wantsLinks: Boolean
22
+ /** True when JavaScript has a tap handler. */
23
+ val wantsTaps: Boolean
24
+ fun sendLink(link: String)
25
+ fun sendTap(tap: TappedNotification)
26
+ }
27
+
28
+ private lateinit var app: Context
29
+ private var sink: Sink? = null
30
+ private val waiting = ArrayList<Any>() // Uri or TappedNotification, in tap order
31
+ private val seen = HashSet<String>() // notification ids already passed on in this process
32
+
33
+ // Holds or passes on the link; the SDK has already opened the app.
34
+ private val linkHandler = PushPilot.LinkHandler { link -> receive(link) }
35
+
36
+ /**
37
+ * At process start: takes the SDK's links only if the last JavaScript run had a link handler.
38
+ * Otherwise the SDK opens links itself, which skips opening the app first.
39
+ */
40
+ @Synchronized
41
+ fun start(context: Context) {
42
+ app = context.applicationContext
43
+ if (prefs().getBoolean(LINK_HANDLER, false)) PushPilot.setLinkHandler(linkHandler)
44
+ }
45
+
46
+ /** JavaScript set or cleared its link handler: applies now and to the next launch. */
47
+ @Synchronized
48
+ fun linkHandlerChanged(enabled: Boolean) {
49
+ prefs().edit().putBoolean(LINK_HANDLER, enabled).apply()
50
+ PushPilot.setLinkHandler(if (enabled) linkHandler else null)
51
+ }
52
+
53
+ /** The app was opened with [intent]: passes on the tap it carries, once per notification. */
54
+ @Synchronized
55
+ fun opened(intent: Intent?) {
56
+ val tap = PushPilot.tappedNotification(intent) ?: return
57
+ if (seen.add(tap.notificationId)) receive(tap)
58
+ }
59
+
60
+ /** JavaScript called init, after setting its handlers: it receives everything that waited. */
61
+ @Synchronized
62
+ fun attach(sink: Sink) {
63
+ this.sink = sink
64
+ val held = waiting.toList()
65
+ waiting.clear()
66
+ held.forEach { receive(it) }
67
+ }
68
+
69
+ /** JavaScript is going away: later taps wait for the next run. */
70
+ @Synchronized
71
+ fun detach(sink: Sink) {
72
+ if (this.sink === sink) this.sink = null
73
+ }
74
+
75
+ /** Sends one link or tap to JavaScript, or opens the link as the SDK would without a handler. */
76
+ @Synchronized
77
+ private fun receive(item: Any) {
78
+ val sink = sink ?: return run { waiting += item }
79
+ when (item) {
80
+ is Uri -> if (sink.wantsLinks) sink.sendLink(item.toString()) else view(item)
81
+ is TappedNotification -> if (sink.wantsTaps) sink.sendTap(item)
82
+ }
83
+ }
84
+
85
+ /** Opens [link] in whatever handles it; the app is already open if nothing does. */
86
+ private fun view(link: Uri) {
87
+ try {
88
+ app.startActivity(Intent(Intent.ACTION_VIEW, link).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
89
+ } catch (e: ActivityNotFoundException) {
90
+ // Nothing opens it; the SDK has already opened the app.
91
+ }
92
+ }
93
+
94
+ private fun prefs() = app.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
95
+ }
@@ -0,0 +1,12 @@
1
+ #import <React/RCTEventEmitter.h>
2
+
3
+ // An event emitter so the links of tapped pushes can reach JavaScript's setLinkHandler.
4
+ #ifdef RCT_NEW_ARCH_ENABLED
5
+ #import <RNPushPilotSpec/RNPushPilotSpec.h>
6
+
7
+ @interface RNPushPilot : RCTEventEmitter <NativePushPilotSpec>
8
+ #else
9
+ @interface RNPushPilot : RCTEventEmitter <RCTBridgeModule>
10
+ #endif
11
+
12
+ @end
@@ -0,0 +1,237 @@
1
+ #import "RNPushPilot.h"
2
+
3
+ #import <UIKit/UIKit.h>
4
+
5
+ // The Swift half of this pod, which can import the Swift-only PushPilot SDK.
6
+ #if __has_include(<PushPilotReactNative/PushPilotReactNative-Swift.h>)
7
+ #import <PushPilotReactNative/PushPilotReactNative-Swift.h>
8
+ #else
9
+ #import "PushPilotReactNative-Swift.h"
10
+ #endif
11
+
12
+ static NSString *const RNPushPilotLinkEvent = @"PushPilotLink";
13
+ static NSString *const RNPushPilotTapEvent = @"PushPilotTap";
14
+
15
+ // Links (NSURL) and taps (NSDictionary) that arrived before JavaScript called init, in order. Main thread only.
16
+ static NSMutableArray *waitingTaps;
17
+ // The module whose JavaScript has called init; nil until then and after a reload. Main thread only.
18
+ static __weak RNPushPilot *tapModule;
19
+
20
+ @interface RNPushPilot ()
21
+ - (void)deliver:(id)item;
22
+ @end
23
+
24
+ // Sends a tapped push's link or data on, or keeps it until JavaScript has called init.
25
+ static void RNPushPilotReceive(id item)
26
+ {
27
+ RNPushPilot *module = tapModule;
28
+ if (module) {
29
+ [module deliver:item];
30
+ } else {
31
+ [waitingTaps addObject:item];
32
+ }
33
+ }
34
+
35
+ // Installs the tap delegate and the link handler when the app starts, with no AppDelegate code.
36
+ @interface RNPushPilotLaunch : NSObject
37
+ @end
38
+
39
+ @implementation RNPushPilotLaunch
40
+
41
+ // Runs as the app loads; the work waits for launch to finish, before iOS delivers a tap that launched the app.
42
+ + (void)load
43
+ {
44
+ waitingTaps = [NSMutableArray new];
45
+ [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification
46
+ object:nil
47
+ queue:[NSOperationQueue mainQueue]
48
+ usingBlock:^(NSNotification *note) {
49
+ [RNPushPilotBridge setLinkHandler:^(NSURL *url) {
50
+ RNPushPilotReceive(url);
51
+ }];
52
+ [RNPushPilotNotifications setTapHandler:^(NSDictionary *tap) {
53
+ RNPushPilotReceive(tap);
54
+ }];
55
+ [RNPushPilotNotifications installAtLaunch];
56
+ }];
57
+ }
58
+
59
+ @end
60
+
61
+ // Passes each JavaScript call straight to the native PushPilot SDK, which does all the work.
62
+ // RCT_EXPORT_METHOD keeps the module working on the old architecture too.
63
+ @implementation RNPushPilot {
64
+ BOOL _wantsLinks; // main thread only
65
+ BOOL _wantsTaps; // main thread only
66
+ }
67
+
68
+ RCT_EXPORT_MODULE(PushPilot)
69
+
70
+ // Nothing here needs the main thread at creation.
71
+ + (BOOL)requiresMainQueueSetup
72
+ {
73
+ return NO;
74
+ }
75
+
76
+ // A tapped push's link, for setLinkHandler, and its id and data, for setTapHandler.
77
+ - (NSArray<NSString *> *)supportedEvents
78
+ {
79
+ return @[ RNPushPilotLinkEvent, RNPushPilotTapEvent ];
80
+ }
81
+
82
+ // JavaScript is going away (reload): taps wait for the next module's init.
83
+ - (void)invalidate
84
+ {
85
+ [super invalidate];
86
+ dispatch_async(dispatch_get_main_queue(), ^{
87
+ if (tapModule == self) {
88
+ tapModule = nil;
89
+ }
90
+ });
91
+ }
92
+
93
+ // Sends a link to JavaScript's handler or opens it as the SDK would without one; sends a tap to its
94
+ // handler or drops it. Main thread.
95
+ - (void)deliver:(id)item
96
+ {
97
+ if ([item isKindOfClass:[NSURL class]]) {
98
+ NSURL *url = item;
99
+ if (_wantsLinks) {
100
+ [self sendEventWithName:RNPushPilotLinkEvent body:url.absoluteString];
101
+ } else {
102
+ [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
103
+ }
104
+ } else if (_wantsTaps) {
105
+ [self sendEventWithName:RNPushPilotTapEvent body:item];
106
+ }
107
+ }
108
+
109
+ // JavaScript set (YES) or cleared its link handler.
110
+ RCT_EXPORT_METHOD(setLinkHandlerEnabled:(BOOL)enabled)
111
+ {
112
+ dispatch_async(dispatch_get_main_queue(), ^{
113
+ self->_wantsLinks = enabled;
114
+ });
115
+ }
116
+
117
+ // JavaScript set (YES) or cleared its tap handler.
118
+ RCT_EXPORT_METHOD(setTapHandlerEnabled:(BOOL)enabled)
119
+ {
120
+ dispatch_async(dispatch_get_main_queue(), ^{
121
+ self->_wantsTaps = enabled;
122
+ });
123
+ }
124
+
125
+ // Starts the SDK; a nil base URL means the SDK's default server. Taps that waited for JavaScript go on now,
126
+ // to the handlers JavaScript set before init.
127
+ RCT_EXPORT_METHOD(initialize:(NSString *)sdkKey baseUrl:(NSString *)baseUrl)
128
+ {
129
+ [self startWithSdkKey:sdkKey baseUrl:baseUrl appGroup:nil];
130
+ }
131
+
132
+ // Starts the SDK as initialize does, with the App Group shared with the app's Widget Extension.
133
+ RCT_EXPORT_METHOD(initializeWithAppGroup:(NSString *)sdkKey baseUrl:(NSString *)baseUrl appGroup:(NSString *)appGroup)
134
+ {
135
+ [self startWithSdkKey:sdkKey baseUrl:baseUrl appGroup:appGroup];
136
+ }
137
+
138
+ // Records a live activity tap and resolves to the activity's own link (nil when it has none); other URLs unchanged.
139
+ RCT_EXPORT_METHOD(handleLiveActivityURL:(NSString *)url
140
+ resolve:(RCTPromiseResolveBlock)resolve
141
+ reject:(RCTPromiseRejectBlock)reject)
142
+ {
143
+ resolve([RNPushPilotBridge handleLiveActivityURL:url]);
144
+ }
145
+
146
+ // Shows a cached in-app message on the main thread, where the SDK requires it, and resolves whether one is on screen.
147
+ RCT_EXPORT_METHOD(showInApp:(NSString *)name
148
+ resolve:(RCTPromiseResolveBlock)resolve
149
+ reject:(RCTPromiseRejectBlock)reject)
150
+ {
151
+ dispatch_async(dispatch_get_main_queue(), ^{
152
+ resolve(@([RNPushPilotBridge showInApp:name]));
153
+ });
154
+ }
155
+
156
+ // Looks the view up by its React tag on the main thread, after views already queued for mounting, and names it
157
+ // for coach marks. A tag with no native view is logged and ignored.
158
+ RCT_EXPORT_METHOD(markView:(double)viewTag name:(NSString *)name)
159
+ {
160
+ [self.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *registry) {
161
+ UIView *view = [registry viewForReactTag:@(viewTag)];
162
+ if (view) {
163
+ [RNPushPilotBridge markView:view name:name];
164
+ } else {
165
+ NSLog(@"[PushPilot] markView ignored: no native view for that ref (is it mounted, with collapsable={false}?)");
166
+ }
167
+ }];
168
+ }
169
+
170
+ // Forgets the named view; on the main queue like markView, so an earlier mark never lands after it.
171
+ RCT_EXPORT_METHOD(unmarkView:(NSString *)name)
172
+ {
173
+ dispatch_async(dispatch_get_main_queue(), ^{
174
+ [RNPushPilotBridge unmarkViewWithName:name];
175
+ });
176
+ }
177
+
178
+ // The work of both initialize calls; a nil App Group means none.
179
+ - (void)startWithSdkKey:(NSString *)sdkKey baseUrl:(NSString *)baseUrl appGroup:(NSString *)appGroup
180
+ {
181
+ [RNPushPilotBridge startWithSdkKey:sdkKey baseUrl:baseUrl appGroup:appGroup];
182
+ dispatch_async(dispatch_get_main_queue(), ^{
183
+ tapModule = self;
184
+ NSArray *waiting = [waitingTaps copy];
185
+ [waitingTaps removeAllObjects];
186
+ for (id item in waiting) {
187
+ [self deliver:item];
188
+ }
189
+ });
190
+ }
191
+
192
+ // Forwards the APNs device token as hex, or nil to clear it.
193
+ RCT_EXPORT_METHOD(setPushToken:(NSString *)token)
194
+ {
195
+ [RNPushPilotBridge setPushToken:token];
196
+ }
197
+
198
+ // Forwards the app's user id, or nil to clear it.
199
+ RCT_EXPORT_METHOD(setExternalUserId:(NSString *)externalUserId)
200
+ {
201
+ [RNPushPilotBridge setExternalUserId:externalUserId];
202
+ }
203
+
204
+ // Forwards the attributes; the SDK checks the value types.
205
+ RCT_EXPORT_METHOD(setAttributes:(NSDictionary *)attributes)
206
+ {
207
+ [RNPushPilotBridge setAttributes:attributes];
208
+ }
209
+
210
+ // Forwards the topic list; the SDK validates the names.
211
+ RCT_EXPORT_METHOD(setTopics:(NSArray *)topics)
212
+ {
213
+ [RNPushPilotBridge setTopics:topics];
214
+ }
215
+
216
+ // Clears the external user id.
217
+ RCT_EXPORT_METHOD(logout)
218
+ {
219
+ [RNPushPilotBridge logout];
220
+ }
221
+
222
+ // Deletes this installation and clears local SDK state.
223
+ RCT_EXPORT_METHOD(forget)
224
+ {
225
+ [RNPushPilotBridge forget];
226
+ }
227
+
228
+ #ifdef RCT_NEW_ARCH_ENABLED
229
+ // Hands React Native the codegen-generated JSI binding for this module.
230
+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
231
+ (const facebook::react::ObjCTurboModule::InitParams &)params
232
+ {
233
+ return std::make_shared<facebook::react::NativePushPilotSpecJSI>(params);
234
+ }
235
+ #endif
236
+
237
+ @end
@@ -0,0 +1,90 @@
1
+ import Foundation
2
+ import PushPilot
3
+ import UIKit
4
+ import UserNotifications
5
+
6
+ /// Calls the Swift-only PushPilot SDK for the Objective-C++ module, which cannot import it directly.
7
+ /// `setDeviceToken` and `handleNotificationResponse` are also for the app's own AppDelegate.
8
+ @objc(RNPushPilotBridge)
9
+ public final class RNPushPilotBridge: NSObject {
10
+ /// Starts the SDK. A base URL that is not a URL is logged and ignored rather than silently using the default server.
11
+ /// `appGroup` is the App Group shared with the Widget Extension, or nil.
12
+ @objc public static func start(sdkKey: String, baseUrl: String?, appGroup: String?) {
13
+ guard let baseUrl else { return PushPilot.initialize(sdkKey: sdkKey, appGroup: appGroup) }
14
+ guard let url = URL(string: baseUrl) else { return NSLog("[PushPilot] init ignored: baseUrl is not a URL") }
15
+ PushPilot.initialize(sdkKey: sdkKey, baseUrl: url, appGroup: appGroup)
16
+ }
17
+
18
+ /// For a URL the app opened: a live activity tap is recorded and its own link returned (nil when it has
19
+ /// none). Any other URL, including one that does not parse, comes back unchanged.
20
+ @objc public static func handleLiveActivityURL(_ url: String) -> String? {
21
+ guard let parsed = URL(string: url) else { return url }
22
+ return PushPilot.handleLiveActivityURL(parsed)?.absoluteString
23
+ }
24
+
25
+ /// Call from `application(_:didRegisterForRemoteNotificationsWithDeviceToken:)` with the token as iOS gives it.
26
+ @objc public static func setDeviceToken(_ deviceToken: Data) {
27
+ PushPilot.setPushToken(deviceToken)
28
+ }
29
+
30
+ /// Forwards the APNs device token from JavaScript as hex; nil clears it. FCM tokens and Firebase
31
+ /// Installation IDs (the 1.x iOS path) are logged and ignored, leaving the stored token as it is.
32
+ @objc public static func setPushToken(_ token: String?) {
33
+ guard let token else { return PushPilot.setPushToken(nil) }
34
+ guard let data = RNPushPilotToken.data(fromHex: token) else {
35
+ return NSLog("[PushPilot] setPushToken ignored: on iOS pass the APNs device token as hex, not an FCM token or Firebase Installation ID")
36
+ }
37
+ PushPilot.setPushToken(data)
38
+ }
39
+
40
+ /// Forwards the user id. The iOS SDK takes no nil here, so nil clears the id with `logout`, as on Android.
41
+ @objc public static func setExternalUserId(_ id: String?) {
42
+ if let id { PushPilot.setExternalUserId(id) } else { PushPilot.logout() }
43
+ }
44
+
45
+ /// Forwards the attributes; JavaScript null arrives as NSNull, which the SDK treats as remove.
46
+ @objc public static func setAttributes(_ attributes: [String: Any]) {
47
+ PushPilot.setAttributes(attributes)
48
+ }
49
+
50
+ /// Forwards the topic list, keeping only strings.
51
+ @objc public static func setTopics(_ topics: [Any]) {
52
+ PushPilot.setTopics(topics.compactMap { $0 as? String })
53
+ }
54
+
55
+ /// Clears the external user id.
56
+ @objc public static func logout() {
57
+ PushPilot.logout()
58
+ }
59
+
60
+ /// Deletes this installation and clears local SDK state.
61
+ @objc public static func forget() {
62
+ PushPilot.forget()
63
+ }
64
+
65
+ /// Only for an app whose own notification-center delegate replaces the wrapper's after launch:
66
+ /// call it from that delegate's `didReceive`. Returns false for pushes not from PushPilot.
67
+ @objc public static func handleNotificationResponse(_ response: UNNotificationResponse) -> Bool {
68
+ RNPushPilotNotifications.handle(response)
69
+ }
70
+
71
+ /// Shows the first eligible cached in-app message with this trigger name; returns whether one is on screen.
72
+ @MainActor @objc public static func showInApp(_ name: String) -> Bool {
73
+ PushPilot.showInApp(name)
74
+ }
75
+
76
+ /// Names a view for coach-mark messages whose target is `name`; the SDK holds it weakly.
77
+ @MainActor @objc public static func markView(_ view: UIView, name: String) {
78
+ PushPilot.markView(view, name: name)
79
+ }
80
+
81
+ /// Forgets the view marked with `name`.
82
+ @MainActor @objc public static func unmarkView(name: String) {
83
+ PushPilot.unmarkView(name: name)
84
+ }
85
+
86
+ /// Sends the links of tapped PushPilot pushes to `handler` on the main thread instead of opening them.
87
+ @objc public static func setLinkHandler(_ handler: @escaping (URL) -> Void) {
88
+ PushPilot.setLinkHandler(handler)
89
+ }
90
+ }