Skip to content

Commit

Permalink
Merge pull request #1922 from OneSignal/update_between_sessions
Browse files Browse the repository at this point in the history
Update push subscription model properties between sessions
  • Loading branch information
nan-li authored Dec 1, 2023
2 parents a5bfd21 + 7c8167d commit fef3aad
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.onesignal.internal

import android.content.Context
import android.os.Build
import com.onesignal.IOneSignal
import com.onesignal.common.AndroidUtils
import com.onesignal.common.DeviceUtils
import com.onesignal.common.IDManager
import com.onesignal.common.OneSignalUtils
import com.onesignal.common.modeling.ModelChangeTags
Expand Down Expand Up @@ -285,6 +288,12 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
legacyUserSyncJSON.safeString("identifier") ?: ""
pushSubscriptionModel.status = SubscriptionStatus.fromInt(notificationTypes)
?: SubscriptionStatus.NO_PERMISSION

pushSubscriptionModel.sdk = OneSignalUtils.SDK_VERSION
pushSubscriptionModel.deviceOS = Build.VERSION.RELEASE
pushSubscriptionModel.carrier = DeviceUtils.getCarrierName(services.getService<IApplicationService>().appContext) ?: ""
pushSubscriptionModel.appVersion = AndroidUtils.getAppVersion(services.getService<IApplicationService>().appContext) ?: ""

configModel!!.pushSubscriptionId = legacyPlayerId
subscriptionModelStore!!.add(
pushSubscriptionModel,
Expand Down Expand Up @@ -466,6 +475,10 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
newPushSubscription.optedIn = currentPushSubscription?.optedIn ?: true
newPushSubscription.address = currentPushSubscription?.address ?: ""
newPushSubscription.status = currentPushSubscription?.status ?: SubscriptionStatus.NO_PERMISSION
newPushSubscription.sdk = OneSignalUtils.SDK_VERSION
newPushSubscription.deviceOS = Build.VERSION.RELEASE
newPushSubscription.carrier = DeviceUtils.getCarrierName(services.getService<IApplicationService>().appContext) ?: ""
newPushSubscription.appVersion = AndroidUtils.getAppVersion(services.getService<IApplicationService>().appContext) ?: ""

// ensure we always know this devices push subscription ID
configModel!!.pushSubscriptionId = newPushSubscription.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ internal class RefreshUserOperationExecutor(
}
}
subscriptionModel.optedIn = subscriptionModel.status != SubscriptionStatus.UNSUBSCRIBE
subscriptionModel.sdk = subscription.sdk ?: ""
subscriptionModel.deviceOS = subscription.deviceOS ?: ""
subscriptionModel.carrier = subscription.carrier ?: ""
subscriptionModel.appVersion = subscription.appVersion ?: ""

// We only add a push subscription if it is this device's push subscription.
if (subscriptionModel.type != SubscriptionType.PUSH || subscriptionModel.id == _configModelStore.model.pushSubscriptionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ISubscriptionManager : IEventNotifier<ISubscriptionChangedHandler> {

fun addEmailSubscription(email: String)

fun addOrUpdatePushSubscription(
fun addOrUpdatePushSubscriptionToken(
pushToken: String?,
pushTokenStatus: SubscriptionStatus,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,29 @@ class SubscriptionModel : Model() {
set(value) {
setEnumProperty(::status.name, value)
}

// Prior to v5.0.5, we did not save the following properties, so we must default get() to ""
var sdk: String
get() = getStringProperty(::sdk.name) { "" }
set(value) {
setStringProperty(::sdk.name, value)
}

var deviceOS: String
get() = getStringProperty(::deviceOS.name) { "" }
set(value) {
setStringProperty(::deviceOS.name, value)
}

var carrier: String
get() = getStringProperty(::carrier.name) { "" }
set(value) {
setStringProperty(::carrier.name, value)
}

var appVersion: String
get() = getStringProperty(::appVersion.name) { "" }
set(value) {
setStringProperty(::appVersion.name, value)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
package com.onesignal.user.internal.subscriptions

import com.onesignal.common.modeling.ModelChangeTags
import com.onesignal.common.modeling.SimpleModelStore
import com.onesignal.core.internal.preferences.IPreferencesService

open class SubscriptionModelStore(prefs: IPreferencesService) : SimpleModelStore<SubscriptionModel>({
SubscriptionModel()
}, "subscriptions", prefs)
}, "subscriptions", prefs) {
override fun replaceAll(
models: List<SubscriptionModel>,
tag: String,
) {
if (tag != ModelChangeTags.HYDRATE) {
return super.replaceAll(models, tag)
}
// When hydrating an existing push subscription model, use existing device properties
synchronized(models) {
for (model in models) {
if (model.type == SubscriptionType.PUSH) {
val existingPushModel = get(model.id)
if (existingPushModel != null) {
model.sdk = existingPushModel.sdk
model.deviceOS = existingPushModel.deviceOS
model.carrier = existingPushModel.carrier
model.appVersion = existingPushModel.appVersion
}
break
}
}
super.replaceAll(models, tag)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.onesignal.user.internal.subscriptions.impl

import android.os.Build
import com.onesignal.common.AndroidUtils
import com.onesignal.common.DeviceUtils
import com.onesignal.common.IDManager
import com.onesignal.common.OneSignalUtils
import com.onesignal.common.events.EventProducer
import com.onesignal.common.modeling.IModelStoreChangeHandler
import com.onesignal.common.modeling.ModelChangedArgs
import com.onesignal.core.internal.application.IApplicationService
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
import com.onesignal.session.internal.session.ISessionLifecycleHandler
import com.onesignal.session.internal.session.ISessionService
import com.onesignal.user.internal.EmailSubscription
import com.onesignal.user.internal.PushSubscription
import com.onesignal.user.internal.SmsSubscription
Expand All @@ -32,8 +39,10 @@ import com.onesignal.user.subscriptions.PushSubscriptionChangedState
* subscription model.
*/
internal class SubscriptionManager(
private val _applicationService: IApplicationService,
private val _sessionService: ISessionService,
private val _subscriptionModelStore: SubscriptionModelStore,
) : ISubscriptionManager, IModelStoreChangeHandler<SubscriptionModel> {
) : ISubscriptionManager, IModelStoreChangeHandler<SubscriptionModel>, ISessionLifecycleHandler {
private val events = EventProducer<ISubscriptionChangedHandler>()
override var subscriptions: SubscriptionList = SubscriptionList(listOf(), UninitializedPushSubscription())
override val pushSubscriptionModel: SubscriptionModel
Expand All @@ -45,8 +54,17 @@ internal class SubscriptionManager(
}

_subscriptionModelStore.subscribe(this)
_sessionService.subscribe(this)
}

override fun onSessionStarted() {
refreshPushSubscriptionState()
}

override fun onSessionActive() { }

override fun onSessionEnded(duration: Long) { }

override fun addEmailSubscription(email: String) {
addSubscriptionToModels(SubscriptionType.EMAIL, email)
}
Expand All @@ -55,7 +73,7 @@ internal class SubscriptionManager(
addSubscriptionToModels(SubscriptionType.SMS, sms)
}

override fun addOrUpdatePushSubscription(
override fun addOrUpdatePushSubscriptionToken(
pushToken: String?,
pushTokenStatus: SubscriptionStatus,
) {
Expand Down Expand Up @@ -215,4 +233,29 @@ internal class SubscriptionManager(
}
}
}

/**
* Called when app has gained focus and the subscription state should be refreshed.
*/
private fun refreshPushSubscriptionState() {
val pushSub = subscriptions.push

if (pushSub is UninitializedPushSubscription) {
return
}
val pushSubModel = (pushSub as Subscription).model

pushSubModel.sdk = OneSignalUtils.SDK_VERSION
pushSubModel.deviceOS = Build.VERSION.RELEASE

val carrier = DeviceUtils.getCarrierName(_applicationService.appContext)
carrier?.let {
pushSubModel.carrier = carrier
}

val appVersion = AndroidUtils.getAppVersion(_applicationService.appContext)
appVersion?.let {
pushSubModel.appVersion = appVersion
}
}
}
Loading

0 comments on commit fef3aad

Please sign in to comment.