@amrshbib/react-native-pushbib 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.
- package/README.md +475 -0
- package/android/build.gradle +51 -0
- package/android/src/main/AndroidManifest.xml +15 -0
- package/android/src/main/java/com/amrshbib/pushbib/FirebaseBootstrap.kt +37 -0
- package/android/src/main/java/com/amrshbib/pushbib/LiveUpdatePayload.kt +102 -0
- package/android/src/main/java/com/amrshbib/pushbib/LiveUpdateRenderer.kt +294 -0
- package/android/src/main/java/com/amrshbib/pushbib/NotificationPayload.kt +130 -0
- package/android/src/main/java/com/amrshbib/pushbib/NotificationRenderer.kt +181 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibChannels.kt +281 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibEmitter.kt +133 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibMessagingService.kt +87 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibModule.kt +286 -0
- package/android/src/main/java/com/amrshbib/pushbib/PushbibPackage.kt +14 -0
- package/index.d.ts +224 -0
- package/ios/PushbibCenterDelegate.h +16 -0
- package/ios/PushbibCenterDelegate.m +138 -0
- package/ios/PushbibEventBus.h +34 -0
- package/ios/PushbibEventBus.m +88 -0
- package/ios/PushbibLiveActivities.h +18 -0
- package/ios/PushbibLiveActivities.m +46 -0
- package/ios/PushbibModule.h +5 -0
- package/ios/PushbibModule.m +260 -0
- package/ios/PushbibSwizzler.m +135 -0
- package/package.json +40 -0
- package/react-native-pushbib.podspec +19 -0
- package/src/EventManager.js +55 -0
- package/src/Logger.js +18 -0
- package/src/NativePushbib.js +36 -0
- package/src/api.js +108 -0
- package/src/index.js +753 -0
- package/src/version.js +2 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
package com.amrshbib.pushbib
|
|
2
|
+
|
|
3
|
+
import android.app.NotificationChannel
|
|
4
|
+
import android.app.NotificationChannelGroup
|
|
5
|
+
import android.app.NotificationManager
|
|
6
|
+
import android.content.Context
|
|
7
|
+
import android.graphics.Color
|
|
8
|
+
import android.media.AudioAttributes
|
|
9
|
+
import android.media.RingtoneManager
|
|
10
|
+
import android.net.Uri
|
|
11
|
+
import android.os.Build
|
|
12
|
+
import org.json.JSONArray
|
|
13
|
+
import org.json.JSONObject
|
|
14
|
+
|
|
15
|
+
object PushbibChannels {
|
|
16
|
+
|
|
17
|
+
private const val PREFS_FILE = "pushbib_sdk"
|
|
18
|
+
private const val PREF_SYNCED_VERSION = "channels_version"
|
|
19
|
+
|
|
20
|
+
class Definition(
|
|
21
|
+
val key: String?,
|
|
22
|
+
val runtimeId: String,
|
|
23
|
+
val previousRuntimeId: String?,
|
|
24
|
+
val nativeId: String?,
|
|
25
|
+
val name: String,
|
|
26
|
+
val description: String?,
|
|
27
|
+
val importance: String,
|
|
28
|
+
val soundMode: String,
|
|
29
|
+
val soundName: String?,
|
|
30
|
+
val vibrationMode: String,
|
|
31
|
+
val vibrationPattern: LongArray?,
|
|
32
|
+
val ledMode: String,
|
|
33
|
+
val ledColor: String?,
|
|
34
|
+
val lockscreenVisibility: String,
|
|
35
|
+
val showBadge: Boolean,
|
|
36
|
+
val bypassDnd: Boolean,
|
|
37
|
+
val groupKey: String?,
|
|
38
|
+
val groupName: String?,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
fun sync(context: Context, channelsJson: String?, version: String?): Int {
|
|
42
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return 0
|
|
43
|
+
val definitions = parseList(channelsJson)
|
|
44
|
+
if (definitions.isEmpty()) return 0
|
|
45
|
+
|
|
46
|
+
val prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)
|
|
47
|
+
if (!version.isNullOrEmpty() && prefs.getString(PREF_SYNCED_VERSION, null) == version) return 0
|
|
48
|
+
|
|
49
|
+
for (definition in definitions) apply(context, definition)
|
|
50
|
+
|
|
51
|
+
if (!version.isNullOrEmpty()) prefs.edit().putString(PREF_SYNCED_VERSION, version).apply()
|
|
52
|
+
return definitions.size
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
fun ensureChannel(context: Context, json: JSONObject?): String? {
|
|
56
|
+
if (json == null) return null
|
|
57
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return null
|
|
58
|
+
val definition = parse(json) ?: return null
|
|
59
|
+
apply(context, definition)
|
|
60
|
+
return definition.runtimeId
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fun listChannels(context: Context): JSONArray {
|
|
64
|
+
val out = JSONArray()
|
|
65
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return out
|
|
66
|
+
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
67
|
+
for (channel in manager.notificationChannels) {
|
|
68
|
+
out.put(
|
|
69
|
+
JSONObject()
|
|
70
|
+
.put("id", channel.id)
|
|
71
|
+
.put("name", channel.name?.toString())
|
|
72
|
+
.put("importance", channel.importance)
|
|
73
|
+
.put("blocked", channel.importance == NotificationManager.IMPORTANCE_NONE)
|
|
74
|
+
.put("showBadge", channel.canShowBadge())
|
|
75
|
+
.putOpt("group", channel.group),
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
return out
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fun soundUri(context: Context, mode: String, name: String?): Uri? = when (mode.lowercase()) {
|
|
82
|
+
"off" -> null
|
|
83
|
+
"custom" -> customSoundUri(context, name)
|
|
84
|
+
else -> RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private fun customSoundUri(context: Context, name: String?): Uri? {
|
|
88
|
+
if (name.isNullOrEmpty()) return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
|
89
|
+
val id = context.resources.getIdentifier(name, "raw", context.packageName)
|
|
90
|
+
if (id != 0) return Uri.parse("android.resource://${context.packageName}/$id")
|
|
91
|
+
android.util.Log.w("Pushbib", "channel sound \"$name\" is not bundled in res/raw — using the default tone")
|
|
92
|
+
return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private fun apply(context: Context, d: Definition) {
|
|
96
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
|
97
|
+
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
98
|
+
|
|
99
|
+
if (!d.groupKey.isNullOrEmpty()) {
|
|
100
|
+
manager.createNotificationChannelGroup(
|
|
101
|
+
NotificationChannelGroup(d.groupKey, d.groupName ?: d.groupKey),
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
val existing = manager.getNotificationChannel(d.runtimeId)
|
|
106
|
+
if (existing != null) {
|
|
107
|
+
existing.name = d.name
|
|
108
|
+
existing.description = d.description
|
|
109
|
+
manager.createNotificationChannel(existing)
|
|
110
|
+
deletePrevious(manager, d)
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
val channel = NotificationChannel(d.runtimeId, d.name, importanceOf(d.importance)).apply {
|
|
115
|
+
description = d.description
|
|
116
|
+
setShowBadge(d.showBadge)
|
|
117
|
+
lockscreenVisibility = visibilityOf(d.lockscreenVisibility)
|
|
118
|
+
|
|
119
|
+
when (d.vibrationMode) {
|
|
120
|
+
"off" -> enableVibration(false)
|
|
121
|
+
"custom" -> {
|
|
122
|
+
enableVibration(true)
|
|
123
|
+
if (d.vibrationPattern != null && d.vibrationPattern.isNotEmpty()) {
|
|
124
|
+
vibrationPattern = d.vibrationPattern
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else -> enableVibration(true)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
when (d.ledMode) {
|
|
131
|
+
"off" -> enableLights(false)
|
|
132
|
+
"custom" -> {
|
|
133
|
+
enableLights(true)
|
|
134
|
+
if (!d.ledColor.isNullOrEmpty()) runCatching { lightColor = Color.parseColor(d.ledColor) }
|
|
135
|
+
}
|
|
136
|
+
else -> enableLights(true)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (!d.groupKey.isNullOrEmpty()) group = d.groupKey
|
|
140
|
+
if (d.bypassDnd) setBypassDnd(true)
|
|
141
|
+
|
|
142
|
+
val uri = soundUri(context, d.soundMode, d.soundName)
|
|
143
|
+
if (uri == null) {
|
|
144
|
+
setSound(null, null)
|
|
145
|
+
} else {
|
|
146
|
+
setSound(
|
|
147
|
+
uri,
|
|
148
|
+
AudioAttributes.Builder()
|
|
149
|
+
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
|
|
150
|
+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
|
151
|
+
.build(),
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
manager.createNotificationChannel(channel)
|
|
157
|
+
deletePrevious(manager, d)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private fun deletePrevious(manager: NotificationManager, d: Definition) {
|
|
161
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
|
162
|
+
val previous = d.previousRuntimeId ?: return
|
|
163
|
+
if (previous == d.runtimeId) return
|
|
164
|
+
runCatching { manager.deleteNotificationChannel(previous) }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private fun parseList(json: String?): List<Definition> {
|
|
168
|
+
if (json.isNullOrEmpty()) return emptyList()
|
|
169
|
+
val array = runCatching { JSONArray(json) }.getOrNull() ?: return emptyList()
|
|
170
|
+
val out = ArrayList<Definition>(array.length())
|
|
171
|
+
for (i in 0 until array.length()) {
|
|
172
|
+
parse(array.optJSONObject(i))?.let { out.add(it) }
|
|
173
|
+
}
|
|
174
|
+
return out
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private fun parse(json: JSONObject?): Definition? {
|
|
178
|
+
if (json == null) return null
|
|
179
|
+
val nativeId = json.pick("nativeId", "native_id")
|
|
180
|
+
val runtimeId = json.pick("runtimeId", "runtime_id") ?: nativeId ?: return null
|
|
181
|
+
val key = json.pick("key")
|
|
182
|
+
|
|
183
|
+
val soundObject = json.optJSONObject("sound")
|
|
184
|
+
val legacySound = if (soundObject == null) json.pick("sound") else null
|
|
185
|
+
val soundName = if (soundObject != null) soundObject.pick("name") else legacySound?.takeIf { it != "none" }
|
|
186
|
+
val soundMode = when {
|
|
187
|
+
soundObject != null -> soundObject.mode(if (soundName.isNullOrEmpty()) "default" else "custom")
|
|
188
|
+
legacySound == null -> "default"
|
|
189
|
+
legacySound == "none" -> "off"
|
|
190
|
+
else -> "custom"
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
val vibrationObject = json.optJSONObject("vibration")
|
|
194
|
+
val vibrationPattern =
|
|
195
|
+
if (vibrationObject != null) vibrationObject.longs("pattern")
|
|
196
|
+
else json.longs("vibrationPattern", "vibration_pattern")
|
|
197
|
+
val vibrationMode = when {
|
|
198
|
+
vibrationObject != null -> vibrationObject.mode(if (vibrationPattern == null) "default" else "custom")
|
|
199
|
+
!json.flag("vibrate", true) -> "off"
|
|
200
|
+
vibrationPattern == null -> "default"
|
|
201
|
+
else -> "custom"
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
val ledObject = json.optJSONObject("led")
|
|
205
|
+
val ledColor = if (ledObject != null) ledObject.pick("color") else json.pick("ledColor", "led_color")
|
|
206
|
+
val ledMode = when {
|
|
207
|
+
ledObject != null -> ledObject.mode(if (ledColor.isNullOrEmpty()) "off" else "custom")
|
|
208
|
+
ledColor.isNullOrEmpty() -> "off"
|
|
209
|
+
else -> "custom"
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return Definition(
|
|
213
|
+
key = key,
|
|
214
|
+
runtimeId = runtimeId,
|
|
215
|
+
previousRuntimeId = json.pick("previousRuntimeId", "previous_runtime_id"),
|
|
216
|
+
nativeId = nativeId,
|
|
217
|
+
name = json.pick("name") ?: key ?: runtimeId,
|
|
218
|
+
description = json.pick("description"),
|
|
219
|
+
importance = json.pick("importance") ?: "high",
|
|
220
|
+
soundMode = soundMode,
|
|
221
|
+
soundName = soundName,
|
|
222
|
+
vibrationMode = vibrationMode,
|
|
223
|
+
vibrationPattern = vibrationPattern,
|
|
224
|
+
ledMode = ledMode,
|
|
225
|
+
ledColor = ledColor,
|
|
226
|
+
lockscreenVisibility = json.pick("lockscreenVisibility", "lockscreen_visibility") ?: "public",
|
|
227
|
+
showBadge = json.flag("showBadge", true, "show_badge"),
|
|
228
|
+
bypassDnd = json.flag("bypassDnd", false, "bypass_dnd"),
|
|
229
|
+
groupKey = json.pick("groupKey", "group_key"),
|
|
230
|
+
groupName = json.pick("groupName", "group_name"),
|
|
231
|
+
)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private fun importanceOf(value: String): Int = when (value.lowercase()) {
|
|
235
|
+
"none", "min", "low" -> NotificationManager.IMPORTANCE_LOW
|
|
236
|
+
"medium", "default" -> NotificationManager.IMPORTANCE_DEFAULT
|
|
237
|
+
"urgent" -> NotificationManager.IMPORTANCE_MAX
|
|
238
|
+
else -> NotificationManager.IMPORTANCE_HIGH
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private fun visibilityOf(value: String): Int = when (value.lowercase()) {
|
|
242
|
+
"secret" -> android.app.Notification.VISIBILITY_SECRET
|
|
243
|
+
"private" -> android.app.Notification.VISIBILITY_PRIVATE
|
|
244
|
+
else -> android.app.Notification.VISIBILITY_PUBLIC
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private fun JSONObject.pick(vararg keys: String): String? {
|
|
248
|
+
for (key in keys) {
|
|
249
|
+
if (!has(key) || isNull(key)) continue
|
|
250
|
+
val value = optString(key, "")
|
|
251
|
+
if (value.isNotEmpty()) return value
|
|
252
|
+
}
|
|
253
|
+
return null
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private fun JSONObject.mode(fallback: String): String {
|
|
257
|
+
val value = pick("mode") ?: return fallback
|
|
258
|
+
return when (value.lowercase()) {
|
|
259
|
+
"off" -> "off"
|
|
260
|
+
"default" -> "default"
|
|
261
|
+
"custom" -> "custom"
|
|
262
|
+
else -> fallback
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private fun JSONObject.flag(camel: String, fallback: Boolean, vararg others: String): Boolean {
|
|
267
|
+
for (key in listOf(camel, *others)) {
|
|
268
|
+
if (has(key) && !isNull(key)) return optBoolean(key, fallback)
|
|
269
|
+
}
|
|
270
|
+
return fallback
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private fun JSONObject.longs(vararg keys: String): LongArray? {
|
|
274
|
+
for (key in keys) {
|
|
275
|
+
val array = optJSONArray(key) ?: continue
|
|
276
|
+
if (array.length() == 0) continue
|
|
277
|
+
return LongArray(array.length()) { array.optLong(it, 0L) }
|
|
278
|
+
}
|
|
279
|
+
return null
|
|
280
|
+
}
|
|
281
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
package com.amrshbib.pushbib
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.os.Handler
|
|
5
|
+
import android.os.Looper
|
|
6
|
+
import com.facebook.react.bridge.Arguments
|
|
7
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
8
|
+
import com.facebook.react.bridge.WritableArray
|
|
9
|
+
import com.facebook.react.bridge.WritableMap
|
|
10
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
11
|
+
import org.json.JSONArray
|
|
12
|
+
import org.json.JSONObject
|
|
13
|
+
import java.util.UUID
|
|
14
|
+
import java.util.concurrent.ConcurrentHashMap
|
|
15
|
+
|
|
16
|
+
object PushbibEmitter {
|
|
17
|
+
const val EVENT_PUSH_TOKEN = "Pushbib#pushToken"
|
|
18
|
+
const val EVENT_CLICK = "Pushbib#click"
|
|
19
|
+
const val EVENT_RECEIVED = "Pushbib#received"
|
|
20
|
+
const val EVENT_FOREGROUND_WILL_DISPLAY = "Pushbib#foregroundWillDisplay"
|
|
21
|
+
|
|
22
|
+
const val EVENT_LIVE_UPDATE = "Pushbib#liveUpdate"
|
|
23
|
+
|
|
24
|
+
private const val QUEUE_CAP = 20
|
|
25
|
+
private const val FOREGROUND_DECISION_TIMEOUT_MS = 5000L
|
|
26
|
+
|
|
27
|
+
@Volatile var reactContext: ReactApplicationContext? = null
|
|
28
|
+
@Volatile var jsListening = false
|
|
29
|
+
@Volatile var foregroundHandling = false
|
|
30
|
+
|
|
31
|
+
private val queue = ArrayList<Pair<String, JSONObject>>()
|
|
32
|
+
private val mainHandler = Handler(Looper.getMainLooper())
|
|
33
|
+
private val pendingDisplays = ConcurrentHashMap<String, PendingDisplay>()
|
|
34
|
+
|
|
35
|
+
private class PendingDisplay(val context: Context, val payload: NotificationPayload, val timeout: Runnable)
|
|
36
|
+
|
|
37
|
+
fun onReactContextGone(context: ReactApplicationContext) {
|
|
38
|
+
if (reactContext === context) {
|
|
39
|
+
reactContext = null
|
|
40
|
+
jsListening = false
|
|
41
|
+
foregroundHandling = false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fun canAskJs(): Boolean {
|
|
46
|
+
val ctx = reactContext
|
|
47
|
+
return foregroundHandling && jsListening && ctx != null && ctx.hasActiveReactInstance()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fun emitOrQueue(name: String, body: JSONObject) {
|
|
51
|
+
val ctx = reactContext
|
|
52
|
+
if (jsListening && ctx != null && ctx.hasActiveReactInstance()) {
|
|
53
|
+
emit(ctx, name, body)
|
|
54
|
+
} else {
|
|
55
|
+
synchronized(queue) {
|
|
56
|
+
if (queue.size >= QUEUE_CAP) queue.removeAt(0)
|
|
57
|
+
queue.add(name to body)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
fun flushQueue() {
|
|
63
|
+
val ctx = reactContext ?: return
|
|
64
|
+
if (!ctx.hasActiveReactInstance()) return
|
|
65
|
+
val drained: List<Pair<String, JSONObject>>
|
|
66
|
+
synchronized(queue) {
|
|
67
|
+
drained = ArrayList(queue)
|
|
68
|
+
queue.clear()
|
|
69
|
+
}
|
|
70
|
+
for ((name, body) in drained) emit(ctx, name, body)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private fun emit(ctx: ReactApplicationContext, name: String, body: JSONObject) {
|
|
74
|
+
try {
|
|
75
|
+
ctx
|
|
76
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
|
77
|
+
.emit(name, jsonToWritableMap(body))
|
|
78
|
+
} catch (e: Exception) {
|
|
79
|
+
android.util.Log.w("Pushbib", "emit($name) failed: ${e.message}")
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fun askForegroundDisplay(context: Context, payload: NotificationPayload) {
|
|
84
|
+
val id = UUID.randomUUID().toString()
|
|
85
|
+
val timeout = Runnable { completeDisplay(id, true) }
|
|
86
|
+
pendingDisplays[id] = PendingDisplay(context.applicationContext, payload, timeout)
|
|
87
|
+
mainHandler.postDelayed(timeout, FOREGROUND_DECISION_TIMEOUT_MS)
|
|
88
|
+
emitOrQueue(
|
|
89
|
+
EVENT_FOREGROUND_WILL_DISPLAY,
|
|
90
|
+
JSONObject().put("completionId", id).put("notification", payload.toJson()),
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fun completeDisplay(completionId: String, display: Boolean) {
|
|
95
|
+
val pending = pendingDisplays.remove(completionId) ?: return
|
|
96
|
+
mainHandler.removeCallbacks(pending.timeout)
|
|
97
|
+
if (display) NotificationRenderer.render(pending.context, pending.payload)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
fun jsonToWritableMap(json: JSONObject): WritableMap {
|
|
101
|
+
val map = Arguments.createMap()
|
|
102
|
+
for (key in json.keys()) {
|
|
103
|
+
when (val value = json.opt(key)) {
|
|
104
|
+
null, JSONObject.NULL -> map.putNull(key)
|
|
105
|
+
is JSONObject -> map.putMap(key, jsonToWritableMap(value))
|
|
106
|
+
is JSONArray -> map.putArray(key, jsonToWritableArray(value))
|
|
107
|
+
is Boolean -> map.putBoolean(key, value)
|
|
108
|
+
is Int -> map.putInt(key, value)
|
|
109
|
+
is Long -> map.putDouble(key, value.toDouble())
|
|
110
|
+
is Number -> map.putDouble(key, value.toDouble())
|
|
111
|
+
else -> map.putString(key, value.toString())
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return map
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private fun jsonToWritableArray(json: JSONArray): WritableArray {
|
|
118
|
+
val arr = Arguments.createArray()
|
|
119
|
+
for (i in 0 until json.length()) {
|
|
120
|
+
when (val value = json.opt(i)) {
|
|
121
|
+
null, JSONObject.NULL -> arr.pushNull()
|
|
122
|
+
is JSONObject -> arr.pushMap(jsonToWritableMap(value))
|
|
123
|
+
is JSONArray -> arr.pushArray(jsonToWritableArray(value))
|
|
124
|
+
is Boolean -> arr.pushBoolean(value)
|
|
125
|
+
is Int -> arr.pushInt(value)
|
|
126
|
+
is Long -> arr.pushDouble(value.toDouble())
|
|
127
|
+
is Number -> arr.pushDouble(value.toDouble())
|
|
128
|
+
else -> arr.pushString(value.toString())
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return arr
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
package com.amrshbib.pushbib
|
|
2
|
+
|
|
3
|
+
import android.app.ActivityManager
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import com.google.firebase.messaging.FirebaseMessagingService
|
|
6
|
+
import com.google.firebase.messaging.RemoteMessage
|
|
7
|
+
import org.json.JSONObject
|
|
8
|
+
|
|
9
|
+
class PushbibMessagingService : FirebaseMessagingService() {
|
|
10
|
+
|
|
11
|
+
override fun onMessageReceived(message: RemoteMessage) {
|
|
12
|
+
if (LiveUpdatePayload.isLiveUpdate(message)) {
|
|
13
|
+
handleLiveUpdate(message)
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
val payload = NotificationPayload.fromRemoteMessage(message) ?: return
|
|
18
|
+
|
|
19
|
+
PushbibEmitter.emitOrQueue(
|
|
20
|
+
PushbibEmitter.EVENT_RECEIVED,
|
|
21
|
+
JSONObject().put("notification", payload.toJson()),
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
if (isOptedOut()) return
|
|
25
|
+
|
|
26
|
+
val foreground = isAppInForeground()
|
|
27
|
+
|
|
28
|
+
if (foreground && !payload.showForeground) return
|
|
29
|
+
if (!foreground && !payload.showBackground) return
|
|
30
|
+
|
|
31
|
+
if (foreground && !localDisplayEnabled("show_foreground")) return
|
|
32
|
+
if (!foreground && !localDisplayEnabled("show_background")) return
|
|
33
|
+
|
|
34
|
+
if (foreground && PushbibEmitter.canAskJs()) {
|
|
35
|
+
PushbibEmitter.askForegroundDisplay(applicationContext, payload)
|
|
36
|
+
} else {
|
|
37
|
+
NotificationRenderer.render(applicationContext, payload)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private fun handleLiveUpdate(message: RemoteMessage) {
|
|
42
|
+
val payload = LiveUpdatePayload.fromRemoteMessage(message) ?: return
|
|
43
|
+
|
|
44
|
+
if (isOptedOut()) {
|
|
45
|
+
emitLiveUpdate(payload, LiveUpdateRenderer.OUTCOME_OPTED_OUT)
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!localDisplayEnabled("show_live_update")) {
|
|
50
|
+
emitLiveUpdate(payload, LiveUpdateRenderer.OUTCOME_LOCAL_DISPLAY_OFF)
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
LiveUpdateRenderer.render(applicationContext, payload) { outcome ->
|
|
55
|
+
emitLiveUpdate(payload, outcome)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private fun emitLiveUpdate(payload: LiveUpdatePayload, outcome: String) {
|
|
60
|
+
PushbibEmitter.emitOrQueue(
|
|
61
|
+
PushbibEmitter.EVENT_LIVE_UPDATE,
|
|
62
|
+
JSONObject()
|
|
63
|
+
.put("liveUpdate", payload.toJson())
|
|
64
|
+
.put("outcome", outcome)
|
|
65
|
+
.put(
|
|
66
|
+
"displayed",
|
|
67
|
+
outcome == LiveUpdateRenderer.OUTCOME_SHOWN || outcome == LiveUpdateRenderer.OUTCOME_ENDED,
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
override fun onNewToken(token: String) {
|
|
73
|
+
PushbibEmitter.emitOrQueue(PushbibEmitter.EVENT_PUSH_TOKEN, JSONObject().put("token", token))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private fun isOptedOut(): Boolean =
|
|
77
|
+
getSharedPreferences("pushbib_sdk", Context.MODE_PRIVATE).getString("opted_out", null) == "1"
|
|
78
|
+
|
|
79
|
+
private fun localDisplayEnabled(key: String): Boolean =
|
|
80
|
+
getSharedPreferences("pushbib_sdk", Context.MODE_PRIVATE).getString(key, null) != "0"
|
|
81
|
+
|
|
82
|
+
private fun isAppInForeground(): Boolean {
|
|
83
|
+
val info = ActivityManager.RunningAppProcessInfo()
|
|
84
|
+
ActivityManager.getMyMemoryState(info)
|
|
85
|
+
return info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
|
|
86
|
+
}
|
|
87
|
+
}
|