【发布时间】:2020-01-23 13:02:02
【问题描述】:
在我的应用中,我使用以下方法安排本地通知:
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 版本上对其进行了测试,并且通知显示为假定的。
也许有些人也发现了这个错误,并有任何解决方法的建议。任何帮助表示赞赏。
【问题讨论】:
-
很可能这就是它的设计方式。如果用户禁用了通知,为什么还要打扰系统安排通知呢?您可能想检查应用程序是否禁用了通知,并推迟通知计划?也许有一个变量来检查每次应用程序进入前台时是否启用通知,如果
true,则安排通知。 -
当推送在设备上处于终止状态时,我们如何触发本地通知。我需要添加这个功能来制作像whatsapp这样的视频通话提醒。
标签: ios swift localnotification unusernotificationcenter