4

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.

return0
  • 93
  • 11
  • See also [this question](https://stackoverflow.com/questions/69277080/before-ios-15-i-was-able-to-send-sound-only-local-notification-now-i-have-to-s) (also no good answer yet) – Justin Emery Oct 20 '21 at 05:58

1 Answers1

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.

Starsky
  • 1,829
  • 18
  • 25
  • The question clearly asks how to make `[.sound]` work in iOS 15 without using list or banner options. – return0 Dec 21 '21 at 14:47
  • @return0 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. – Starsky Dec 26 '21 at 15:17