我以前在我的应用中使用了本地通知,但只显示标题。现在,我想为每个通知添加一个字幕和正文。我为subtitle
添加了body
和UNMutableNotificationContent
。但是当通知出现时,字幕或正文不存在。 我该如何解决?
这是我添加本地通知的方式:
let content = UNMutableNotificationContent()
content.title = "title works!"
content.subtitle = "testing subTitle"
content.body = "testing body"
content.sound = .default
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let identifier = "notificationId"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
print("Hey listen! Error adding notification: \(error.localizedDescription)")
} else {
print("saved notification")
}
}
这是我要求通知权限的方式:
private func requestNotificationAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if let error = error {
print("Hey listen! Got an error requesting notification authorization: \(error.localizedDescription)")
} else {
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
print("not granted")
}
}
}
}
这是我的willPresent
代码:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
您是指在前台还是在后台接收本地通知的情况?
如果位于前台,则将使用userNotificationCenter(_:willPresent:withCompletionHandler:)
,在这种情况下,如果您想要警报,则需要自己处理警报(在代码中)。因此,您可以从userNotificationCenter(_:willPresent:withCompletionHandler:)
开始检查代码,以查看是否有仅显示标题的自定义警报。 (您也可以编辑您的信息以向我们显示userNotificationCenter(_:willPresent:withCompletionHandler:)
中的内容)。