Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: disable FCM test by default #196

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ android {
def API_ENDPOINT = properties.getProperty("api_endpoint") ?: System.getenv("API_ENDPOINT")
buildConfigField("String", "API_KEY", "\"${API_KEY}\"")
buildConfigField("String", "API_ENDPOINT", "\"${API_ENDPOINT}\"")

setProperty("archivesBaseName", "BKT_v$versionName($versionCode)")
}
signingConfigs {
release {
Expand Down Expand Up @@ -72,7 +74,8 @@ dependencies {

implementation libs.kotlin.coroutines.android
implementation(platform(libs.firebase))
implementation libs.firebase.messaging.ktx
//uncomment for test FCM realtime update
//implementation libs.firebase.messaging.ktx
}
repositories {
mavenCentral()
Expand Down
50 changes: 28 additions & 22 deletions sample/src/main/java/io/bucketeer/sample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package io.bucketeer.sample

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.google.android.material.textfield.TextInputLayout
import com.google.firebase.ktx.Firebase
import com.google.firebase.messaging.ktx.messaging
import io.bucketeer.sdk.android.BKTClient
import io.bucketeer.sdk.android.BKTConfig
import io.bucketeer.sdk.android.BKTException
Expand All @@ -27,7 +26,6 @@ import io.bucketeer.sdk.android.sample.BuildConfig
import io.bucketeer.sdk.android.sample.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -67,10 +65,15 @@ class MainActivity : ComponentActivity() {
}

private lateinit var variantTypeSpinner: Spinner
private lateinit var appVersionTextView: TextView

@SuppressLint("SetTextI18n")
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
appVersionTextView = findViewById(R.id.appVersionTv)
appVersionTextView.text = "${BuildConfig.VERSION_NAME}-b${BuildConfig.VERSION_CODE}"

variantTypeSpinner = findViewById(R.id.spinner)
val adapter =
ArrayAdapter(
Expand Down Expand Up @@ -262,7 +265,7 @@ class MainActivity : ComponentActivity() {
.apiKey(BuildConfig.API_KEY)
.apiEndpoint(BuildConfig.API_ENDPOINT)
.featureTag(getTag())
.appVersion(BuildConfig.VERSION_NAME)
.appVersion(BuildConfig.VERSION_CODE.toString())
.eventsMaxQueueSize(10)
.pollingInterval(TimeUnit.SECONDS.toMillis(20))
.backgroundPollingInterval(TimeUnit.SECONDS.toMillis(60))
Expand All @@ -273,6 +276,7 @@ class MainActivity : ComponentActivity() {
BKTUser
.builder()
.id(getUserId())
.customAttributes(mapOf("app_version" to BuildConfig.VERSION_CODE.toString()))
.build()

val future = BKTClient.initialize(this, config, user)
Expand Down Expand Up @@ -320,28 +324,30 @@ class MainActivity : ComponentActivity() {
}

private suspend fun onGrantedNotificationPermission() {
val token = Firebase.messaging.token.await()
println("FCM Token $token")

subscribeToTopic()
// uncomment for test FCM realtime update
// val token = Firebase.messaging.token.await()
// println("FCM Token $token")
//
// subscribeToTopic()
}

// In order to receive the update notification when the flag value changed
// We need subscribe to topic, with the topic name is in this format bucketeer-<YOUR_FEATURE_TAG>
// Please put your Firebase project's google-services.json under the folder `sample/src` before test this.
private fun subscribeToTopic() {
val tag = getTag()
Firebase.messaging
.subscribeToTopic("bucketeer-$tag")
.addOnCompleteListener { task ->
var msg = "Real time update enabled"
if (!task.isSuccessful) {
msg = "Subscribe to real time update failed"
}
Log.d(TAG, msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
}
}

// private fun subscribeToTopic() {
// val tag = getTag()
// Firebase.messaging
// .subscribeToTopic("bucketeer-$tag")
// .addOnCompleteListener { task ->
// var msg = "Real time update enabled"
// if (!task.isSuccessful) {
// msg = "Subscribe to real time update failed"
// }
// Log.d(TAG, msg)
// Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
// }
// }

companion object {
const val TAG = "MainActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
package io.bucketeer.sample
// uncomment for test FCM realtime update

import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import io.bucketeer.sdk.android.BKTClient

class MyFirebaseMessagingService : FirebaseMessagingService() {
// [START receive_message]
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "From: ${remoteMessage.from}")

remoteMessage.data.also { data ->
Log.d(TAG, "Message data payload: ${remoteMessage.data}")
val isFeatureFlagUpdated = data["bucketeer_feature_flag_updated"]
if (isFeatureFlagUpdated == "true") {
// Feature flag changed
Log.d(TAG, "Bucketeer feature flag changed")

runCatching {
// Make sure BKTClient has been initialize before access it
val client = BKTClient.getInstance()
val future = client.fetchEvaluations(3000)
val error = future.get()
if (error == null) {
val showNewFeature = client.stringVariation(Constants.FCM_FEATURE_FLAG_ID, "")
Log.d(TAG, "Bucketeer feature flag new value: $showNewFeature")
} else {
// Handle the error
}
}
}
}

remoteMessage.notification?.let {
Log.d(TAG, "Message Notification Body: ${it.body}")
}
}

override fun onNewToken(token: String) {
Log.d(TAG, "Refreshed token: $token")
}

companion object {
private const val TAG = "MyFirebaseMsgService"
}
}
//package io.bucketeer.sample
//
//import android.util.Log
//import com.google.firebase.messaging.FirebaseMessagingService
//import com.google.firebase.messaging.RemoteMessage
//import io.bucketeer.sdk.android.BKTClient
//
//class MyFirebaseMessagingService : FirebaseMessagingService() {
// // [START receive_message]
// override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Log.d(TAG, "From: ${remoteMessage.from}")
//
// remoteMessage.data.also { data ->
// Log.d(TAG, "Message data payload: ${remoteMessage.data}")
// val isFeatureFlagUpdated = data["bucketeer_feature_flag_updated"]
// if (isFeatureFlagUpdated == "true") {
// // Feature flag changed
// Log.d(TAG, "Bucketeer feature flag changed")
//
// runCatching {
// // Make sure BKTClient has been initialize before access it
// val client = BKTClient.getInstance()
// val future = client.fetchEvaluations(3000)
// val error = future.get()
// if (error == null) {
// val showNewFeature = client.stringVariation(Constants.FCM_FEATURE_FLAG_ID, "")
// Log.d(TAG, "Bucketeer feature flag new value: $showNewFeature")
// } else {
// // Handle the error
// }
// }
// }
// }
//
// remoteMessage.notification?.let {
// Log.d(TAG, "Message Notification Body: ${it.body}")
// }
// }
//
// override fun onNewToken(token: String) {
// Log.d(TAG, "Refreshed token: $token")
// }
//
// companion object {
// private const val TAG = "MyFirebaseMsgService"
// }
//}
8 changes: 8 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
android:orientation="vertical"
android:padding="@dimen/padding_16">

<TextView
android:id="@+id/appVersionTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center"
android:textAlignment="center" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/get_variation"
android:layout_width="match_parent"
Expand Down
Loading