系统会提示用户拒绝/授予通知权限:
该应用程序有一个设置视图,用户可以在其中切换通知。如果未授予权限,我希望将用户指向iOS设置(就像WhatsApp一样)。
如何检查权限是否被授予?特别是在用户授予权限但随后决定从iOS设置中禁用它们的情况下,而不是在应用程序内部。
有一个非常流行的permissions plugin,遗憾的是它不支持这个特殊的权限。
发布于 2019-11-18 22:21:20
您可以使用DependencyService检查是否为应用程序启用了通知。
在iOS中:
[assembly: Dependency(typeof(DeviceService))]
class DeviceService : IDeviceService
{
public bool GetApplicationNotificationSettings()
{
var settings = UIApplication.SharedApplication.CurrentUserNotificationSettings.Types;
return settings != UIUserNotificationType.None;
}
} 在表单中:
public interface IDeviceService
{
bool GetApplicationNotificationSettings();
}然后,您可以从页面或视图模型调用DependencyService,如下所示:
bool isNotificationEnabled = DependencyService.Get<IDeviceService>().GetApplicationNotificationSettings();发布于 2019-11-18 22:02:56
基本上,您需要在每次运行应用程序时请求授权。因此,您将了解授权状态,并在需要时将用户导航到应用程序的ios设置。
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
});如果给定了权限,此方法将在每次运行时返回true。如果它改变了,它将返回false。
发布于 2019-11-18 22:01:16
试试这个:
Device.OpenUri(new Uri("app-settings:"));或者你可以这样做(对于android和iOS):https://dotnetco.de/open-app-settings-in-xamarin-forms/
https://stackoverflow.com/questions/58915284
复制相似问题