【发布时间】:2019-07-11 08:48:18
【问题描述】:
我制作了这样的 UNUserNotification:
let content = UNMutableNotificationContent()
content.title = "a title"
content.body = "a body"
content.sound = .default
content.categoryIdentifier = "\(type(of: self))"
let request = UNNotificationRequest(
identifier: UUID().uuidString,
content: content,
trigger: UNTimeIntervalNotificationTrigger(timeInterval: 0.01, repeats: false)
)
let category = UNNotificationCategory(
identifier: content.categoryIdentifier,
actions: [
UNNotificationAction(identifier: Strings.accept.id, title: Strings.accept.title, options: [.authenticationRequired, .foreground]),
UNNotificationAction(identifier: Strings.reject.id, title: Strings.reject.title, options: [.authenticationRequired, .destructive])
],
intentIdentifiers: [],
options: .customDismissAction
)
它将显示一个带有标题和正文的通知,以及两个自定义操作按钮(接受和拒绝,这是一个来电)。它也会播放声音,但由于某种原因,如果应用程序处于前台则不会。
我还尝试用它来伪造一个持久通知,就像 Whatsapp 通过每约 4 秒重复一次本地通知来做到这一点的方式一样,但是第二次显示通知时声音被切断了一点。这不是第一个的声音,第二个显示有自己的声音播放,但这一次并没有完全播放出来。声音只有约 1-2 秒。
我不明白为什么会有这些差异,如何让它始终播放声音(前景、背景、不运行)以及如何确保在第二次显示通知时声音不会被切断?
【问题讨论】: