Skip to content

Commit 1275219

Browse files
lfkelloggkaibolay
authored andcommitted
Request notification permissions in test app (#4541)
1 parent 221ac44 commit 1275219

File tree

1 file changed

+52
-0
lines changed
  • firebase-appdistribution/test-app/src/main/kotlin/com/googletest/firebase/appdistribution/testapp

1 file changed

+52
-0
lines changed

firebase-appdistribution/test-app/src/main/kotlin/com/googletest/firebase/appdistribution/testapp/MainActivity.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
package com.googletest.firebase.appdistribution.testapp
1616

17+
import android.Manifest.permission.POST_NOTIFICATIONS
1718
import android.app.AlertDialog
1819
import android.content.Intent
20+
import android.content.pm.PackageManager
1921
import android.os.Build
2022
import android.os.Bundle
2123
import android.util.Log
@@ -27,8 +29,11 @@ import android.widget.ArrayAdapter
2729
import android.widget.AutoCompleteTextView
2830
import android.widget.ProgressBar
2931
import android.widget.TextView
32+
import androidx.activity.result.ActivityResultLauncher
33+
import androidx.activity.result.contract.ActivityResultContracts
3034
import androidx.appcompat.app.AppCompatActivity
3135
import androidx.appcompat.widget.AppCompatButton
36+
import androidx.core.content.ContextCompat
3237
import androidx.core.widget.doOnTextChanged
3338
import com.google.android.gms.tasks.Task
3439
import com.google.android.material.textfield.TextInputLayout
@@ -61,6 +66,7 @@ class MainActivity : AppCompatActivity() {
6166
lateinit var progressPercent: TextView
6267
lateinit var progressBar: ProgressBar
6368
lateinit var feedbackTriggerMenu: TextInputLayout
69+
lateinit var permissionRequestLauncher: ActivityResultLauncher<String>
6470

6571
override fun onCreate(savedInstanceState: Bundle?) {
6672
super.onCreate(savedInstanceState)
@@ -115,6 +121,52 @@ class MainActivity : AppCompatActivity() {
115121
}
116122
}
117123

124+
setupFeedbackNotification()
125+
}
126+
127+
private fun setupFeedbackNotification() {
128+
// Register the permissions callback, which handles the user's response to the
129+
// system permissions dialog
130+
permissionRequestLauncher =
131+
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
132+
if (isGranted) {
133+
showFeedbackNotification()
134+
} else {
135+
// Typically, here we should show the user a message explaining what behavior is
136+
// unavailable because they have not enabled notifications. However in this case we are
137+
// calling this method every time the activity is created, and we don't want to keep
138+
// showing them such a message over and over if they've explicitly disabled notifications
139+
// for the app.
140+
}
141+
}
142+
143+
// Check the permissions and behave accordingly
144+
when {
145+
ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) ==
146+
PackageManager.PERMISSION_GRANTED -> {
147+
showFeedbackNotification()
148+
}
149+
shouldShowRequestPermissionRationale(POST_NOTIFICATIONS) -> {
150+
Log.i(TAG, "Showing customer rationale for requesting permission.")
151+
AlertDialog.Builder(this)
152+
.setMessage(
153+
"Using a notification to initiate feedback to the developer. " +
154+
"To enable this feature, allow the app to post notifications."
155+
)
156+
.setPositiveButton("OK") { _, _ ->
157+
Log.i(TAG, "Launching request for permission.")
158+
permissionRequestLauncher.launch(POST_NOTIFICATIONS)
159+
}
160+
.setNegativeButton("No thanks") { _, _ -> Log.i(TAG, "User denied permission request.") }
161+
.show()
162+
}
163+
else -> {
164+
permissionRequestLauncher.launch(POST_NOTIFICATIONS)
165+
}
166+
}
167+
}
168+
169+
private fun showFeedbackNotification() {
118170
firebaseAppDistribution.showFeedbackNotification(
119171
R.string.feedbackInfoText,
120172
InterruptionLevel.HIGH

0 commit comments

Comments
 (0)