-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
feat(appdistribution): add in-app-feedback support #1463
base: master
Are you sure you want to change the base?
Changes from all commits
197a062
ca654a8
e9cc93b
c142c9f
73141ba
2940487
9ea2ed6
8ba8dab
b22860c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
package com.google.firebase.appdistributionquickstart.java; | ||
|
||
import android.Manifest; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.widget.Toast; | ||
|
||
import androidx.activity.result.ActivityResultLauncher; | ||
import androidx.activity.result.contract.ActivityResultContracts; | ||
import androidx.appcompat.app.AlertDialog; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.core.content.ContextCompat; | ||
|
||
import com.google.firebase.appdistribution.FirebaseAppDistribution; | ||
import com.google.firebase.appdistribution.FirebaseAppDistributionException; | ||
import com.google.firebase.appdistribution.InterruptionLevel; | ||
import com.google.firebase.appdistributionquickstart.R; | ||
import com.google.firebase.appdistributionquickstart.databinding.ActivityMainBinding; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
private static final String TAG = "AppDistribution-Quickstart"; | ||
private static final String TAG = "MainActivity.java"; | ||
|
||
// Declare the launcher at the top of your Activity/Fragment: | ||
private final ActivityResultLauncher<String> requestPermissionLauncher = | ||
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { | ||
if (isGranted) { | ||
showFeedbackNotification(); | ||
} else { | ||
Toast.makeText( | ||
MainActivity.this, | ||
"You won't be able to tap a notification to send feedback because" + | ||
" the notification permission was denied", | ||
Toast.LENGTH_LONG | ||
).show(); | ||
} | ||
}); | ||
|
||
private FirebaseAppDistribution mFirebaseAppDistribution; | ||
|
||
|
@@ -21,6 +47,14 @@ protected void onCreate(Bundle savedInstanceState) { | |
setContentView(binding.getRoot()); | ||
|
||
mFirebaseAppDistribution = FirebaseAppDistribution.getInstance(); | ||
|
||
binding.btShowNotification.setOnClickListener(view -> { | ||
askNotificationPermission(); | ||
}); | ||
|
||
binding.btSendFeedback.setOnClickListener(view -> { | ||
mFirebaseAppDistribution.startFeedback(R.string.feedback_additional_form_text); | ||
}); | ||
} | ||
|
||
@Override | ||
|
@@ -37,4 +71,48 @@ public void onResume() { | |
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
// Hide the notification once this activity is destroyed | ||
mFirebaseAppDistribution.cancelFeedbackNotification(); | ||
} | ||
|
||
private void showFeedbackNotification() { | ||
mFirebaseAppDistribution.showFeedbackNotification( | ||
R.string.feedback_additional_form_text, | ||
InterruptionLevel.HIGH | ||
); | ||
} | ||
|
||
private void askNotificationPermission() { | ||
// This is only necessary for API level >= 33 (TIRAMISU) | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you want this check here anymore. Even if they are on an older device we still want to show the notification. On older devices the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc: @marinacoelho |
||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == | ||
PackageManager.PERMISSION_GRANTED) { | ||
// All set. We can post notifications | ||
showFeedbackNotification(); | ||
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { | ||
Log.i(TAG, "Showing customer rationale for requesting permission."); | ||
new AlertDialog.Builder(this) | ||
.setMessage("Using a notification to initiate feedback to the developer. " + | ||
"To enable this feature, allow the app to post notifications." | ||
) | ||
.setPositiveButton("OK", (dialogInterface, i) -> { | ||
Log.i(TAG, "Launching request for permission."); | ||
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS); | ||
}) | ||
.setNegativeButton("No thanks", (dialogInterface, i) -> { | ||
Log.i(TAG, "User denied permission request."); | ||
}) | ||
.show(); | ||
} else { | ||
// Directly ask for the permission | ||
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS); | ||
} | ||
} else { | ||
showFeedbackNotification(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,57 @@ | ||
package com.google.firebase.appdistributionquickstart.kotlin | ||
|
||
import android.Manifest | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.content.ContextCompat | ||
import com.google.firebase.appdistribution.FirebaseAppDistribution | ||
import com.google.firebase.appdistribution.FirebaseAppDistributionException | ||
import com.google.firebase.appdistribution.InterruptionLevel | ||
import com.google.firebase.appdistribution.ktx.appDistribution | ||
import com.google.firebase.appdistributionquickstart.R | ||
import com.google.firebase.appdistributionquickstart.databinding.ActivityMainBinding | ||
import com.google.firebase.ktx.Firebase | ||
|
||
class KotlinMainActivity : AppCompatActivity() { | ||
|
||
private lateinit var firebaseAppDistribution: FirebaseAppDistribution | ||
|
||
// Declare the launcher at the top of your Activity/Fragment: | ||
private val requestPermissionLauncher = registerForActivityResult( | ||
ActivityResultContracts.RequestPermission() | ||
) { isGranted: Boolean -> | ||
if (isGranted) { | ||
showFeedbackNotification() | ||
} else { | ||
Toast.makeText( | ||
this@KotlinMainActivity, | ||
"You won't be able to tap a notification to send feedback because" + | ||
" the notification permission was denied", | ||
Toast.LENGTH_LONG | ||
).show() | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val binding = ActivityMainBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
firebaseAppDistribution = Firebase.appDistribution | ||
|
||
binding.btShowNotification.setOnClickListener { | ||
askNotificationPermission() | ||
} | ||
|
||
binding.btSendFeedback.setOnClickListener { | ||
firebaseAppDistribution.startFeedback(R.string.feedback_additional_form_text) | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
|
@@ -34,10 +68,51 @@ class KotlinMainActivity : AppCompatActivity() { | |
} | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
// Hide the notification once this activity is destroyed | ||
firebaseAppDistribution.cancelFeedbackNotification() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to Lee's comment above, I think we don't need this line here as well. |
||
} | ||
|
||
private fun showFeedbackNotification() { | ||
firebaseAppDistribution.showFeedbackNotification( | ||
R.string.feedback_additional_form_text, | ||
InterruptionLevel.HIGH | ||
) | ||
} | ||
|
||
private fun askNotificationPermission() { | ||
// This is only necessary for API level >= 33 (TIRAMISU) | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as Lee's comment above. And if we absolutely need to keep this check here, what do you think of changing this function a bit to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a |
||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == | ||
PackageManager.PERMISSION_GRANTED | ||
) { | ||
// All set. We can post notifications | ||
showFeedbackNotification() | ||
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { | ||
Log.i(TAG, "Showing customer rationale for requesting permission.") | ||
AlertDialog.Builder(this) | ||
.setMessage( | ||
"Using a notification to initiate feedback to the developer. " + | ||
"To enable this feature, allow the app to post notifications." | ||
) | ||
.setPositiveButton("OK") { _, _ -> | ||
Log.i(TAG, "Launching request for permission.") | ||
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) | ||
} | ||
.setNegativeButton("No thanks") { _, _ -> Log.i(TAG, "User denied permission request.") } | ||
.show() | ||
} else { | ||
// Directly ask for the permission | ||
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) | ||
} | ||
} else { | ||
showFeedbackNotification() | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
private const val TAG = "AppDistribution-Quickstart" | ||
private const val TAG = "KotlinMainActivity.kt" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
<resources> | ||
<string name="app_name">App Distribution Quickstart</string> | ||
<string name="textview_text">Welcome to the Firebase App Distribution Quickstart app. Press the button to trigger an analytics event!</string> | ||
<string name="textview_text">Welcome to the Firebase App Distribution Quickstart app</string> | ||
|
||
<string name="installation_id_fmt">Firebase Installation ID: %s</string> | ||
<string name="show_feedback_notification">Show Feedback Notification</string> | ||
<string name="send_feedback">Send Feedback</string> | ||
<string name="feedback_additional_form_text">This is a placeholder for text from the developer that is shown to the tester before they provide feedback. It can contain <a href="https://firebase.google.com/docs/app-distribution">links</a> to more information.</string> | ||
</resources> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't need this anymore now that we made the change to automatically hide the notification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When does the automatic hide take place? I tried closing the Activity, but it didn't dismiss the notification. Is it when the app is closed?