Skip to content

Commit 3e39c0a

Browse files
authored
Merge pull request #1770 from OneSignal/user-model/add_to_json_object
User model - add toJSONObject for push subscription event parameters
2 parents 3611eaa + 5064005 commit 3e39c0a

File tree

12 files changed

+84
-57
lines changed

12 files changed

+84
-57
lines changed

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/activity/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void onBackPressed() {
4141
protected void onResume() {
4242
super.onResume();
4343

44-
boolean hasConsent = OneSignal.getRequiresPrivacyConsent();
44+
boolean hasConsent = OneSignal.getConsentGiven();
4545
if (hasConsent)
4646
viewModel.setupLayout();
4747
}

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/MainActivityViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ private String getOneSignalAppId() {
884884
}
885885

886886
private void togglePrivacyConsent(boolean hasConsent) {
887-
OneSignal.setPrivacyConsent(hasConsent);
887+
OneSignal.setConsentGiven(hasConsent);
888888
SharedPreferenceUtil.cacheUserPrivacyConsent(context, hasConsent);
889889

890890
shouldScrollTop = hasConsent;

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/SplashActivityViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void networkDisconnected() {
6363
private void setupOneSignalSDK() {
6464
boolean privacyConsent = true;
6565

66-
OneSignal.setRequiresPrivacyConsent(privacyConsent);
66+
OneSignal.setConsentRequired(privacyConsent);
6767

6868
boolean isLocationShared = SharedPreferenceUtil.getCachedLocationSharedStatus(context);
6969
OneSignal.getLocation().setShared(isLocationShared);

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/IOneSignal.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ interface IOneSignal {
6262
* should be set to `true` prior to the invocation of
6363
* [initWithContext] to ensure compliance.
6464
*/
65-
var requiresPrivacyConsent: Boolean
65+
var consentRequired: Boolean
6666

6767
/**
6868
* Indicates whether privacy consent has been granted. This field is only relevant when
69-
* the application has opted into data privacy protections. See [requiresPrivacyConsent].
69+
* the application has opted into data privacy protections. See [consentRequired].
7070
*/
71-
var privacyConsent: Boolean
71+
var consentGiven: Boolean
7272

7373
/**
7474
* Whether to disable the "GMS is missing" prompt to the user.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/OneSignal.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ object OneSignal {
9494
* [initWithContext] to ensure compliance.
9595
*/
9696
@JvmStatic
97-
var requiresPrivacyConsent: Boolean
98-
get() = oneSignal.requiresPrivacyConsent
99-
set(value) { oneSignal.requiresPrivacyConsent = value }
97+
var consentRequired: Boolean
98+
get() = oneSignal.consentRequired
99+
set(value) { oneSignal.consentRequired = value }
100100

101101
/**
102102
* Indicates whether privacy consent has been granted. This field is only relevant when
103103
* the application has opted into data privacy protections. See [requiresPrivacyConsent].
104104
*/
105105
@JvmStatic
106-
var privacyConsent: Boolean
107-
get() = oneSignal.privacyConsent
108-
set(value) { oneSignal.privacyConsent = value }
106+
var consentGiven: Boolean
107+
get() = oneSignal.consentGiven
108+
set(value) { oneSignal.consentGiven = value }
109109

110110
/**
111111
* Whether to disable the "GMS is missing" prompt to the user.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/config/ConfigModel.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ class ConfigModel : Model() {
3636
/**
3737
* Whether the SDK requires privacy consent to send data to backend.
3838
*/
39-
var requiresPrivacyConsent: Boolean?
40-
get() = getOptBooleanProperty(::requiresPrivacyConsent.name)
41-
set(value) { setOptBooleanProperty(::requiresPrivacyConsent.name, value) }
39+
var consentRequired: Boolean?
40+
get() = getOptBooleanProperty(::consentRequired.name)
41+
set(value) { setOptBooleanProperty(::consentRequired.name, value) }
4242

4343
/**
4444
* Whether the SDK has been given consent to privacy.
4545
*/
46-
var givenPrivacyConsent: Boolean?
47-
get() = getOptBooleanProperty(::givenPrivacyConsent.name)
48-
set(value) { setOptBooleanProperty(::givenPrivacyConsent.name, value) }
46+
var consentGiven: Boolean?
47+
get() = getOptBooleanProperty(::consentGiven.name)
48+
set(value) { setOptBooleanProperty(::consentGiven.name, value) }
4949

5050
/**
5151
* Whether location is shared.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/config/impl/ConfigModelStoreListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ internal class ConfigModelStoreListener(
8888
params.disableGMSMissingPrompt?.let { config.disableGMSMissingPrompt = it }
8989
params.unsubscribeWhenNotificationsDisabled?.let { config.unsubscribeWhenNotificationsDisabled = it }
9090
params.locationShared?.let { config.locationShared = it }
91-
params.requiresUserPrivacyConsent?.let { config.requiresPrivacyConsent = it }
91+
params.requiresUserPrivacyConsent?.let { config.consentRequired = it }
9292
params.opRepoExecutionInterval?.let { config.opRepoExecutionInterval = it }
9393
params.influenceParams.notificationLimit?.let { config.influenceParams.notificationLimit = it }
9494
params.influenceParams.indirectNotificationAttributionWindow?.let { config.influenceParams.indirectNotificationAttributionWindow = it }

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/http/impl/HttpClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal class HttpClient(
5858
cacheKey: String?,
5959
): HttpResponse {
6060
// If privacy consent is required but not yet given, any non-GET request should be blocked.
61-
if (method != null && _configModelStore.model.requiresPrivacyConsent == true && _configModelStore.model.givenPrivacyConsent != true) {
61+
if (method != null && _configModelStore.model.consentRequired == true && _configModelStore.model.consentGiven != true) {
6262
Logging.warn("$method `$url` was called before the user provided privacy consent. Your application is set to require the user's privacy consent before the OneSignal SDK can be initialized. Please ensure the user has provided consent before calling this method. You can check the latest OneSignal consent status by calling OneSignal.privacyConsent")
6363
return HttpResponse(0, null, null)
6464
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
5353
override val sdkVersion: String = OneSignalUtils.sdkVersion
5454
override var isInitialized: Boolean = false
5555

56-
override var requiresPrivacyConsent: Boolean
57-
get() = _configModel?.requiresPrivacyConsent ?: (_requiresPrivacyConsent == true)
56+
override var consentRequired: Boolean
57+
get() = _configModel?.consentRequired ?: (_consentRequired == true)
5858
set(value) {
59-
_requiresPrivacyConsent = value
60-
_configModel?.requiresPrivacyConsent = value
59+
_consentRequired = value
60+
_configModel?.consentRequired = value
6161
}
6262

63-
override var privacyConsent: Boolean
64-
get() = _configModel?.givenPrivacyConsent ?: (_givenPrivacyConsent == true)
63+
override var consentGiven: Boolean
64+
get() = _configModel?.consentGiven ?: (_consentGiven == true)
6565
set(value) {
66-
_givenPrivacyConsent = value
67-
_configModel?.givenPrivacyConsent = value
66+
_consentGiven = value
67+
_configModel?.consentGiven = value
6868
}
6969

7070
override var disableGMSMissingPrompt: Boolean
@@ -99,8 +99,8 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
9999
private val _services: ServiceProvider
100100
private var _configModel: ConfigModel? = null
101101
private var _sessionModel: SessionModel? = null
102-
private var _requiresPrivacyConsent: Boolean? = null
103-
private var _givenPrivacyConsent: Boolean? = null
102+
private var _consentRequired: Boolean? = null
103+
private var _consentGiven: Boolean? = null
104104
private var _disableGMSMissingPrompt: Boolean? = null
105105
private val _loginLock: Any = Any()
106106

@@ -174,13 +174,13 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
174174
}
175175

176176
// if requires privacy consent was set prior to init, set it in the model now
177-
if (_requiresPrivacyConsent != null) {
178-
_configModel!!.requiresPrivacyConsent = _requiresPrivacyConsent!!
177+
if (_consentRequired != null) {
178+
_configModel!!.consentRequired = _consentRequired!!
179179
}
180180

181181
// if privacy consent was set prior to init, set it in the model now
182-
if (_givenPrivacyConsent != null) {
183-
_configModel!!.givenPrivacyConsent = _givenPrivacyConsent!!
182+
if (_consentGiven != null) {
183+
_configModel!!.consentGiven = _consentGiven!!
184184
}
185185

186186
if (_disableGMSMissingPrompt != null) {
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package com.onesignal.user.subscriptions
22

3+
import org.json.JSONObject
4+
35
class PushSubscriptionChangedState(
46
val previous: PushSubscriptionState,
57
val current: PushSubscriptionState
6-
)
8+
) {
9+
fun toJSONObject(): JSONObject {
10+
return JSONObject()
11+
.put("previous", previous.toJSONObject())
12+
.put("current", current.toJSONObject())
13+
}
14+
}
15+

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/subscriptions/PushSubscriptionState.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.onesignal.user.subscriptions
22

3+
import org.json.JSONObject
4+
35
/**
46
* A subscription state.
57
*/
@@ -24,5 +26,14 @@ class PushSubscriptionState(
2426
* user will not receive notifications through this subscription (even when the user has
2527
* granted app permission).
2628
*/
27-
val optedIn: Boolean
28-
)
29+
val optedIn: Boolean,
30+
) {
31+
fun toJSONObject(): JSONObject {
32+
return JSONObject()
33+
.put("id", id)
34+
.put("token", token)
35+
.put("optedIn", optedIn)
36+
}
37+
}
38+
39+

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/session/internal/outcomes/OutcomeEventsControllerTests.kt

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class OutcomeEventsControllerTests : FunSpec({
4343
Logging.logLevel = LogLevel.NONE
4444
}
4545

46+
fun createTestSubscriptionModel(): SubscriptionModel {
47+
val subModel = SubscriptionModel()
48+
subModel.id = "subscriptionId"
49+
subModel.address = "subscriptionAddress"
50+
subModel.optedIn = true
51+
return subModel
52+
}
53+
4654
test("send outcome with disabled influences") {
4755
/* Given */
4856
val now = 111L
@@ -86,8 +94,7 @@ class OutcomeEventsControllerTests : FunSpec({
8694
val mockInfluenceManager = mockk<IInfluenceManager>()
8795
every { mockInfluenceManager.influences } returns listOf(Influence(InfluenceChannel.NOTIFICATION, InfluenceType.UNATTRIBUTED, null))
8896

89-
val subscriptionModel = SubscriptionModel()
90-
subscriptionModel.id = "subscriptionId"
97+
val subscriptionModel = createTestSubscriptionModel()
9198

9299
val mockSubscriptionManager = mockk<ISubscriptionManager>()
93100
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
@@ -132,8 +139,8 @@ class OutcomeEventsControllerTests : FunSpec({
132139
val mockInfluenceManager = mockk<IInfluenceManager>()
133140
every { mockInfluenceManager.influences } returns listOf(Influence(InfluenceChannel.NOTIFICATION, InfluenceType.INDIRECT, JSONArray(notificationIds)))
134141

135-
val subscriptionModel = SubscriptionModel()
136-
subscriptionModel.id = "subscriptionId"
142+
val subscriptionModel = createTestSubscriptionModel()
143+
137144
val mockSubscriptionManager = mockk<ISubscriptionManager>()
138145
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
139146

@@ -178,8 +185,8 @@ class OutcomeEventsControllerTests : FunSpec({
178185
val mockInfluenceManager = mockk<IInfluenceManager>()
179186
every { mockInfluenceManager.influences } returns listOf(Influence(InfluenceChannel.NOTIFICATION, InfluenceType.DIRECT, JSONArray(notificationIds)))
180187

181-
val subscriptionModel = SubscriptionModel()
182-
subscriptionModel.id = "subscriptionId"
188+
val subscriptionModel = createTestSubscriptionModel()
189+
183190
val mockSubscriptionManager = mockk<ISubscriptionManager>()
184191
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
185192

@@ -224,8 +231,8 @@ class OutcomeEventsControllerTests : FunSpec({
224231
val mockInfluenceManager = mockk<IInfluenceManager>()
225232
every { mockInfluenceManager.influences } returns listOf(Influence(InfluenceChannel.NOTIFICATION, InfluenceType.UNATTRIBUTED, null))
226233

227-
val subscriptionModel = SubscriptionModel()
228-
subscriptionModel.id = "subscriptionId"
234+
val subscriptionModel = createTestSubscriptionModel()
235+
229236
val mockSubscriptionManager = mockk<ISubscriptionManager>()
230237
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
231238

@@ -268,8 +275,8 @@ class OutcomeEventsControllerTests : FunSpec({
268275
val mockInfluenceManager = mockk<IInfluenceManager>()
269276
every { mockInfluenceManager.influences } returns listOf(Influence(InfluenceChannel.NOTIFICATION, InfluenceType.UNATTRIBUTED, null))
270277

271-
val subscriptionModel = SubscriptionModel()
272-
subscriptionModel.id = "subscriptionId"
278+
val subscriptionModel = createTestSubscriptionModel()
279+
273280
val mockSubscriptionManager = mockk<ISubscriptionManager>()
274281
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
275282

@@ -322,8 +329,8 @@ class OutcomeEventsControllerTests : FunSpec({
322329
coEvery { mockOutcomeEventsRepository.getNotCachedUniqueInfluencesForOutcome("OUTCOME_1", any()) } returns listOf(notificationInfluence) andThen listOf()
323330
coEvery { mockOutcomeEventsRepository.saveUniqueOutcomeEventParams(any()) } answers { waiter.wake() }
324331

325-
val subscriptionModel = SubscriptionModel()
326-
subscriptionModel.id = "subscriptionId"
332+
val subscriptionModel = createTestSubscriptionModel()
333+
327334
val mockSubscriptionManager = mockk<ISubscriptionManager>()
328335
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
329336

@@ -376,8 +383,8 @@ class OutcomeEventsControllerTests : FunSpec({
376383
val notificationInfluence2 = Influence(InfluenceChannel.NOTIFICATION, InfluenceType.DIRECT, JSONArray(notificationIds2))
377384
every { mockInfluenceManager.influences } returns listOf(notificationInfluence1) andThen listOf(notificationInfluence2)
378385

379-
val subscriptionModel = SubscriptionModel()
380-
subscriptionModel.id = "subscriptionId"
386+
val subscriptionModel = createTestSubscriptionModel()
387+
381388
val mockSubscriptionManager = mockk<ISubscriptionManager>()
382389
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
383390

@@ -438,8 +445,8 @@ class OutcomeEventsControllerTests : FunSpec({
438445
val mockInfluenceManager = mockk<IInfluenceManager>()
439446
every { mockInfluenceManager.influences } returns listOf(Influence(InfluenceChannel.NOTIFICATION, InfluenceType.UNATTRIBUTED, null))
440447

441-
val subscriptionModel = SubscriptionModel()
442-
subscriptionModel.id = "subscriptionId"
448+
val subscriptionModel = createTestSubscriptionModel()
449+
443450
val mockSubscriptionManager = mockk<ISubscriptionManager>()
444451
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
445452

@@ -483,8 +490,8 @@ class OutcomeEventsControllerTests : FunSpec({
483490
val mockSessionService = mockk<ISessionService>()
484491
every { mockSessionService.subscribe(any()) } just Runs
485492

486-
val subscriptionModel = SubscriptionModel()
487-
subscriptionModel.id = "subscriptionId"
493+
val subscriptionModel = createTestSubscriptionModel()
494+
488495
val mockSubscriptionManager = mockk<ISubscriptionManager>()
489496
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
490497

@@ -571,8 +578,8 @@ class OutcomeEventsControllerTests : FunSpec({
571578
val mockSessionService = mockk<ISessionService>()
572579
every { mockSessionService.subscribe(any()) } just Runs
573580

574-
val subscriptionModel = SubscriptionModel()
575-
subscriptionModel.id = "subscriptionId"
581+
val subscriptionModel = createTestSubscriptionModel()
582+
576583
val mockSubscriptionManager = mockk<ISubscriptionManager>()
577584
every { mockSubscriptionManager.subscriptions.push } returns PushSubscription(subscriptionModel)
578585

0 commit comments

Comments
 (0)