Ste*_*fan 3 c# permissions runtime ios xamarin
系统会提示用户拒绝/授予通知权限:
该应用程序有一个设置视图,用户可以在其中切换通知。如果未授予权限,我想将用户指向 iOS 设置(就像 WhatsApp 那样)。
如何检查权限是否被授予?特别是在用户授予权限但随后决定从 iOS 设置而不是在应用程序内部禁用它们的情况下。
有一个非常流行的权限插件,遗憾的是它不支持此特定权限。
您可以使用 DependencyService 检查应用程序是否启用了通知。
在iOS中:
[assembly: Dependency(typeof(DeviceService))]
class DeviceService : IDeviceService
{
public bool GetApplicationNotificationSettings()
{
var settings = UIApplication.SharedApplication.CurrentUserNotificationSettings.Types;
return settings != UIUserNotificationType.None;
}
}
Run Code Online (Sandbox Code Playgroud)
在表格中:
public interface IDeviceService
{
bool GetApplicationNotificationSettings();
}
Run Code Online (Sandbox Code Playgroud)
在这些之后,您可以从您的页面或视图模型中调用您的 DependencyService,如下所示:
bool isNotificationEnabled = DependencyService.Get<IDeviceService>().GetApplicationNotificationSettings();
Run Code Online (Sandbox Code Playgroud)