Reputation: 1636
I have previously had local notifications working in my app that show just a title. Now I would like to have a subtitle and body for each notification. I added a subtitle
and body
for my UNMutableNotificationContent
. But when the notification presents itself, the subtitle or body is not there. How can I fix this?
Here's how I add a local notification:
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")
}
}
Here is how I ask for notification permissions:
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")
}
}
}
}
Here is my willPresent
code:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
Upvotes: 2
Views: 1579
Reputation: 4311
Are you referring to the case of receiving the local notification in the foreground or in the background?
If it is in the foreground, you would be using userNotificationCenter(_:willPresent:withCompletionHandler:)
and in that case if you want an alert, you need to handle the alert yourself (in your code). So you can check your code starting from userNotificationCenter(_:willPresent:withCompletionHandler:)
to see if you have a custom alert that is only presenting the title. (you can also edit your post to show us what you have in userNotificationCenter(_:willPresent:withCompletionHandler:)
).
Upvotes: 1