@entrig/react-native 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
+ <!-- Permissions for push notifications -->
4
+ <uses-permission android:name="android.permission.INTERNET" />
5
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6
+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
7
+ </manifest>
@@ -0,0 +1,190 @@
1
+ package com.entrig.reactnative
2
+
3
+ import android.app.Activity
4
+ import android.content.Context
5
+ import android.content.Intent
6
+ import android.content.pm.PackageManager
7
+ import android.os.Build
8
+ import androidx.activity.ComponentActivity
9
+ import androidx.activity.result.contract.ActivityResultContracts
10
+ import androidx.core.content.ContextCompat
11
+ import com.facebook.react.bridge.*
12
+ import com.facebook.react.modules.core.DeviceEventManagerModule
13
+ import com.entrig.sdk.Entrig
14
+ import com.entrig.sdk.models.EntrigConfig
15
+
16
+ class EntrigModule(reactContext: ReactApplicationContext) :
17
+ ReactContextBaseJavaModule(reactContext), ActivityEventListener {
18
+
19
+ private val context: Context
20
+ get() = reactApplicationContext
21
+
22
+ override fun getName(): String = "Entrig"
23
+
24
+ override fun initialize() {
25
+ super.initialize()
26
+
27
+ // Register for activity events (onNewIntent, onActivityResult)
28
+ reactApplicationContext.addActivityEventListener(this)
29
+
30
+ val currentActivity = reactApplicationContext.currentActivity
31
+ // Set activity on SDK for foreground detection
32
+ currentActivity?.let { Entrig.setActivity(it) }
33
+
34
+ // Handle initial intent (app launched from notification tap)
35
+ currentActivity?.intent?.let { intent ->
36
+ Entrig.handleIntent(intent)
37
+ }
38
+
39
+ Entrig.setOnForegroundNotificationListener { notification ->
40
+ sendEvent("onForegroundNotification", Arguments.makeNativeMap(notification.toMap() as Map<String, Any?>))
41
+ }
42
+
43
+ Entrig.setOnNotificationOpenedListener { notification ->
44
+ sendEvent("onNotificationOpened", Arguments.makeNativeMap(notification.toMap() as Map<String, Any?>))
45
+ }
46
+ }
47
+
48
+ override fun invalidate() {
49
+ reactApplicationContext.removeActivityEventListener(this)
50
+ super.invalidate()
51
+ }
52
+
53
+ // ActivityEventListener — called when a new intent arrives (notification tap while app is open)
54
+ override fun onNewIntent(intent: Intent) {
55
+ Entrig.handleIntent(intent)
56
+ }
57
+
58
+ override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) {
59
+ // Not used
60
+ }
61
+
62
+ @ReactMethod
63
+ fun initialize(config: ReadableMap, promise: Promise) {
64
+ val apiKey = config.getString("apiKey")
65
+ if (apiKey.isNullOrEmpty()) {
66
+ promise.reject("INVALID_API_KEY", "API key is required and cannot be empty")
67
+ return
68
+ }
69
+
70
+ val showForegroundNotification = if (config.hasKey("showForegroundNotification")) {
71
+ config.getBoolean("showForegroundNotification")
72
+ } else {
73
+ true
74
+ }
75
+ val entrigConfig = EntrigConfig(
76
+ apiKey = apiKey,
77
+ handlePermission = false, // Module handles permission itself
78
+ showForegroundNotification = showForegroundNotification
79
+ )
80
+
81
+ val appCtx = context.applicationContext
82
+ Entrig.initialize(appCtx, entrigConfig) { success, error ->
83
+ if (success) {
84
+ promise.resolve(null)
85
+ } else {
86
+ promise.reject("INIT_ERROR", error ?: "Failed to initialize SDK")
87
+ }
88
+ }
89
+ }
90
+
91
+ @ReactMethod
92
+ fun register(userId: String, isDebug: Boolean?, promise: Promise) {
93
+ val activity = reactApplicationContext.currentActivity
94
+ if (activity == null) {
95
+ promise.reject("NO_ACTIVITY", "Activity not available")
96
+ return
97
+ }
98
+
99
+ if (needsNotificationPermission()) {
100
+ requestNotificationPermission(activity) {
101
+ // Proceed with registration regardless of permission result
102
+ doRegister(userId, activity, promise)
103
+ }
104
+ return
105
+ }
106
+
107
+ doRegister(userId, activity, promise)
108
+ }
109
+
110
+ @ReactMethod
111
+ fun requestPermission(promise: Promise) {
112
+ val activity = reactApplicationContext.currentActivity
113
+ if (activity == null) {
114
+ promise.reject("NO_ACTIVITY", "Activity not available")
115
+ return
116
+ }
117
+
118
+ if (needsNotificationPermission()) {
119
+ requestNotificationPermission(activity) { granted ->
120
+ promise.resolve(granted)
121
+ }
122
+ return
123
+ }
124
+
125
+ promise.resolve(true)
126
+ }
127
+
128
+ @ReactMethod
129
+ fun unregister(promise: Promise) {
130
+ Entrig.unregister { success, error ->
131
+ if (success) {
132
+ promise.resolve(null)
133
+ } else {
134
+ promise.reject("UNREGISTER_ERROR", error ?: "Unregistration failed")
135
+ }
136
+ }
137
+ }
138
+
139
+ @ReactMethod
140
+ fun getInitialNotification(promise: Promise) {
141
+ val initialNotification = Entrig.getInitialNotification()
142
+ if (initialNotification != null) {
143
+ promise.resolve(Arguments.makeNativeMap(initialNotification.toMap() as Map<String, Any?>))
144
+ } else {
145
+ promise.resolve(null)
146
+ }
147
+ }
148
+
149
+ private fun needsNotificationPermission(): Boolean {
150
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
151
+ ContextCompat.checkSelfPermission(context, android.Manifest.permission.POST_NOTIFICATIONS) !=
152
+ PackageManager.PERMISSION_GRANTED
153
+ }
154
+
155
+ private fun requestNotificationPermission(activity: Activity, callback: (Boolean) -> Unit) {
156
+ val componentActivity = activity as? ComponentActivity
157
+ if (componentActivity == null) {
158
+ callback(false)
159
+ return
160
+ }
161
+
162
+ val key = "entrig_permission_${System.nanoTime()}"
163
+ val registry = componentActivity.activityResultRegistry
164
+
165
+ var launcher: androidx.activity.result.ActivityResultLauncher<String>? = null
166
+ launcher = registry.register(key, ActivityResultContracts.RequestPermission()) { granted ->
167
+ launcher?.unregister()
168
+ callback(granted)
169
+ }
170
+ launcher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
171
+ }
172
+
173
+ private fun doRegister(userId: String, activity: Activity, promise: Promise) {
174
+ Entrig.register(userId, activity, "react-native") { success, error ->
175
+ if (success) {
176
+ promise.resolve(null)
177
+ } else {
178
+ promise.reject("REGISTER_ERROR", error ?: "Registration failed")
179
+ }
180
+ }
181
+ }
182
+
183
+ private fun sendEvent(eventName: String, params: WritableMap?) {
184
+ if (reactApplicationContext.hasActiveReactInstance()) {
185
+ reactApplicationContext
186
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
187
+ .emit(eventName, params)
188
+ }
189
+ }
190
+ }
@@ -0,0 +1,16 @@
1
+ package com.entrig.reactnative
2
+
3
+ import com.facebook.react.ReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.uimanager.ViewManager
7
+
8
+ class EntrigPackage : ReactPackage {
9
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
10
+ return listOf(EntrigModule(reactContext))
11
+ }
12
+
13
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
14
+ return emptyList()
15
+ }
16
+ }
package/app.plugin.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./plugin/withEntrig');