@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.
- package/LICENSE +201 -0
- package/PushPilotReactNative.podspec +27 -0
- package/README.md +314 -2
- package/android/build.gradle +38 -0
- package/android/src/main/AndroidManifest.xml +24 -0
- package/android/src/main/java/ai/pushpilot/reactnative/PushPilotMessageReceiver.kt +42 -0
- package/android/src/main/java/ai/pushpilot/reactnative/PushPilotModule.kt +109 -0
- package/android/src/main/java/ai/pushpilot/reactnative/PushPilotPackage.kt +28 -0
- package/android/src/main/java/ai/pushpilot/reactnative/PushPilotStartupProvider.kt +31 -0
- package/android/src/main/java/ai/pushpilot/reactnative/PushPilotTaps.kt +95 -0
- package/ios/RNPushPilot.h +12 -0
- package/ios/RNPushPilot.mm +185 -0
- package/ios/RNPushPilotBridge.swift +66 -0
- package/ios/RNPushPilotNotifications.swift +96 -0
- package/ios/RNPushPilotToken.swift +29 -0
- package/lib/module/NativePushPilot.js +9 -0
- package/lib/module/NativePushPilot.js.map +1 -0
- package/lib/module/index.js +100 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativePushPilot.d.ts +17 -0
- package/lib/typescript/src/NativePushPilot.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +55 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +147 -4
- package/src/NativePushPilot.ts +26 -0
- package/src/index.tsx +138 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
package ai.pushpilot.reactnative
|
|
2
|
+
|
|
3
|
+
import ai.pushpilot.sdk.PushPilot
|
|
4
|
+
import android.content.BroadcastReceiver
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.content.Intent
|
|
7
|
+
|
|
8
|
+
/** Hands every FCM data message to the native SDK, which shows PushPilot pushes and ignores the rest. */
|
|
9
|
+
class PushPilotMessageReceiver : BroadcastReceiver() {
|
|
10
|
+
|
|
11
|
+
/** Runs `handleMessage` off the main thread, keeping the broadcast alive while the image downloads. */
|
|
12
|
+
override fun onReceive(context: Context, intent: Intent) {
|
|
13
|
+
val bundle = intent.extras ?: return
|
|
14
|
+
@Suppress("DEPRECATION") // Bundle.get is the only untyped getter; FCM's data values are all strings.
|
|
15
|
+
val data = messageData(bundle.keySet().associateWith { bundle.get(it) }) ?: return
|
|
16
|
+
val app = context.applicationContext
|
|
17
|
+
val pending = goAsync()
|
|
18
|
+
Thread {
|
|
19
|
+
try {
|
|
20
|
+
PushPilot.handleMessage(app, data)
|
|
21
|
+
} catch (e: Exception) {
|
|
22
|
+
// The SDK catches its own errors; this only keeps an unexpected one from killing the process.
|
|
23
|
+
} finally {
|
|
24
|
+
pending.finish()
|
|
25
|
+
}
|
|
26
|
+
}.start()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
companion object {
|
|
30
|
+
private val FCM_KEYS = setOf("from", "message_type", "collapse_key")
|
|
31
|
+
|
|
32
|
+
/** The message's data, as FCM's `RemoteMessage.getData()` builds it; null for deletion and send-status broadcasts. */
|
|
33
|
+
@JvmStatic
|
|
34
|
+
fun messageData(extras: Map<String, Any?>): Map<String, String>? {
|
|
35
|
+
if ((extras["message_type"] as? String ?: "gcm") != "gcm") return null
|
|
36
|
+
return extras.entries
|
|
37
|
+
.filter { (key, _) -> !key.startsWith("google.") && !key.startsWith("gcm.") && key !in FCM_KEYS }
|
|
38
|
+
.mapNotNull { (key, value) -> (value as? String)?.let { key to it } }
|
|
39
|
+
.toMap()
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
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 com.facebook.react.bridge.ActivityEventListener
|
|
8
|
+
import com.facebook.react.bridge.Arguments
|
|
9
|
+
import com.facebook.react.bridge.LifecycleEventListener
|
|
10
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
11
|
+
import com.facebook.react.bridge.ReadableArray
|
|
12
|
+
import com.facebook.react.bridge.ReadableMap
|
|
13
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
14
|
+
|
|
15
|
+
/** Passes each JavaScript call straight to the native PushPilot SDK, and tapped pushes back to JavaScript. */
|
|
16
|
+
class PushPilotModule(reactContext: ReactApplicationContext) :
|
|
17
|
+
NativePushPilotSpec(reactContext), PushPilotTaps.Sink, ActivityEventListener, LifecycleEventListener {
|
|
18
|
+
|
|
19
|
+
@Volatile override var wantsLinks = false
|
|
20
|
+
@Volatile override var wantsTaps = false
|
|
21
|
+
|
|
22
|
+
init {
|
|
23
|
+
PushPilotTaps.start(reactContext)
|
|
24
|
+
reactContext.addActivityEventListener(this)
|
|
25
|
+
reactContext.addLifecycleEventListener(this)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Starts the SDK (a null base URL means its default server); then taps that waited for JavaScript go to it. */
|
|
29
|
+
override fun initialize(sdkKey: String, baseUrl: String?) {
|
|
30
|
+
PushPilot.init(reactApplicationContext.applicationContext, sdkKey, baseUrl)
|
|
31
|
+
PushPilotTaps.opened(reactApplicationContext.currentActivity?.intent)
|
|
32
|
+
PushPilotTaps.attach(this)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Forwards the Firebase Installation ID or FCM token, or null to clear it. */
|
|
36
|
+
override fun setPushToken(token: String?) = PushPilot.setPushToken(token)
|
|
37
|
+
|
|
38
|
+
/** Forwards the app's user id, or null to clear it. */
|
|
39
|
+
override fun setExternalUserId(id: String?) = PushPilot.setExternalUserId(id)
|
|
40
|
+
|
|
41
|
+
/** Forwards the attributes as a plain map; the SDK checks the value types. */
|
|
42
|
+
override fun setAttributes(attributes: ReadableMap) = PushPilot.setAttributes(attributes.toHashMap())
|
|
43
|
+
|
|
44
|
+
/** Forwards the topic list; the SDK validates the names. */
|
|
45
|
+
override fun setTopics(topics: ReadableArray) =
|
|
46
|
+
PushPilot.setTopics(topics.toArrayList().filterIsInstance<String>())
|
|
47
|
+
|
|
48
|
+
/** Clears the external user id. */
|
|
49
|
+
override fun logout() = PushPilot.logout()
|
|
50
|
+
|
|
51
|
+
/** Deletes this installation and clears local SDK state. */
|
|
52
|
+
override fun forget() = PushPilot.forget()
|
|
53
|
+
|
|
54
|
+
/** JavaScript set (true) or cleared its link handler; the SDK's links come here only while one is set. */
|
|
55
|
+
override fun setLinkHandlerEnabled(enabled: Boolean) {
|
|
56
|
+
wantsLinks = enabled
|
|
57
|
+
PushPilotTaps.linkHandlerChanged(enabled)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** JavaScript set (true) or cleared its tap handler. */
|
|
61
|
+
override fun setTapHandlerEnabled(enabled: Boolean) {
|
|
62
|
+
wantsTaps = enabled
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Sends a tapped link to JavaScript's link handler. */
|
|
66
|
+
override fun sendLink(link: String) = emit("PushPilotLink", link)
|
|
67
|
+
|
|
68
|
+
/** Sends a tapped push's id and data to JavaScript's tap handler. */
|
|
69
|
+
override fun sendTap(tap: TappedNotification) {
|
|
70
|
+
val data = Arguments.createMap().apply { tap.data.forEach { (k, v) -> putString(k, v) } }
|
|
71
|
+
emit("PushPilotTap", Arguments.createMap().apply {
|
|
72
|
+
putString("notificationId", tap.notificationId)
|
|
73
|
+
putMap("data", data)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A tap on a push while the app was running opens the launcher activity with a new intent. */
|
|
78
|
+
override fun onNewIntent(intent: Intent) = PushPilotTaps.opened(intent)
|
|
79
|
+
|
|
80
|
+
/** Catches a tap that recreated the activity (a non-singleTask launcher) instead of sending a new intent. */
|
|
81
|
+
override fun onHostResume() = PushPilotTaps.opened(reactApplicationContext.currentActivity?.intent)
|
|
82
|
+
|
|
83
|
+
override fun onHostPause() = Unit
|
|
84
|
+
override fun onHostDestroy() = Unit
|
|
85
|
+
override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) = Unit
|
|
86
|
+
|
|
87
|
+
/** JavaScript is going away (reload or exit): taps wait for the next run. */
|
|
88
|
+
override fun invalidate() {
|
|
89
|
+
PushPilotTaps.detach(this)
|
|
90
|
+
reactApplicationContext.removeActivityEventListener(this)
|
|
91
|
+
reactApplicationContext.removeLifecycleEventListener(this)
|
|
92
|
+
super.invalidate()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** NativeEventEmitter's bookkeeping; the handler flags above decide what is sent. */
|
|
96
|
+
override fun addListener(eventName: String) = Unit
|
|
97
|
+
|
|
98
|
+
/** NativeEventEmitter's bookkeeping. */
|
|
99
|
+
override fun removeListeners(count: Double) = Unit
|
|
100
|
+
|
|
101
|
+
/** Emits through React Native's device event emitter, which NativeEventEmitter listens to. */
|
|
102
|
+
private fun emit(name: String, payload: Any) {
|
|
103
|
+
reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java).emit(name, payload)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
companion object {
|
|
107
|
+
const val NAME = NativePushPilotSpec.NAME
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -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,185 @@
|
|
|
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
|
+
[RNPushPilotBridge startWithSdkKey:sdkKey baseUrl:baseUrl];
|
|
130
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
131
|
+
tapModule = self;
|
|
132
|
+
NSArray *waiting = [waitingTaps copy];
|
|
133
|
+
[waitingTaps removeAllObjects];
|
|
134
|
+
for (id item in waiting) {
|
|
135
|
+
[self deliver:item];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Forwards the APNs device token as hex, or nil to clear it.
|
|
141
|
+
RCT_EXPORT_METHOD(setPushToken:(NSString *)token)
|
|
142
|
+
{
|
|
143
|
+
[RNPushPilotBridge setPushToken:token];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Forwards the app's user id, or nil to clear it.
|
|
147
|
+
RCT_EXPORT_METHOD(setExternalUserId:(NSString *)externalUserId)
|
|
148
|
+
{
|
|
149
|
+
[RNPushPilotBridge setExternalUserId:externalUserId];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Forwards the attributes; the SDK checks the value types.
|
|
153
|
+
RCT_EXPORT_METHOD(setAttributes:(NSDictionary *)attributes)
|
|
154
|
+
{
|
|
155
|
+
[RNPushPilotBridge setAttributes:attributes];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Forwards the topic list; the SDK validates the names.
|
|
159
|
+
RCT_EXPORT_METHOD(setTopics:(NSArray *)topics)
|
|
160
|
+
{
|
|
161
|
+
[RNPushPilotBridge setTopics:topics];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Clears the external user id.
|
|
165
|
+
RCT_EXPORT_METHOD(logout)
|
|
166
|
+
{
|
|
167
|
+
[RNPushPilotBridge logout];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Deletes this installation and clears local SDK state.
|
|
171
|
+
RCT_EXPORT_METHOD(forget)
|
|
172
|
+
{
|
|
173
|
+
[RNPushPilotBridge forget];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
177
|
+
// Hands React Native the codegen-generated JSI binding for this module.
|
|
178
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
179
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
180
|
+
{
|
|
181
|
+
return std::make_shared<facebook::react::NativePushPilotSpecJSI>(params);
|
|
182
|
+
}
|
|
183
|
+
#endif
|
|
184
|
+
|
|
185
|
+
@end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import PushPilot
|
|
3
|
+
import UserNotifications
|
|
4
|
+
|
|
5
|
+
/// Calls the Swift-only PushPilot SDK for the Objective-C++ module, which cannot import it directly.
|
|
6
|
+
/// `setDeviceToken` and `handleNotificationResponse` are also for the app's own AppDelegate.
|
|
7
|
+
@objc(RNPushPilotBridge)
|
|
8
|
+
public final class RNPushPilotBridge: NSObject {
|
|
9
|
+
/// Starts the SDK. A base URL that is not a URL is logged and ignored rather than silently using the default server.
|
|
10
|
+
@objc public static func start(sdkKey: String, baseUrl: String?) {
|
|
11
|
+
guard let baseUrl else { return PushPilot.initialize(sdkKey: sdkKey) }
|
|
12
|
+
guard let url = URL(string: baseUrl) else { return NSLog("[PushPilot] init ignored: baseUrl is not a URL") }
|
|
13
|
+
PushPilot.initialize(sdkKey: sdkKey, baseUrl: url)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/// Call from `application(_:didRegisterForRemoteNotificationsWithDeviceToken:)` with the token as iOS gives it.
|
|
17
|
+
@objc public static func setDeviceToken(_ deviceToken: Data) {
|
|
18
|
+
PushPilot.setPushToken(deviceToken)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// Forwards the APNs device token from JavaScript as hex; nil clears it. FCM tokens and Firebase
|
|
22
|
+
/// Installation IDs (the 1.x iOS path) are logged and ignored, leaving the stored token as it is.
|
|
23
|
+
@objc public static func setPushToken(_ token: String?) {
|
|
24
|
+
guard let token else { return PushPilot.setPushToken(nil) }
|
|
25
|
+
guard let data = RNPushPilotToken.data(fromHex: token) else {
|
|
26
|
+
return NSLog("[PushPilot] setPushToken ignored: on iOS pass the APNs device token as hex, not an FCM token or Firebase Installation ID")
|
|
27
|
+
}
|
|
28
|
+
PushPilot.setPushToken(data)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// Forwards the user id. The iOS SDK takes no nil here, so nil clears the id with `logout`, as on Android.
|
|
32
|
+
@objc public static func setExternalUserId(_ id: String?) {
|
|
33
|
+
if let id { PushPilot.setExternalUserId(id) } else { PushPilot.logout() }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// Forwards the attributes; JavaScript null arrives as NSNull, which the SDK treats as remove.
|
|
37
|
+
@objc public static func setAttributes(_ attributes: [String: Any]) {
|
|
38
|
+
PushPilot.setAttributes(attributes)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// Forwards the topic list, keeping only strings.
|
|
42
|
+
@objc public static func setTopics(_ topics: [Any]) {
|
|
43
|
+
PushPilot.setTopics(topics.compactMap { $0 as? String })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/// Clears the external user id.
|
|
47
|
+
@objc public static func logout() {
|
|
48
|
+
PushPilot.logout()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// Deletes this installation and clears local SDK state.
|
|
52
|
+
@objc public static func forget() {
|
|
53
|
+
PushPilot.forget()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Only for an app whose own notification-center delegate replaces the wrapper's after launch:
|
|
57
|
+
/// call it from that delegate's `didReceive`. Returns false for pushes not from PushPilot.
|
|
58
|
+
@objc public static func handleNotificationResponse(_ response: UNNotificationResponse) -> Bool {
|
|
59
|
+
RNPushPilotNotifications.handle(response)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// Sends the links of tapped PushPilot pushes to `handler` on the main thread instead of opening them.
|
|
63
|
+
@objc public static func setLinkHandler(_ handler: @escaping (URL) -> Void) {
|
|
64
|
+
PushPilot.setLinkHandler(handler)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import PushPilot
|
|
2
|
+
import UIKit
|
|
3
|
+
import UserNotifications
|
|
4
|
+
|
|
5
|
+
/// Gets taps on notifications to the PushPilot SDK by joining the app's notification-center delegate
|
|
6
|
+
/// chain, the way React Native Firebase and expo-notifications do, so the app adds no tap code.
|
|
7
|
+
@objc(RNPushPilotNotifications)
|
|
8
|
+
public final class RNPushPilotNotifications: NSObject {
|
|
9
|
+
private static let delegate = NotificationDelegate()
|
|
10
|
+
/// Receives the id and custom data of each tap that opens the app; set once at launch. Main thread.
|
|
11
|
+
static var tapHandler: (([String: Any]) -> Void)?
|
|
12
|
+
|
|
13
|
+
/// Sets where taps go; called by the module's launch hook before `installAtLaunch`.
|
|
14
|
+
@objc public static func setTapHandler(_ handler: @escaping ([String: Any]) -> Void) {
|
|
15
|
+
tapHandler = handler
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/// Ids of the taps already sent to `tapHandler`, so a tap forwarded twice is reported once. Main thread.
|
|
19
|
+
private static var reported: Set<String> = []
|
|
20
|
+
|
|
21
|
+
/// Hands a tap to the SDK, which records the click and opens or passes on the link, then reports it to
|
|
22
|
+
/// `tapHandler` if it opened the app. Returns false for pushes not from PushPilot. Main thread.
|
|
23
|
+
static func handle(_ response: UNNotificationResponse) -> Bool {
|
|
24
|
+
guard PushPilot.handleNotificationResponse(response) else { return false }
|
|
25
|
+
if let tap = tap(response.notification.request.content.userInfo, action: response.actionIdentifier),
|
|
26
|
+
let id = tap["notificationId"] as? String, reported.insert(id).inserted {
|
|
27
|
+
tapHandler?(tap)
|
|
28
|
+
}
|
|
29
|
+
return true
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// The id and custom data of a tapped PushPilot push, as Android's `TappedNotification` has them. Nil
|
|
33
|
+
/// without an id, and for a tap that leaves the app closed: a dismissal or a button without a `url`.
|
|
34
|
+
private static func tap(_ userInfo: [AnyHashable: Any], action: String) -> [String: Any]? {
|
|
35
|
+
guard let id = userInfo["notification_id"] as? String, !id.isEmpty else { return nil }
|
|
36
|
+
if action != UNNotificationDefaultActionIdentifier {
|
|
37
|
+
let button = (userInfo["buttons"] as? [[String: Any]])?.first { $0["id"] as? String == action }
|
|
38
|
+
guard (button?["url"] as? String)?.isEmpty == false else { return nil } // the SDK's rule for opening the app
|
|
39
|
+
}
|
|
40
|
+
return ["notificationId": id, "data": userInfo["data"] as? [String: String] ?? [:]]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// Runs once launch has finished: becomes the delegate, keeping the earlier one, and asks for the device token when allowed.
|
|
44
|
+
@objc public static func installAtLaunch() {
|
|
45
|
+
let center = UNUserNotificationCenter.current()
|
|
46
|
+
if center.delegate !== delegate {
|
|
47
|
+
delegate.previous = center.delegate
|
|
48
|
+
center.delegate = delegate
|
|
49
|
+
}
|
|
50
|
+
registerIfAllowed()
|
|
51
|
+
// didBecomeActive also fires when the permission alert closes, so a new permission gets a token at once.
|
|
52
|
+
NotificationCenter.default.addObserver(
|
|
53
|
+
forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main
|
|
54
|
+
) { _ in registerIfAllowed() }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// Registers for remote notifications once the user has allowed them; iOS then hands the token to the app delegate.
|
|
58
|
+
private static func registerIfAllowed() {
|
|
59
|
+
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
|
60
|
+
guard [.authorized, .provisional, .ephemeral].contains(settings.authorizationStatus) else { return }
|
|
61
|
+
DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() }
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// The delegate itself; private so the generated Objective-C header needs no UserNotifications import.
|
|
67
|
+
private final class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
|
|
68
|
+
/// The delegate set before this one (the app's own or another library's); every call is passed on to it.
|
|
69
|
+
weak var previous: UNUserNotificationCenterDelegate?
|
|
70
|
+
|
|
71
|
+
/// A tap on a push or one of its buttons: PushPilot taps are handled, then the earlier delegate gets every tap.
|
|
72
|
+
func userNotificationCenter(
|
|
73
|
+
_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
|
|
74
|
+
withCompletionHandler completionHandler: @escaping () -> Void
|
|
75
|
+
) {
|
|
76
|
+
_ = RNPushPilotNotifications.handle(response)
|
|
77
|
+
if previous?.userNotificationCenter?(center, didReceive: response, withCompletionHandler: completionHandler) == nil {
|
|
78
|
+
completionHandler()
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// A push while the app is open: the earlier delegate decides; without one, iOS's default of not showing it.
|
|
83
|
+
func userNotificationCenter(
|
|
84
|
+
_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
|
|
85
|
+
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
|
|
86
|
+
) {
|
|
87
|
+
if previous?.userNotificationCenter?(center, willPresent: notification, withCompletionHandler: completionHandler) == nil {
|
|
88
|
+
completionHandler([])
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// The in-app notification settings request, passed on unchanged.
|
|
93
|
+
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
|
|
94
|
+
previous?.userNotificationCenter?(center, openSettingsFor: notification)
|
|
95
|
+
}
|
|
96
|
+
}
|