Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 633
Stefan Avatar asked Mar 03 '23 04:03

Stefan


1 Answers

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();
like image 63
Tuğçe Arar Avatar answered Mar 10 '23 19:03

Tuğçe Arar