Blazor.Notifications icon indicating copy to clipboard operation
Blazor.Notifications copied to clipboard

Add check permission without user request

Open nikminer opened this issue 9 months ago • 0 comments

Add a check for permission of browser notifications without asking the user for permission, since the NotificationService.PermissionStatus parameter is always default; it is impossible to get the current status without asking the user for permission, although in the js Notification object itself the status is available without asking for verification.

This is necessary so that the site can operate within the current user permissions and not request any additional actions.

To implement this, you can add to the script: script.js

export function getPermission() {
    return new Promise((resolve, reject) => {
        resolve(Notification.permission);
    });
}

and this method to NotificationService

public async ValueTask<PermissionType> GetPermissionAsync()
{
    var module = await _moduleTask.Value;
    string permission = await module.InvokeAsync<string>("getPermission");

    if (permission.Equals("granted", StringComparison.InvariantCultureIgnoreCase))
        PermissionStatus = PermissionType.Granted;

    if (permission.Equals("denied", StringComparison.InvariantCultureIgnoreCase))
        PermissionStatus = PermissionType.Denied;

    if (permission.Equals("default", StringComparison.InvariantCultureIgnoreCase))
        PermissionStatus = PermissionType.Default;

    return PermissionStatus;
}

nikminer avatar Apr 22 '25 10:04 nikminer