我需要在我的应用程序中知道,该用户在自己的手机>设置>应用程序通知>为此应用程序设置通知。
从android 13开始,我们需要在运行时请求
android.permission.POST_NOTIFICATIONS
权限。所以我们可以使用下面的代码在Activity
的OnCreate
或OnStart
方法中检查并请求通知权限:
const int requestNotification = 0;
string[] notiPermission =
{
Manifest.Permission.PostNotifications
};
if ((int)Build.VERSION.SdkInt < 33) return;
if (this.CheckSelfPermission(Manifest.Permission.PostNotifications) != Permission.Granted)
{
this.RequestPermissions(notiPermission, requestNotification);
}
注:
1.更多信息请查看文档:通知运行时权限、请求运行时权限和Xamarin.Android中的权限。
2.这里有一个官方示例,您可以在这里查看:运行时权限示例。
3.
Manifest.Permission.PostNotifications
只能在.net7.0及以上版本使用。