I want to notify the to the user by playing a sound only, for example after a given time:
Button(action: {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { granted, error in
Logger().log("Granted: (granted)")
})
}, label: { Text("request") })
Button(action: {
UNUserNotificationCenter.current().delegate = delegate
let content = UNMutableNotificationContent()
// content.title = "title" // All works as expected with a notification title
content.sound = .default
for seconds in 3...5 {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: Double(seconds), repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}, label: { Text("click") })
And:
class UserNotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.sound, .list, .banner])
}
}
This works when the app is in foreground. When in background, the sound is not hearable, unless I add a title (or a subtitle or something) to the notification content. I actually want to play custom sounds, but it doesn’t seem to make any difference to use the default sound. I’m sending quite an amount of notifications and don’t want to flood the notification center with useless messages. I’ve tried to use the same request identifier for all notification requests, but that results in only a single notification. My question is whether this is intended, or if there’s a way to send sound-only notifications of an app in the background.