Search code examples
c#iosxamarinpermissionsruntime

Check if notification permission got granted (Xamarin, iOS)


The user is prompted to deny/grant permission for notifications:

screenshot

The app has a settings view where the user can toggle notifications. If the permission is not granted I want to point the user to the iOS settings (like WhatsApp does).

How do I check if the permissions got granted? Particularly in the case when a user grants permission but then decides to disable them from the iOS settings, not inside the app.

There is a wildly popular permissions plugin which sadly does not support this particular permission.


Solution

  • You can use DependencyService to check if notifications is enabled for the app.

    In iOS:

    [assembly: Dependency(typeof(DeviceService))]
      class DeviceService : IDeviceService
      {
        public bool GetApplicationNotificationSettings()
         {
         var settings = UIApplication.SharedApplication.CurrentUserNotificationSettings.Types;
                return settings != UIUserNotificationType.None;
         }
       } 
    

    In Forms:

    public interface IDeviceService
    {
       bool GetApplicationNotificationSettings();
    }
    

    And after these, you can call your DependencyService from your page or view model like this:

    bool isNotificationEnabled = DependencyService.Get<IDeviceService>().GetApplicationNotificationSettings();