Skip to content

Commit fdeae76

Browse files
Fixing askNotificationPermission to avoid infinite loop
1 parent 7a8957b commit fdeae76

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/main/java/com/afwsamples/testdpc/PolicyManagementActivity.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,34 @@ private void askNotificationPermission() {
194194
Log.d(TAG, "Notification permission granted");
195195
} else {
196196
Log.e(TAG, "Notification permission missing");
197-
// Directly ask for the permission
198-
ActivityCompat.requestPermissions(
199-
this, new String[] {Manifest.permission.POST_NOTIFICATIONS}, 101);
197+
198+
// Check if we should show an explanation (rationale)
199+
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.POST_NOTIFICATIONS)) {
200+
// If the permission was denied, show a Snackbar directing the user to settings
201+
showPermissionSnackbar();
202+
} else {
203+
// Directly ask for the permission
204+
ActivityCompat.requestPermissions(
205+
this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101);
206+
}
200207
}
201208
}
202209
}
210+
211+
private void showPermissionSnackbar() {
212+
// Create a Snackbar to inform the user and guide them to app settings
213+
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
214+
"Notification permission is necessary please enable it in settings.",
215+
Snackbar.LENGTH_LONG)
216+
.setAction("Settings", new View.OnClickListener() {
217+
@Override
218+
public void onClick(View v) {
219+
// Open the app settings to allow the user to manually enable permissions
220+
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
221+
intent.setData(Uri.fromParts("package", getPackageName(), null));
222+
startActivity(intent);
223+
}
224+
});
225+
snackbar.show();
226+
}
203227
}

0 commit comments

Comments
 (0)