From fdeae76ff12f256ca545191a02ea78df571da796 Mon Sep 17 00:00:00 2001 From: Sourav Ganguly Date: Mon, 7 Oct 2024 20:58:22 +0530 Subject: [PATCH] Fixing askNotificationPermission to avoid infinite loop --- .../testdpc/PolicyManagementActivity.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/afwsamples/testdpc/PolicyManagementActivity.java b/src/main/java/com/afwsamples/testdpc/PolicyManagementActivity.java index 942295fa..930f504a 100644 --- a/src/main/java/com/afwsamples/testdpc/PolicyManagementActivity.java +++ b/src/main/java/com/afwsamples/testdpc/PolicyManagementActivity.java @@ -194,10 +194,34 @@ private void askNotificationPermission() { Log.d(TAG, "Notification permission granted"); } else { Log.e(TAG, "Notification permission missing"); - // Directly ask for the permission - ActivityCompat.requestPermissions( - this, new String[] {Manifest.permission.POST_NOTIFICATIONS}, 101); + + // Check if we should show an explanation (rationale) + if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.POST_NOTIFICATIONS)) { + // If the permission was denied, show a Snackbar directing the user to settings + showPermissionSnackbar(); + } else { + // Directly ask for the permission + ActivityCompat.requestPermissions( + this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101); + } } } } + + private void showPermissionSnackbar() { + // Create a Snackbar to inform the user and guide them to app settings + Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), + "Notification permission is necessary please enable it in settings.", + Snackbar.LENGTH_LONG) + .setAction("Settings", new View.OnClickListener() { + @Override + public void onClick(View v) { + // Open the app settings to allow the user to manually enable permissions + Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); + intent.setData(Uri.fromParts("package", getPackageName(), null)); + startActivity(intent); + } + }); + snackbar.show(); + } }