-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate with Firebase In-App Messaging & Notifications
- Loading branch information
Showing
8 changed files
with
319 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package io.atha.bababasic | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.provider.Settings | ||
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.google.android.gms.tasks.OnCompleteListener | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import com.google.android.play.core.appupdate.AppUpdateManagerFactory | ||
import com.google.android.play.core.install.model.AppUpdateType | ||
import com.google.android.play.core.install.model.UpdateAvailability | ||
import com.google.firebase.analytics.FirebaseAnalytics | ||
import com.google.firebase.analytics.ktx.analytics | ||
import com.google.firebase.inappmessaging.ktx.inAppMessaging | ||
import com.google.firebase.ktx.Firebase | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import com.google.firebase.remoteconfig.FirebaseRemoteConfig | ||
import com.google.firebase.remoteconfig.ktx.remoteConfig | ||
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
abstract class BabaActivity : AppCompatActivity() { | ||
lateinit var firebaseAnalytics: FirebaseAnalytics | ||
|
||
protected fun initFirebase() { | ||
firebaseAnalytics = Firebase.analytics | ||
CrashHandler.INSTANCE.init(this) | ||
val remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig | ||
val configSettings = remoteConfigSettings { | ||
minimumFetchIntervalInSeconds = 3600 | ||
} | ||
remoteConfig.setConfigSettingsAsync(configSettings) | ||
|
||
Firebase.inAppMessaging.addClickListener { inAppMessage, action -> | ||
Log.i("inapp", "clicked ${inAppMessage} ${action}") | ||
} | ||
|
||
val testLabSetting = Settings.System.getString(contentResolver, "firebase.test.lab") | ||
if ("true" == testLabSetting) { | ||
val bundle = Bundle().apply { | ||
putString("traffic_type", "testlab") | ||
} | ||
Firebase.analytics.setDefaultEventParameters(bundle) | ||
} | ||
|
||
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task -> | ||
if (!task.isSuccessful) { | ||
Log.w("fcm", "Fetching FCM registration token failed", task.exception) | ||
return@OnCompleteListener | ||
} | ||
|
||
// Get new FCM registration token | ||
val token = task.result | ||
|
||
// Log and toast | ||
Log.d("fcm", token) | ||
}) | ||
|
||
if (Build.VERSION.SDK_INT >= 33) { | ||
// notificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS) | ||
} else { | ||
hasNotificationPermissionGranted = true | ||
} | ||
} | ||
|
||
private fun showSettingDialog() { | ||
MaterialAlertDialogBuilder(this, com.google.android.material.R.style.MaterialAlertDialog_Material3) | ||
.setTitle("Notification Permission") | ||
.setMessage("Notification permission is required, Please allow notification permission from setting") | ||
.setPositiveButton("Ok") { _, _ -> | ||
val intent = Intent(ACTION_APPLICATION_DETAILS_SETTINGS) | ||
intent.data = Uri.parse("package:$packageName") | ||
startActivity(intent) | ||
} | ||
.setNegativeButton("Cancel", null) | ||
.show() | ||
} | ||
|
||
private fun showNotificationPermissionRationale() { | ||
MaterialAlertDialogBuilder(this, com.google.android.material.R.style.MaterialAlertDialog_Material3) | ||
.setTitle("Alert") | ||
.setMessage("Notification permission is required, to show notification") | ||
.setPositiveButton("Ok") { _, _ -> | ||
if (Build.VERSION.SDK_INT >= 33) { | ||
notificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS) | ||
} | ||
} | ||
.setNegativeButton("Cancel", null) | ||
.show() | ||
} | ||
|
||
var hasNotificationPermissionGranted = false | ||
|
||
private val notificationPermissionLauncher = | ||
this.registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> | ||
hasNotificationPermissionGranted = isGranted | ||
if (!isGranted) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
if (Build.VERSION.SDK_INT >= 33) { | ||
if (shouldShowRequestPermissionRationale(android.Manifest.permission.POST_NOTIFICATIONS)) { | ||
showNotificationPermissionRationale() | ||
} else { | ||
showSettingDialog() | ||
} | ||
} | ||
} | ||
} else { | ||
Toast.makeText(applicationContext, "notification permission granted", Toast.LENGTH_SHORT) | ||
.show() | ||
} | ||
} | ||
|
||
|
||
private fun checkForUpdates() { | ||
val context = this | ||
CoroutineScope(Dispatchers.Main).launch { | ||
val appUpdateManager = AppUpdateManagerFactory.create(context) | ||
val appUpdateInfoTask = appUpdateManager.appUpdateInfo | ||
|
||
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> | ||
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE | ||
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE) | ||
) { | ||
appUpdateManager.startUpdateFlowForResult( | ||
appUpdateInfo, | ||
AppUpdateType.IMMEDIATE, | ||
context, | ||
1011 | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.