@onesignal/capacitor-plugin 1.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/OneSignalCapacitorPlugin.podspec +20 -0
- package/Package.swift +36 -0
- package/README.md +361 -0
- package/android/build.gradle.kts +126 -0
- package/android/gradle/libs.versions.toml +25 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/kotlin/com/onesignal/capacitor/OneSignalCapacitorPlugin.kt +736 -0
- package/dist/index.d.ts +1012 -0
- package/dist/index.js +849 -0
- package/ios/Sources/OneSignalCapacitorPlugin/OneSignalCapacitorPlugin.swift +639 -0
- package/package.json +84 -0
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
package com.onesignal.capacitor
|
|
2
|
+
|
|
3
|
+
import com.getcapacitor.JSObject
|
|
4
|
+
import com.getcapacitor.Plugin
|
|
5
|
+
import com.getcapacitor.PluginCall
|
|
6
|
+
import com.getcapacitor.PluginMethod
|
|
7
|
+
import com.getcapacitor.annotation.CapacitorPlugin
|
|
8
|
+
import com.onesignal.OneSignal
|
|
9
|
+
import com.onesignal.common.OneSignalWrapper
|
|
10
|
+
import com.onesignal.inAppMessages.IInAppMessageClickEvent
|
|
11
|
+
import com.onesignal.inAppMessages.IInAppMessageClickListener
|
|
12
|
+
import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent
|
|
13
|
+
import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent
|
|
14
|
+
import com.onesignal.inAppMessages.IInAppMessageLifecycleListener
|
|
15
|
+
import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent
|
|
16
|
+
import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent
|
|
17
|
+
import com.onesignal.notifications.INotification
|
|
18
|
+
import com.onesignal.notifications.INotificationClickEvent
|
|
19
|
+
import com.onesignal.notifications.INotificationClickListener
|
|
20
|
+
import com.onesignal.notifications.INotificationLifecycleListener
|
|
21
|
+
import com.onesignal.notifications.INotificationWillDisplayEvent
|
|
22
|
+
import com.onesignal.notifications.IPermissionObserver
|
|
23
|
+
import com.onesignal.user.state.IUserStateObserver
|
|
24
|
+
import com.onesignal.user.state.UserChangedState
|
|
25
|
+
import com.onesignal.user.subscriptions.IPushSubscriptionObserver
|
|
26
|
+
import com.onesignal.user.subscriptions.PushSubscriptionChangedState
|
|
27
|
+
import kotlinx.coroutines.CoroutineScope
|
|
28
|
+
import kotlinx.coroutines.Dispatchers
|
|
29
|
+
import kotlinx.coroutines.launch
|
|
30
|
+
import org.json.JSONArray
|
|
31
|
+
import org.json.JSONObject
|
|
32
|
+
|
|
33
|
+
@CapacitorPlugin(name = "OneSignalCapacitor")
|
|
34
|
+
class OneSignalCapacitorPlugin : Plugin(),
|
|
35
|
+
INotificationLifecycleListener,
|
|
36
|
+
INotificationClickListener,
|
|
37
|
+
IInAppMessageLifecycleListener,
|
|
38
|
+
IInAppMessageClickListener {
|
|
39
|
+
|
|
40
|
+
private val notificationWillDisplayCache = mutableMapOf<String, INotificationWillDisplayEvent>()
|
|
41
|
+
private val preventDefaultCache = mutableSetOf<String>()
|
|
42
|
+
private var pendingClickEvent: INotificationClickEvent? = null
|
|
43
|
+
|
|
44
|
+
// region Core
|
|
45
|
+
|
|
46
|
+
@PluginMethod
|
|
47
|
+
fun initialize(call: PluginCall) {
|
|
48
|
+
val appId = call.getString("appId")
|
|
49
|
+
if (appId == null) {
|
|
50
|
+
call.reject("appId is required")
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
OneSignalWrapper.sdkType = "capacitor"
|
|
55
|
+
OneSignalWrapper.sdkVersion = "010000"
|
|
56
|
+
OneSignal.initWithContext(context, appId)
|
|
57
|
+
|
|
58
|
+
OneSignal.Notifications.addPermissionObserver(object : IPermissionObserver {
|
|
59
|
+
override fun onNotificationPermissionChange(permission: Boolean) {
|
|
60
|
+
val ret = JSObject()
|
|
61
|
+
ret.put("permission", permission)
|
|
62
|
+
notifyListeners("permissionChange", ret)
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
OneSignal.Notifications.addForegroundLifecycleListener(this)
|
|
67
|
+
OneSignal.Notifications.addClickListener(this)
|
|
68
|
+
|
|
69
|
+
OneSignal.User.pushSubscription.addObserver(object : IPushSubscriptionObserver {
|
|
70
|
+
override fun onPushSubscriptionChange(state: PushSubscriptionChangedState) {
|
|
71
|
+
val ret = JSObject()
|
|
72
|
+
val prev = JSObject()
|
|
73
|
+
prev.put("id", state.previous.id.ifEmpty { JSONObject.NULL })
|
|
74
|
+
prev.put("token", state.previous.token.ifEmpty { JSONObject.NULL })
|
|
75
|
+
prev.put("optedIn", state.previous.optedIn)
|
|
76
|
+
ret.put("previous", prev)
|
|
77
|
+
|
|
78
|
+
val curr = JSObject()
|
|
79
|
+
curr.put("id", state.current.id.ifEmpty { JSONObject.NULL })
|
|
80
|
+
curr.put("token", state.current.token.ifEmpty { JSONObject.NULL })
|
|
81
|
+
curr.put("optedIn", state.current.optedIn)
|
|
82
|
+
ret.put("current", curr)
|
|
83
|
+
|
|
84
|
+
notifyListeners("pushSubscriptionChange", ret)
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
OneSignal.User.addObserver(object : IUserStateObserver {
|
|
89
|
+
override fun onUserStateChange(state: UserChangedState) {
|
|
90
|
+
val ret = JSObject()
|
|
91
|
+
val curr = JSObject()
|
|
92
|
+
curr.put("onesignalId", state.current.onesignalId.ifEmpty { JSONObject.NULL })
|
|
93
|
+
curr.put("externalId", state.current.externalId.ifEmpty { JSONObject.NULL })
|
|
94
|
+
ret.put("current", curr)
|
|
95
|
+
notifyListeners("userStateChange", ret)
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
OneSignal.InAppMessages.addLifecycleListener(this)
|
|
100
|
+
OneSignal.InAppMessages.addClickListener(this)
|
|
101
|
+
|
|
102
|
+
pendingClickEvent?.let { event ->
|
|
103
|
+
val ret = JSObject()
|
|
104
|
+
val clickResult = JSObject()
|
|
105
|
+
clickResult.put("actionId", event.result.actionId)
|
|
106
|
+
clickResult.put("url", event.result.url)
|
|
107
|
+
ret.put("result", clickResult)
|
|
108
|
+
ret.put("notification", JSObject(event.notification.rawPayload))
|
|
109
|
+
notifyListeners("notificationClick", ret)
|
|
110
|
+
pendingClickEvent = null
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
call.resolve()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@PluginMethod
|
|
117
|
+
fun login(call: PluginCall) {
|
|
118
|
+
val externalId = call.getString("externalId")
|
|
119
|
+
if (externalId == null) {
|
|
120
|
+
call.reject("externalId is required")
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
OneSignal.login(externalId)
|
|
124
|
+
call.resolve()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
@PluginMethod
|
|
128
|
+
fun logout(call: PluginCall) {
|
|
129
|
+
OneSignal.logout()
|
|
130
|
+
call.resolve()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
@PluginMethod
|
|
134
|
+
fun setConsentRequired(call: PluginCall) {
|
|
135
|
+
val required = call.getBoolean("required") ?: false
|
|
136
|
+
OneSignal.consentRequired = required
|
|
137
|
+
call.resolve()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@PluginMethod
|
|
141
|
+
fun setConsentGiven(call: PluginCall) {
|
|
142
|
+
val granted = call.getBoolean("granted") ?: false
|
|
143
|
+
OneSignal.consentGiven = granted
|
|
144
|
+
call.resolve()
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// endregion
|
|
148
|
+
|
|
149
|
+
// region Debug
|
|
150
|
+
|
|
151
|
+
@PluginMethod
|
|
152
|
+
fun setLogLevel(call: PluginCall) {
|
|
153
|
+
val level = call.getInt("logLevel") ?: 0
|
|
154
|
+
OneSignal.Debug.logLevel = com.onesignal.debug.LogLevel.fromInt(level)
|
|
155
|
+
call.resolve()
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
@PluginMethod
|
|
159
|
+
fun setAlertLevel(call: PluginCall) {
|
|
160
|
+
val level = call.getInt("logLevel") ?: 0
|
|
161
|
+
OneSignal.Debug.alertLevel = com.onesignal.debug.LogLevel.fromInt(level)
|
|
162
|
+
call.resolve()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// endregion
|
|
166
|
+
|
|
167
|
+
// region User
|
|
168
|
+
|
|
169
|
+
@PluginMethod
|
|
170
|
+
fun setLanguage(call: PluginCall) {
|
|
171
|
+
val language = call.getString("language")
|
|
172
|
+
if (language == null) {
|
|
173
|
+
call.reject("language is required")
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
OneSignal.User.setLanguage(language)
|
|
177
|
+
call.resolve()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@PluginMethod
|
|
181
|
+
fun addAliases(call: PluginCall) {
|
|
182
|
+
val aliasesObj = call.getObject("aliases") ?: run {
|
|
183
|
+
call.reject("aliases is required")
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
val aliases = mutableMapOf<String, String>()
|
|
187
|
+
aliasesObj.keys().forEach { key -> aliasesObj.getString(key)?.let { aliases[key] = it } }
|
|
188
|
+
OneSignal.User.addAliases(aliases)
|
|
189
|
+
call.resolve()
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
@PluginMethod
|
|
193
|
+
fun removeAliases(call: PluginCall) {
|
|
194
|
+
val labels = call.getArray("labels") ?: run {
|
|
195
|
+
call.reject("labels is required")
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
val list = mutableListOf<String>()
|
|
199
|
+
for (i in 0 until labels.length()) {
|
|
200
|
+
list.add(labels.getString(i))
|
|
201
|
+
}
|
|
202
|
+
OneSignal.User.removeAliases(list)
|
|
203
|
+
call.resolve()
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
@PluginMethod
|
|
207
|
+
fun addEmail(call: PluginCall) {
|
|
208
|
+
val email = call.getString("email")
|
|
209
|
+
if (email == null) {
|
|
210
|
+
call.reject("email is required")
|
|
211
|
+
return
|
|
212
|
+
}
|
|
213
|
+
OneSignal.User.addEmail(email)
|
|
214
|
+
call.resolve()
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
@PluginMethod
|
|
218
|
+
fun removeEmail(call: PluginCall) {
|
|
219
|
+
val email = call.getString("email")
|
|
220
|
+
if (email == null) {
|
|
221
|
+
call.reject("email is required")
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
OneSignal.User.removeEmail(email)
|
|
225
|
+
call.resolve()
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
@PluginMethod
|
|
229
|
+
fun addSms(call: PluginCall) {
|
|
230
|
+
val smsNumber = call.getString("smsNumber")
|
|
231
|
+
if (smsNumber == null) {
|
|
232
|
+
call.reject("smsNumber is required")
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
OneSignal.User.addSms(smsNumber)
|
|
236
|
+
call.resolve()
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
@PluginMethod
|
|
240
|
+
fun removeSms(call: PluginCall) {
|
|
241
|
+
val smsNumber = call.getString("smsNumber")
|
|
242
|
+
if (smsNumber == null) {
|
|
243
|
+
call.reject("smsNumber is required")
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
OneSignal.User.removeSms(smsNumber)
|
|
247
|
+
call.resolve()
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
@PluginMethod
|
|
251
|
+
fun addTags(call: PluginCall) {
|
|
252
|
+
val tagsObj = call.getObject("tags") ?: run {
|
|
253
|
+
call.reject("tags is required")
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
val tags = mutableMapOf<String, String>()
|
|
257
|
+
tagsObj.keys().forEach { key -> tagsObj.getString(key)?.let { tags[key] = it } }
|
|
258
|
+
OneSignal.User.addTags(tags)
|
|
259
|
+
call.resolve()
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
@PluginMethod
|
|
263
|
+
fun removeTags(call: PluginCall) {
|
|
264
|
+
val keysArr = call.getArray("keys") ?: run {
|
|
265
|
+
call.reject("keys is required")
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
val keys = mutableListOf<String>()
|
|
269
|
+
for (i in 0 until keysArr.length()) {
|
|
270
|
+
keys.add(keysArr.getString(i))
|
|
271
|
+
}
|
|
272
|
+
OneSignal.User.removeTags(keys)
|
|
273
|
+
call.resolve()
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
@PluginMethod
|
|
277
|
+
fun getTags(call: PluginCall) {
|
|
278
|
+
val tags = OneSignal.User.getTags()
|
|
279
|
+
val tagsObj = JSObject()
|
|
280
|
+
tags.forEach { (key, value) -> tagsObj.put(key, value) }
|
|
281
|
+
val ret = JSObject()
|
|
282
|
+
ret.put("tags", tagsObj)
|
|
283
|
+
call.resolve(ret)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
@PluginMethod
|
|
287
|
+
fun getOnesignalId(call: PluginCall) {
|
|
288
|
+
val ret = JSObject()
|
|
289
|
+
ret.put("onesignalId", OneSignal.User.onesignalId.ifEmpty { JSONObject.NULL })
|
|
290
|
+
call.resolve(ret)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
@PluginMethod
|
|
294
|
+
fun getExternalId(call: PluginCall) {
|
|
295
|
+
val ret = JSObject()
|
|
296
|
+
ret.put("externalId", OneSignal.User.externalId.ifEmpty { JSONObject.NULL })
|
|
297
|
+
call.resolve(ret)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
@PluginMethod
|
|
301
|
+
fun trackEvent(call: PluginCall) {
|
|
302
|
+
val name = call.getString("name")
|
|
303
|
+
if (name == null) {
|
|
304
|
+
call.reject("name is required")
|
|
305
|
+
return
|
|
306
|
+
}
|
|
307
|
+
val propertiesObj = call.getObject("properties")
|
|
308
|
+
val properties = propertiesObj?.let { jsonObjectToMap(it) }
|
|
309
|
+
|
|
310
|
+
OneSignal.User.trackEvent(name, properties)
|
|
311
|
+
call.resolve()
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private fun jsonObjectToMap(jsonObject: JSONObject): Map<String, Any?> {
|
|
315
|
+
val map = mutableMapOf<String, Any?>()
|
|
316
|
+
jsonObject.keys().forEach { key ->
|
|
317
|
+
map[key] = convertJsonValue(jsonObject.get(key))
|
|
318
|
+
}
|
|
319
|
+
return map
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private fun jsonArrayToList(jsonArray: JSONArray): List<Any?> {
|
|
323
|
+
val list = mutableListOf<Any?>()
|
|
324
|
+
for (i in 0 until jsonArray.length()) {
|
|
325
|
+
list.add(convertJsonValue(jsonArray.get(i)))
|
|
326
|
+
}
|
|
327
|
+
return list
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private fun convertJsonValue(value: Any): Any? {
|
|
331
|
+
return when {
|
|
332
|
+
value == JSONObject.NULL -> null
|
|
333
|
+
value is JSONObject -> jsonObjectToMap(value)
|
|
334
|
+
value is JSONArray -> jsonArrayToList(value)
|
|
335
|
+
else -> value
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// endregion
|
|
340
|
+
|
|
341
|
+
// region Push Subscription
|
|
342
|
+
|
|
343
|
+
@PluginMethod
|
|
344
|
+
fun getPushSubscriptionId(call: PluginCall) {
|
|
345
|
+
val ret = JSObject()
|
|
346
|
+
ret.put("id", OneSignal.User.pushSubscription.id)
|
|
347
|
+
call.resolve(ret)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
@PluginMethod
|
|
351
|
+
fun getPushSubscriptionToken(call: PluginCall) {
|
|
352
|
+
val ret = JSObject()
|
|
353
|
+
ret.put("token", OneSignal.User.pushSubscription.token)
|
|
354
|
+
call.resolve(ret)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
@PluginMethod
|
|
358
|
+
fun getPushSubscriptionOptedIn(call: PluginCall) {
|
|
359
|
+
val ret = JSObject()
|
|
360
|
+
ret.put("optedIn", OneSignal.User.pushSubscription.optedIn)
|
|
361
|
+
call.resolve(ret)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
@PluginMethod
|
|
365
|
+
fun optInPushSubscription(call: PluginCall) {
|
|
366
|
+
OneSignal.User.pushSubscription.optIn()
|
|
367
|
+
call.resolve()
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
@PluginMethod
|
|
371
|
+
fun optOutPushSubscription(call: PluginCall) {
|
|
372
|
+
OneSignal.User.pushSubscription.optOut()
|
|
373
|
+
call.resolve()
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// endregion
|
|
377
|
+
|
|
378
|
+
// region Notifications
|
|
379
|
+
|
|
380
|
+
@PluginMethod
|
|
381
|
+
fun getPermission(call: PluginCall) {
|
|
382
|
+
val ret = JSObject()
|
|
383
|
+
ret.put("permission", OneSignal.Notifications.permission)
|
|
384
|
+
call.resolve(ret)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
@PluginMethod
|
|
388
|
+
fun permissionNative(call: PluginCall) {
|
|
389
|
+
val ret = JSObject()
|
|
390
|
+
ret.put("permission", if (OneSignal.Notifications.permission) 2 else 1)
|
|
391
|
+
call.resolve(ret)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
@PluginMethod
|
|
395
|
+
fun requestPermission(call: PluginCall) {
|
|
396
|
+
val fallback = call.getBoolean("fallbackToSettings") ?: false
|
|
397
|
+
CoroutineScope(Dispatchers.Main).launch {
|
|
398
|
+
val accepted = OneSignal.Notifications.requestPermission(fallback)
|
|
399
|
+
val ret = JSObject()
|
|
400
|
+
ret.put("permission", accepted)
|
|
401
|
+
call.resolve(ret)
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
@PluginMethod
|
|
406
|
+
fun canRequestPermission(call: PluginCall) {
|
|
407
|
+
val ret = JSObject()
|
|
408
|
+
ret.put("canRequest", OneSignal.Notifications.canRequestPermission)
|
|
409
|
+
call.resolve(ret)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
@PluginMethod
|
|
413
|
+
fun registerForProvisionalAuthorization(call: PluginCall) {
|
|
414
|
+
val ret = JSObject()
|
|
415
|
+
ret.put("accepted", true)
|
|
416
|
+
call.resolve(ret)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
@PluginMethod
|
|
420
|
+
fun clearAllNotifications(call: PluginCall) {
|
|
421
|
+
OneSignal.Notifications.clearAllNotifications()
|
|
422
|
+
call.resolve()
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
@PluginMethod
|
|
426
|
+
fun removeNotification(call: PluginCall) {
|
|
427
|
+
val id = call.getInt("id") ?: run {
|
|
428
|
+
call.reject("id is required")
|
|
429
|
+
return
|
|
430
|
+
}
|
|
431
|
+
OneSignal.Notifications.removeNotification(id)
|
|
432
|
+
call.resolve()
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
@PluginMethod
|
|
436
|
+
fun removeGroupedNotifications(call: PluginCall) {
|
|
437
|
+
val id = call.getString("id")
|
|
438
|
+
if (id == null) {
|
|
439
|
+
call.reject("id is required")
|
|
440
|
+
return
|
|
441
|
+
}
|
|
442
|
+
OneSignal.Notifications.removeGroupedNotifications(id)
|
|
443
|
+
call.resolve()
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
@PluginMethod
|
|
447
|
+
fun preventDefault(call: PluginCall) {
|
|
448
|
+
val notificationId = call.getString("notificationId")
|
|
449
|
+
if (notificationId == null) {
|
|
450
|
+
call.reject("notificationId is required")
|
|
451
|
+
return
|
|
452
|
+
}
|
|
453
|
+
val event = notificationWillDisplayCache[notificationId]
|
|
454
|
+
if (event == null) {
|
|
455
|
+
call.reject("Could not find notification will display event")
|
|
456
|
+
return
|
|
457
|
+
}
|
|
458
|
+
event.preventDefault()
|
|
459
|
+
preventDefaultCache.add(notificationId)
|
|
460
|
+
call.resolve()
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
@PluginMethod
|
|
464
|
+
fun proceedWithWillDisplay(call: PluginCall) {
|
|
465
|
+
val notificationId = call.getString("notificationId")
|
|
466
|
+
if (notificationId == null) {
|
|
467
|
+
call.reject("notificationId is required")
|
|
468
|
+
return
|
|
469
|
+
}
|
|
470
|
+
val event = notificationWillDisplayCache[notificationId]
|
|
471
|
+
if (event == null) {
|
|
472
|
+
call.reject("Could not find notification will display event")
|
|
473
|
+
return
|
|
474
|
+
}
|
|
475
|
+
if (!preventDefaultCache.contains(notificationId)) {
|
|
476
|
+
event.notification.display()
|
|
477
|
+
}
|
|
478
|
+
call.resolve()
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
@PluginMethod
|
|
482
|
+
fun displayNotification(call: PluginCall) {
|
|
483
|
+
val notificationId = call.getString("notificationId")
|
|
484
|
+
if (notificationId == null) {
|
|
485
|
+
call.reject("notificationId is required")
|
|
486
|
+
return
|
|
487
|
+
}
|
|
488
|
+
val event = notificationWillDisplayCache[notificationId]
|
|
489
|
+
if (event == null) {
|
|
490
|
+
call.reject("Could not find notification will display event")
|
|
491
|
+
return
|
|
492
|
+
}
|
|
493
|
+
event.notification.display()
|
|
494
|
+
call.resolve()
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// endregion
|
|
498
|
+
|
|
499
|
+
// region In-App Messages
|
|
500
|
+
|
|
501
|
+
@PluginMethod
|
|
502
|
+
fun addTriggers(call: PluginCall) {
|
|
503
|
+
val triggersObj = call.getObject("triggers") ?: run {
|
|
504
|
+
call.reject("triggers is required")
|
|
505
|
+
return
|
|
506
|
+
}
|
|
507
|
+
val triggers = mutableMapOf<String, String>()
|
|
508
|
+
triggersObj.keys().forEach { key -> triggersObj.getString(key)?.let { triggers[key] = it } }
|
|
509
|
+
OneSignal.InAppMessages.addTriggers(triggers)
|
|
510
|
+
call.resolve()
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
@PluginMethod
|
|
514
|
+
fun removeTriggers(call: PluginCall) {
|
|
515
|
+
val keysArr = call.getArray("keys") ?: run {
|
|
516
|
+
call.reject("keys is required")
|
|
517
|
+
return
|
|
518
|
+
}
|
|
519
|
+
val keys = mutableListOf<String>()
|
|
520
|
+
for (i in 0 until keysArr.length()) {
|
|
521
|
+
keys.add(keysArr.getString(i))
|
|
522
|
+
}
|
|
523
|
+
OneSignal.InAppMessages.removeTriggers(keys)
|
|
524
|
+
call.resolve()
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
@PluginMethod
|
|
528
|
+
fun clearTriggers(call: PluginCall) {
|
|
529
|
+
OneSignal.InAppMessages.clearTriggers()
|
|
530
|
+
call.resolve()
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
@PluginMethod
|
|
534
|
+
fun setPaused(call: PluginCall) {
|
|
535
|
+
val pause = call.getBoolean("pause") ?: false
|
|
536
|
+
OneSignal.InAppMessages.paused = pause
|
|
537
|
+
call.resolve()
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
@PluginMethod
|
|
541
|
+
fun isPaused(call: PluginCall) {
|
|
542
|
+
val ret = JSObject()
|
|
543
|
+
ret.put("paused", OneSignal.InAppMessages.paused)
|
|
544
|
+
call.resolve(ret)
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// endregion
|
|
548
|
+
|
|
549
|
+
// region Session / Outcomes
|
|
550
|
+
|
|
551
|
+
@PluginMethod
|
|
552
|
+
fun addOutcome(call: PluginCall) {
|
|
553
|
+
val name = call.getString("name")
|
|
554
|
+
if (name == null) {
|
|
555
|
+
call.reject("name is required")
|
|
556
|
+
return
|
|
557
|
+
}
|
|
558
|
+
OneSignal.Session.addOutcome(name)
|
|
559
|
+
call.resolve()
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
@PluginMethod
|
|
563
|
+
fun addUniqueOutcome(call: PluginCall) {
|
|
564
|
+
val name = call.getString("name")
|
|
565
|
+
if (name == null) {
|
|
566
|
+
call.reject("name is required")
|
|
567
|
+
return
|
|
568
|
+
}
|
|
569
|
+
OneSignal.Session.addUniqueOutcome(name)
|
|
570
|
+
call.resolve()
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
@PluginMethod
|
|
574
|
+
fun addOutcomeWithValue(call: PluginCall) {
|
|
575
|
+
val name = call.getString("name")
|
|
576
|
+
if (name == null) {
|
|
577
|
+
call.reject("name is required")
|
|
578
|
+
return
|
|
579
|
+
}
|
|
580
|
+
val value = call.getFloat("value") ?: 0f
|
|
581
|
+
OneSignal.Session.addOutcomeWithValue(name, value)
|
|
582
|
+
call.resolve()
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// endregion
|
|
586
|
+
|
|
587
|
+
// region Location
|
|
588
|
+
|
|
589
|
+
@PluginMethod
|
|
590
|
+
fun requestLocationPermission(call: PluginCall) {
|
|
591
|
+
CoroutineScope(Dispatchers.Main).launch {
|
|
592
|
+
OneSignal.Location.requestPermission()
|
|
593
|
+
call.resolve()
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
@PluginMethod
|
|
598
|
+
fun setLocationShared(call: PluginCall) {
|
|
599
|
+
val shared = call.getBoolean("shared") ?: false
|
|
600
|
+
OneSignal.Location.isShared = shared
|
|
601
|
+
call.resolve()
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
@PluginMethod
|
|
605
|
+
fun isLocationShared(call: PluginCall) {
|
|
606
|
+
val ret = JSObject()
|
|
607
|
+
ret.put("shared", OneSignal.Location.isShared)
|
|
608
|
+
call.resolve(ret)
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// endregion
|
|
612
|
+
|
|
613
|
+
// region Live Activities (no-op on Android)
|
|
614
|
+
|
|
615
|
+
@PluginMethod
|
|
616
|
+
fun enterLiveActivity(call: PluginCall) {
|
|
617
|
+
call.resolve()
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
@PluginMethod
|
|
621
|
+
fun exitLiveActivity(call: PluginCall) {
|
|
622
|
+
call.resolve()
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
@PluginMethod
|
|
626
|
+
fun setPushToStartToken(call: PluginCall) {
|
|
627
|
+
call.resolve()
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
@PluginMethod
|
|
631
|
+
fun removePushToStartToken(call: PluginCall) {
|
|
632
|
+
call.resolve()
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
@PluginMethod
|
|
636
|
+
fun setupDefaultLiveActivity(call: PluginCall) {
|
|
637
|
+
call.resolve()
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
@PluginMethod
|
|
641
|
+
fun startDefaultLiveActivity(call: PluginCall) {
|
|
642
|
+
call.resolve()
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// endregion
|
|
646
|
+
|
|
647
|
+
// region Observer Callbacks
|
|
648
|
+
|
|
649
|
+
override fun onWillDisplay(event: INotificationWillDisplayEvent) {
|
|
650
|
+
val notificationId = event.notification.notificationId ?: return
|
|
651
|
+
notificationWillDisplayCache[notificationId] = event
|
|
652
|
+
event.preventDefault()
|
|
653
|
+
notifyListeners("notificationForegroundWillDisplay", serializeNotification(event.notification))
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
override fun onClick(event: INotificationClickEvent) {
|
|
657
|
+
if (bridge != null) {
|
|
658
|
+
val ret = JSObject()
|
|
659
|
+
val clickResult = JSObject()
|
|
660
|
+
clickResult.put("actionId", event.result.actionId)
|
|
661
|
+
clickResult.put("url", event.result.url)
|
|
662
|
+
ret.put("result", clickResult)
|
|
663
|
+
ret.put("notification", serializeNotification(event.notification))
|
|
664
|
+
notifyListeners("notificationClick", ret)
|
|
665
|
+
} else {
|
|
666
|
+
pendingClickEvent = event
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
private fun serializeNotification(notification: INotification): JSObject {
|
|
671
|
+
val json = JSObject()
|
|
672
|
+
json.put("notificationId", notification.notificationId)
|
|
673
|
+
json.put("title", notification.title)
|
|
674
|
+
json.put("body", notification.body)
|
|
675
|
+
json.put("sound", notification.sound)
|
|
676
|
+
json.put("launchURL", notification.launchURL)
|
|
677
|
+
json.put("rawPayload", notification.rawPayload)
|
|
678
|
+
json.put("actionButtons", notification.actionButtons)
|
|
679
|
+
json.put("additionalData", notification.additionalData)
|
|
680
|
+
json.put("groupKey", notification.groupKey)
|
|
681
|
+
json.put("groupMessage", notification.groupMessage)
|
|
682
|
+
json.put("groupedNotifications", notification.groupedNotifications)
|
|
683
|
+
json.put("ledColor", notification.ledColor)
|
|
684
|
+
json.put("priority", notification.priority)
|
|
685
|
+
json.put("smallIcon", notification.smallIcon)
|
|
686
|
+
json.put("largeIcon", notification.largeIcon)
|
|
687
|
+
json.put("bigPicture", notification.bigPicture)
|
|
688
|
+
json.put("collapseId", notification.collapseId)
|
|
689
|
+
json.put("fromProjectNumber", notification.fromProjectNumber)
|
|
690
|
+
json.put("smallIconAccentColor", notification.smallIconAccentColor)
|
|
691
|
+
json.put("lockScreenVisibility", notification.lockScreenVisibility)
|
|
692
|
+
json.put("androidNotificationId", notification.androidNotificationId)
|
|
693
|
+
return json
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
override fun onWillDisplay(event: IInAppMessageWillDisplayEvent) {
|
|
697
|
+
val ret = JSObject()
|
|
698
|
+
ret.put("message", JSObject().put("messageId", event.message.messageId))
|
|
699
|
+
notifyListeners("inAppMessageWillDisplay", ret)
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
override fun onDidDisplay(event: IInAppMessageDidDisplayEvent) {
|
|
703
|
+
val ret = JSObject()
|
|
704
|
+
ret.put("message", JSObject().put("messageId", event.message.messageId))
|
|
705
|
+
notifyListeners("inAppMessageDidDisplay", ret)
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
override fun onWillDismiss(event: IInAppMessageWillDismissEvent) {
|
|
709
|
+
val ret = JSObject()
|
|
710
|
+
ret.put("message", JSObject().put("messageId", event.message.messageId))
|
|
711
|
+
notifyListeners("inAppMessageWillDismiss", ret)
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
override fun onDidDismiss(event: IInAppMessageDidDismissEvent) {
|
|
715
|
+
val ret = JSObject()
|
|
716
|
+
ret.put("message", JSObject().put("messageId", event.message.messageId))
|
|
717
|
+
notifyListeners("inAppMessageDidDismiss", ret)
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
override fun onClick(event: IInAppMessageClickEvent) {
|
|
721
|
+
val urlTarget = event.result.urlTarget?.toString() ?: "browser"
|
|
722
|
+
|
|
723
|
+
val clickResult = JSObject()
|
|
724
|
+
clickResult.put("closingMessage", event.result.closingMessage)
|
|
725
|
+
clickResult.put("actionId", event.result.actionId)
|
|
726
|
+
clickResult.put("url", event.result.url)
|
|
727
|
+
clickResult.put("urlTarget", urlTarget)
|
|
728
|
+
|
|
729
|
+
val ret = JSObject()
|
|
730
|
+
ret.put("message", JSObject().put("messageId", event.message.messageId))
|
|
731
|
+
ret.put("result", clickResult)
|
|
732
|
+
notifyListeners("inAppMessageClick", ret)
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// endregion
|
|
736
|
+
}
|