Plugin.LocalNotification icon indicating copy to clipboard operation
Plugin.LocalNotification copied to clipboard

Missing AskPermission & exceptions

Open PonyOny opened this issue 3 years ago • 6 comments

It seems that 'Plugin.LocalNotification.NotificationCenter.AskPermission();' is missing, I would like to ask for permission when user activates a toggle in the App, it actually works as mentioned when I create a Notification, but it would be nice to also be able to ask for the permission as described in documentation, I also can't find how to get the user permission status in order to be able to tell the user to activate the permission if he did decline the approval when he was asked for, creating a new notification when user has not granted access is not returning any status or error, how can I see the response of a new notification, if it was success, if it threw an exception because user does not allow local notifications etc.. it would be nice to have this in order to be able to give some feedback to the user.

PonyOny avatar Feb 17 '22 21:02 PonyOny

@PonyOny can you please attach a sample project?

thudugala avatar Feb 19 '22 09:02 thudugala

It seems that 'Plugin.LocalNotification.NotificationCenter.AskPermission();' is missing, I would like to ask for permission when user activates a toggle in the App, it actually works as mentioned when I create a Notification, but it would be nice to also be able to ask for the permission as described in documentation, I also can't find how to get the user permission status in order to be able to tell the user to activate the permission if he did decline the approval when he was asked for, creating a new notification when user has not granted access is not returning any status or error, how can I see the response of a new notification, if it was success, if it threw an exception because user does not allow local notifications etc.. it would be nice to have this in order to be able to give some feedback to the user.

Hello, my workaround for this is that if the user declines the first notification permission and may want to re-activate, then I will redirect the user to the application's settings using Xamarin Essentials' AppInfo.ShowSettingsUI() method. Hope this helps.

jl-mobitech avatar Feb 22 '22 09:02 jl-mobitech

Hi, thanks, I have already a work around.. but I was expecting that the plugin could manage this scenario, I have created a custom service to manage the permissions and the current status of the Notifications access so i can know if he has granted permission or if he declined to show as you mentioned a custom dialog that will take to the app settings.

public interface IDeviceService
    {
        bool AreNotificationsAllowed();

        Task<bool> RequestNotificationsPermission();

        void OpenSettings();
    }

IOS:

public class DeviceService : IDeviceService
    {
        public bool AreNotificationsAllowed()
        {
            var settings = UIApplication.SharedApplication.CurrentUserNotificationSettings.Types;
            return settings != UIUserNotificationType.None;
        }

        public void OpenSettings()
        {
            var url = new NSUrl($"app-settings:");
            UIApplication.SharedApplication.OpenUrl(url);
        }

        public async Task<bool> RequestNotificationsPermission()
        {
            var status = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound);
            return status.Item1;
        }
    }

PonyOny avatar Feb 22 '22 10:02 PonyOny

@PonyOny can you try 9.2.1-preview01

thudugala avatar Mar 17 '22 05:03 thudugala

@PonyOny, please try 10.0.0-preview02

thudugala avatar Jun 27 '22 12:06 thudugala

I see the issue. AskPermission is only defined in the the project that targets iOS. As a result this can not be called in the Xamarin head project.

I think AskPermission should be moved to INotificationService. This would allow you to setup notification outside of the AppDelegate.

Additionally, Android 13 will be adding this:

https://developer.android.com/about/versions/13/changes/notification-permission#user-choice

mphill avatar Aug 13 '22 10:08 mphill

Try version 10.0.3

thudugala avatar Nov 29 '22 09:11 thudugala