Stefan
Stefan

Reputation: 153

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.

Upvotes: 4

Views: 5575

Answers (4)

Mahab
Mahab

Reputation: 188

A more thorough notification settings check:

var Notificationssettings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync();
switch (Notificationssettings.AuthorizationStatus)
{
    case UNAuthorizationStatus.Authorized:
        return true;
    case UNAuthorizationStatus.Denied:
        return false;
    case UNAuthorizationStatus.Ephemeral:
        return true;
    case UNAuthorizationStatus.NotDetermined:
        return false;
    case UNAuthorizationStatus.Provisional:
        return true;
    default:
        return false;
}

Upvotes: 1

Tuğçe Arar
Tuğçe Arar

Reputation: 806

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();

Upvotes: 8

Darko
Darko

Reputation: 119

Basically you need to request authorization on each app run. Therefore you'll know the authorization status and navigate users to ios settings of your app if you need to do so.

UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (approved, error) =>
            {
                // do something with approved
                // approved will be true if user given permission or false if not
            });

If the permission is given this method will return true on each run.. If it changes it will return false.

Upvotes: 2

Dom
Dom

Reputation: 569

try this:

Device.OpenUri(new Uri("app-settings:"));

or you can do it like this (for android and iOS): https://dotnetco.de/open-app-settings-in-xamarin-forms/

Upvotes: -1

Related Questions