swift - 在第一次启动之后接受通知
问题描述
在我的应用程序中,用户可以使用从应用程序发送的通知。首次启动时,appdelegate 中会显示拒绝或允许通知的警报。如果我接受通知,一切正常。我制作了一个屏幕/视图控制器,用户可以在其中接受或不接受开关通知。如果用户在首次启动时拒绝通知,我如何访问 appdelegate 中的控件,就像我在首次启动时一样?
谢谢
解决方案
根据Apple 的开发者文档和这个答案(为了澄清),推送通知权限只能请求一次。iOS 会在用户做出决定后存储用户的决定,并且无法再次请求。请为某些用户可能不想要通知的事实做好准备,您可以随时检查您的应用收到的权限状态:
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { settings in
guard settings.authorizationStatus == .authorized else { return }
if settings.alertSetting == .enabled {
// Schedule an alert-only notification.
} else {
// Schedule a notification with a badge and sound.
}
}
推荐阅读
- android - 为什么 MediatorLiveData 没有观察到两个不同的来源?
- javascript - 将脚本加载到 javascript
- excel - 将多个工作簿中的数据收集到主工作簿中 - 来自单元格的路径
- angularjs - AngularJS:仅在某些数据存在时添加 ng-show 属性
- spring - Spring Feign Client JSON解析错误:非法字符
- git - Git引发简单合并的冲突
- java - 发送没有关键值的改造获取请求
- javascript - 为动态创建的输入标签分配和更新值 - jquery
- .net-core - 使用 Magick.Net 生成的 Pdf 文件的 png 是黑色的 #695
- rust - 如果我在代码的不同位置使用非可变变量,为什么会出现借用错误?