What is the right way to play a tone associated with notification in iOS 15 without displaying banner or list?
When handling notifications in foreground, both local and push, notification sounds are not playing if UNNotificationPresentationOptions
is only sound
. If additional options like banner
or list
is added along with sound
then notification tone plays.
When app is in background, all options of notification presentation are working properly.
I know alert
option is depreciated from iOS 15. Is using sound
as the only presentation option, no longer valid?
Below is the snippet
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification, withCompletionHandler
completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.sound) //not working
//completionHandler([.banner, .sound]) //works
//completionHandler([.list, .sound]) //works
}
Update: Apple confirmed that this was a bug and it is now fixed in iOS 16 beta 2. No solution for iOS 15 though.
I found the solution. You would need to set the completion handler like this:
Now the in-app notifications come with sound and with my custom banner.
.list
will add the notification on the Notification Center as well, which I find very useful, in case the user is using the app and missed some notifications.