14
14
15
15
package com.googletest.firebase.appdistribution.testapp
16
16
17
+ import android.Manifest.permission.POST_NOTIFICATIONS
17
18
import android.app.AlertDialog
18
19
import android.content.Intent
20
+ import android.content.pm.PackageManager
19
21
import android.os.Build
20
22
import android.os.Bundle
21
23
import android.util.Log
@@ -27,8 +29,11 @@ import android.widget.ArrayAdapter
27
29
import android.widget.AutoCompleteTextView
28
30
import android.widget.ProgressBar
29
31
import android.widget.TextView
32
+ import androidx.activity.result.ActivityResultLauncher
33
+ import androidx.activity.result.contract.ActivityResultContracts
30
34
import androidx.appcompat.app.AppCompatActivity
31
35
import androidx.appcompat.widget.AppCompatButton
36
+ import androidx.core.content.ContextCompat
32
37
import androidx.core.widget.doOnTextChanged
33
38
import com.google.android.gms.tasks.Task
34
39
import com.google.android.material.textfield.TextInputLayout
@@ -61,6 +66,7 @@ class MainActivity : AppCompatActivity() {
61
66
lateinit var progressPercent: TextView
62
67
lateinit var progressBar: ProgressBar
63
68
lateinit var feedbackTriggerMenu: TextInputLayout
69
+ lateinit var permissionRequestLauncher: ActivityResultLauncher <String >
64
70
65
71
override fun onCreate (savedInstanceState : Bundle ? ) {
66
72
super .onCreate(savedInstanceState)
@@ -115,6 +121,52 @@ class MainActivity : AppCompatActivity() {
115
121
}
116
122
}
117
123
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 () {
118
170
firebaseAppDistribution.showFeedbackNotification(
119
171
R .string.feedbackInfoText,
120
172
InterruptionLevel .HIGH
0 commit comments