Skip to content
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

Fixing askNotificationPermission to avoid infinite loop between PolicyManagementActivity and GrantPermissionsActivity #228

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/main/java/com/afwsamples/testdpc/PolicyManagementActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}