iOS 15 - Notification sounds are not playing for foreground notifications

1.8k Views Asked by At

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.

1

There are 1 best solutions below

2
On

I found the solution. You would need to set the completion handler like this:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    switch UIApplication.shared.applicationState {

    case .active:
        if #available(iOS 14.0, *) {
            completionHandler([.sound, .list])
        } else {
            // Fallback on earlier versions
            completionHandler([.sound])
        }
    default:
        if #available(iOS 14.0, *) {
            completionHandler([.banner, .sound])
        } else {
            // Fallback on earlier versions
            completionHandler([.alert, .sound])
        }
    }
}

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.