iOS 15 - Notification sounds are not playing for foreground notifications
Asked Answered
U

1

5

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.

Univalent answered 2/9, 2021 at 3:39 Comment(1)
See also this question (also no good answer yet)Emia
C
1

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.

Chive answered 21/12, 2021 at 13:48 Comment(2)
The question clearly asks how to make [.sound] work in iOS 15 without using list or banner options.Univalent
@Univalent Well, it seems that it is not working on iOS 15 just with [.sound]. You would need to choose either .banner or .list. The second option won't show any visual representation of an incoming notification while the app is in foreground state. You can show a custom in-app popup if you want.Chive

© 2022 - 2024 — McMap. All rights reserved.