admin管理员组

文章数量:1394051

I have a .NET MAUI Android Application and I am debugging on a physical device with Android 14.0 - API 34

Now in my AndroidManifest.xml I have the line <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

I am expecting that on application startup the user will be requested to accept or decline push notifications.

But just NOTHING happens...

I need to manually request the permission like described here

So what I am doing currently in my App.xaml.cs is:

protected override async void OnStart()
{
    await CheckAndRequestPushNotificationsPermission();
}

public static async Task<PermissionStatus> CheckAndRequestPushNotificationsPermission()
{
    PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.PostNotifications>();

    if (status == PermissionStatus.Granted)
        return status;

    if (status == PermissionStatus.Denied && DeviceInfo.Platform == DevicePlatform.iOS)
    {
        // Prompt the user to turn on in settings
        // On iOS once a permission has been denied it may not be requested again from the application
        return status;
    }

    if (Permissions.ShouldShowRationale<Permissions.PostNotifications>())
    {
        // Prompt the user with additional information as to why the permission is needed
    }

    status = await Permissions.RequestAsync<Permissions.PostNotifications>();

    return status;
}

This works now but I have questions:

  1. Why do I need the uses-permission inside my AndroidManifest.xml when I need to request it manually anyway?
  2. When I initially start the App the PermissionStatus is Denied and I get asked if I want to enable. When I deny the status does not change... so why am I not getting asked over and over again?
  3. Is that the right way to do it?

I have a .NET MAUI Android Application and I am debugging on a physical device with Android 14.0 - API 34

Now in my AndroidManifest.xml I have the line <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

I am expecting that on application startup the user will be requested to accept or decline push notifications.

But just NOTHING happens...

I need to manually request the permission like described here

So what I am doing currently in my App.xaml.cs is:

protected override async void OnStart()
{
    await CheckAndRequestPushNotificationsPermission();
}

public static async Task<PermissionStatus> CheckAndRequestPushNotificationsPermission()
{
    PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.PostNotifications>();

    if (status == PermissionStatus.Granted)
        return status;

    if (status == PermissionStatus.Denied && DeviceInfo.Platform == DevicePlatform.iOS)
    {
        // Prompt the user to turn on in settings
        // On iOS once a permission has been denied it may not be requested again from the application
        return status;
    }

    if (Permissions.ShouldShowRationale<Permissions.PostNotifications>())
    {
        // Prompt the user with additional information as to why the permission is needed
    }

    status = await Permissions.RequestAsync<Permissions.PostNotifications>();

    return status;
}

This works now but I have questions:

  1. Why do I need the uses-permission inside my AndroidManifest.xml when I need to request it manually anyway?
  2. When I initially start the App the PermissionStatus is Denied and I get asked if I want to enable. When I deny the status does not change... so why am I not getting asked over and over again?
  3. Is that the right way to do it?
Share Improve this question edited Mar 27 at 13:24 CrazyEight asked Mar 27 at 13:04 CrazyEightCrazyEight 4778 silver badges27 bronze badges 2
  • 1 Documentation – tomerpacific Commented Mar 27 at 13:30
  • Yeah ok it is required in the AppManifest. I was already that far – CrazyEight Commented Mar 27 at 13:40
Add a comment  | 

1 Answer 1

Reset to default 1

Why do I need the uses-permission inside my AndroidManifest.xml when I need to request it manually anyway?

The uses-permission in the AndroidManifest.xml is used to declare the permissions your app need. And the official document about Declaring app permissions said:

if your app requests app permissions, you must declare these permissions in your app's manifest file. These declarations help app stores and users understand the set of permissions that your app might request.

And the POST_NOTIFICATIONS is a runtime permission. The official document about Request runtime permissions said:

If you declare any dangerous permissions, and if your app is installed on a device that runs Android 6.0 (API level 23) or higher, you must request the dangerous permissions at runtime

so why am I not getting asked over and over again?

This is because you only called the Permissions.RequestAsync<Permissions.PostNotifications>(); once. And the code executed finished after user operation. For more information, you can check the official document about App capabilities depend on user choice in permissions dialog.

But if user denies the permission more than once, the request dialog will not show any more. The official document about Handle permission denial said:

Starting in Android 11 (API level 30), if the user taps Deny for a specific permission more than once during your app's lifetime of installation on a device, the user doesn't see the system permissions dialog if your app requests that permission again. The user's action implies "don't ask again." On previous versions, users saw the system permissions dialog each time your app requested a permission, unless they had previously selected a "don't ask again" checkbox or option.

本文标签: cMAUIAndroidManifest usespermission no requestStack Overflow