ios - 如果在 iOS 设置中禁用通知访问时安排本地通知,则不会在 iOS 13 上触发本地通知
问题描述
在我的应用程序中,我使用以下方法安排本地通知:
func addNotificationRequest(fireDate: Date, identifier: String, sound: UNNotificationSound)
{
let notificationCenter = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Important"
content.body = notificationMessage
content.sound = sound
content.categoryIdentifier = "UserActions"
let calendar = Calendar(identifier: .gregorian)
let triggerDate = calendar.dateComponents([.hour, .minute, .second], from: fireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
let notificationRequest = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
notificationCenter.add(notificationRequest) { error in
if let error = error
{
print(error.localizedDescription)
}
}
let myAction = UNNotificationAction(identifier: "MyActionID", title: "Open", options: [.foreground])
let category = UNNotificationCategory(identifier: "UserActions", actions: [myAction], intentIdentifiers: [], options: [])
notificationCenter.setNotificationCategories([category])
}
通知应该在给定时间触发,并且应该每天在同一时间重复。
在 iOS 13 上,我发现了一个可以通过以下步骤重现的错误:
- 我去iOS设置>通知>应用程序名称>禁用“允许通知”
- 然后我打开应用程序并安排本地通知,例如 2 分钟后
- 之后,我返回设置并启用“允许通知”开关。
- 2 分钟后没有显示本地通知。在较旧的 iOS 版本上对其进行了测试,并且通知显示为假定的。
也许有些人也发现了这个错误,并有任何解决方法的建议。任何帮助表示赞赏。
解决方案
星空是对的!
根据Apple 的文档:
在尝试从您的应用程序安排本地通知之前,请确保您的应用程序有权这样做,因为用户可以随时更改您的应用程序的授权设置。用户还可以更改您的应用程序允许的交互类型,这可能会导致您更改配置通知的方式。
如果权限被禁用,您要么必须在安排通知时“静默失败”,要么通知用户他们需要进入“设置”应用程序以重新启用它们:
就我而言,我使用SwiftMessages做了这样的事情:
static func showNotificationDisabledInfo() {
print("INFO: Notification permissions denied, need to reset in Settings!")
showAlertMessage(withTitle: "Notifications are disabled!",
body: "Go to the Settings app to re-enable notifications.",
type: .info)
}
推荐阅读
- swift - 如何在 tableviewer 中显示下一个索引路径行和节项
- identityserver4 - #identityserver 500 错误使用授权属性时
- cobol - 如何根据我设置的图片子句从 COBOL 剪切计算结果
- c# - 我对 C# 非常陌生,并且刚刚尝试了基础知识,其中一个是更温暖更冷的游戏
- reactjs - 使用 TypeScript 的样式化组件
- wpf - 复选框勾选方向
- r - 将某一行的值与 data.table 中一定数量的先前行进行比较
- r - 格式化日期的缩写月份问题
- python - 在python中打印函数的返回值?
- python - matplotlib 绘图问题