Skip to content

Commit

Permalink
only refresh User when app is foregrounded
Browse files Browse the repository at this point in the history
Ensure cache for the user is refreshed once per cold start when app
is in the foreground. This saves resources as there are a number of
events (such as push received or non-OneSignal events) that start
the app in the background but will never read/write any user
properties.
  • Loading branch information
jkasten2 committed Mar 27, 2024
1 parent 92a32fa commit 94fe2a8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,6 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
}
} else {
Logging.debug("initWithContext: using cached user ${identityModelStore!!.model.onesignalId}")
operationRepo!!.enqueue(
RefreshUserOperation(
configModel!!.appId,
identityModelStore!!.model.onesignalId,
),
)
}

startupService!!.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.onesignal.common.modules.IModule
import com.onesignal.common.services.ServiceBuilder
import com.onesignal.core.internal.operations.IOperationExecutor
import com.onesignal.core.internal.startup.IBootstrapService
import com.onesignal.core.internal.startup.IStartableService
import com.onesignal.user.internal.UserManager
import com.onesignal.user.internal.backend.IIdentityBackendService
import com.onesignal.user.internal.backend.ISubscriptionBackendService
Expand All @@ -24,6 +25,7 @@ import com.onesignal.user.internal.operations.impl.listeners.IdentityModelStoreL
import com.onesignal.user.internal.operations.impl.listeners.PropertiesModelStoreListener
import com.onesignal.user.internal.operations.impl.listeners.SubscriptionModelStoreListener
import com.onesignal.user.internal.properties.PropertiesModelStore
import com.onesignal.user.internal.service.UserRefreshService
import com.onesignal.user.internal.subscriptions.ISubscriptionManager
import com.onesignal.user.internal.subscriptions.SubscriptionModelStore
import com.onesignal.user.internal.subscriptions.impl.SubscriptionManager
Expand Down Expand Up @@ -61,5 +63,7 @@ internal class UserModule : IModule {
builder.register<LoginUserFromSubscriptionOperationExecutor>().provides<IOperationExecutor>()
builder.register<RefreshUserOperationExecutor>().provides<IOperationExecutor>()
builder.register<UserManager>().provides<IUserManager>()

builder.register<UserRefreshService>().provides<IStartableService>()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.onesignal.user.internal.service

import com.onesignal.core.internal.application.IApplicationLifecycleHandler
import com.onesignal.core.internal.application.IApplicationService
import com.onesignal.core.internal.config.ConfigModelStore
import com.onesignal.core.internal.operations.IOperationRepo
import com.onesignal.core.internal.startup.IStartableService
import com.onesignal.user.internal.identity.IdentityModelStore
import com.onesignal.user.internal.operations.RefreshUserOperation

// Ensure cache for the user is refreshed once per cold start when app
// is in the foreground. This saves resources as there are a number of
// events (such as push received or non-OneSignal events) that start
// the app in the background but will never read/write any user
// properties.
class UserRefreshService(
private val _applicationService: IApplicationService,
private val _operationRepo: IOperationRepo,
private val _configModelStore: ConfigModelStore,
private val _identityModelStore: IdentityModelStore,
) : IStartableService,
IApplicationLifecycleHandler {
private fun refreshUser() {
_operationRepo.enqueue(
RefreshUserOperation(
_configModelStore.model.appId,
_identityModelStore.model.onesignalId,
),
)
}

override fun start() {
if (_applicationService.isInForeground) {
refreshUser()
} else {
_applicationService.addApplicationLifecycleHandler(this)
}
}

private var onFocusCalled: Boolean = false

override fun onFocus() {
if (onFocusCalled) return
onFocusCalled = true
refreshUser()
}

override fun onUnfocused() { }
}

0 comments on commit 94fe2a8

Please sign in to comment.