Hello,
You can use ActivityCompat.RequestPermissions(this, reqestPermission, REQUEST_P0STNOTIFICATIONS);
to request permission.
Before you request the permission, you need to check the current running version(android 13 or later) and check the permission if it is grand.
So, I add following code in OnCreate
method to check the verion and check the permission for testing
protected override void OnCreate(Bundle savedInstanceState)
{
SetContentView(R
CreateNotificationChannel();
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu)
{
if (!(CheckPermissionGranted(Manifest.Permission.PostNotifications)))
{
RequestPostNotificationsPermission();
}
}
}
Here is my code about CheckPermissionGranted
method to check the permission.
public bool CheckPermissionGranted(string Permissions)
{
// Check if the permission is already available.
if (ActivityCompat.CheckSelfPermission(this, Permissions) != Permission.Granted)
{
return false;
}
else
{
return true;
}
}
If you do not have this permission, you need to request it.
public static int REQUEST_P0STNOTIFICATIONS = 10023;
string[] reqestPermission={ "android.permission.POST_NOTIFICATIONS" };
private void RequestPostNotificationsPermission()
{
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, "android.permission.POST_NOTIFICATIONS");)
{
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
ActivityCompat.RequestPermissions(this, reqestPermission, REQUEST_P0STNOTIFICATIONS);
}
else
{
//P0STNOTIFICATIONS permission has not been granted yet. Request it directly.
ActivityCompat.RequestPermissions(this, reqestPermission, REQUEST_P0STNOTIFICATIONS);
}
}
If User accept or reject this permission this method will get called:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_P0STNOTIFICATIONS)
{
if (grantResults.Length <= 0)
{
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
Log.Info("error", "User interaction was cancelled.");
}
else if (grantResults[0] == PermissionChecker.PermissionGranted)
{
// Permission was granted.
}
else
{
// Permission denied.
Toast.MakeText(this, " REQUEST_P0STNOTIFICATIONS Permission Denied", ToastLength.Long).Show();
}
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.