Skip to content
Open
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ dataStore = "1.2.1"
datastoreCore = "1.2.1"
datastorePreferencesRxjava2 = "1.2.1"
datastorePreferencesRxjava3 = "1.2.1"
engageCore = "1.5.12"
engageCore = "1.6.0"
firebase-ai = "17.13.0"
firebase-bom = "34.15.0"
firebase-ondevice = "16.0.0-beta03"
Expand Down
24 changes: 18 additions & 6 deletions misc/src/main/java/com/example/snippets/engage/EngageWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import android.content.Context
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.google.android.engage.common.datamodel.ClusterType
import com.google.android.engage.service.AppEngageErrorCode
import com.google.android.engage.service.AppEngageException
import com.google.android.engage.service.AppEngagePublishClient
import com.google.android.engage.service.AppEngagePublishStatusCode
import com.google.android.engage.service.PublishStatusRequest
import com.google.android.engage.service.ServiceAvailabilityRequest
import com.google.android.gms.tasks.Task
import kotlinx.coroutines.tasks.await

Expand All @@ -47,15 +49,25 @@ class EngageWorker(context: Context, workerParams: WorkerParameters) : Coroutine
return Result.failure()
}

// Check if engage service is available before publishing.
val isAvailable = client.isServiceAvailable.await()
val publishType = inputData.getString(Constants.PUBLISH_TYPE_KEY)
val intendedClusterType = when (publishType) {
Constants.PUBLISH_TYPE_RECOMMENDATIONS -> ClusterType.TYPE_RECOMMENDATION
Constants.PUBLISH_TYPE_FEATURED -> ClusterType.TYPE_FEATURED
Constants.PUBLISH_TYPE_CONTINUATION -> ClusterType.TYPE_CONTINUATION
Constants.PUBLISH_TYPE_USER_ACCOUNT_MANAGEMENT -> ClusterType.TYPE_ENGAGEMENT
else -> ClusterType.TYPE_UNKNOWN
}

// If the service is not available, do not attempt to publish and indicate failure.
if (!isAvailable) {
return Result.failure()
if (intendedClusterType != ClusterType.TYPE_UNKNOWN) {
val request = ServiceAvailabilityRequest.Builder()
.addIntendedClusterType(intendedClusterType)
.build()
val availabilityMap = client.isServiceAvailable(request).await()
if (availabilityMap[intendedClusterType] != true) {
return Result.failure()
}
}
Comment thread
amulyagaur marked this conversation as resolved.

val publishType = inputData.getString(Constants.PUBLISH_TYPE_KEY)
return when (publishType) {
Constants.PUBLISH_TYPE_RECOMMENDATIONS -> publishRecommendations()
// Constants.PUBLISH_TYPE_FEATURED -> publishFeatured()
Expand Down