Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Audio Session in Notification Service Extension

Is there anyway that I could use AVAudioSession, AVAudioPlayer or anything similar in Notification Service Extension?

I am trying to implement Audio Playback in the Notification Service Extension to play specific audio file when receiving Notification regardless the app state(foreground, background or killed), but I am not able to activate audio session in Notification Service Extension.

NSError *sessionError = nil;

BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];

success = [[AVAudioSession sharedInstance] setActive:YES error:&sessionError];

if (!success) {
  NSLog(@"Error activating audio session: %@", sessionError);
}

Below is the error that I got when I am trying to run the code above in Notification Service Extension.

Error Domain=NSOSStatusErrorDomain Code=561015905 "Session activation failed" UserInfo={NSLocalizedDescription=Session activation failed}

You cannot play audio in a notification service extension. If you have audio files to play, you can always specify the audio file in the notification payload. Alternatively you can also download the file in the NSE if the file size and network conditions would allow it to be downloaded within the time limit (30 seconds).

You would use the "sound" key in the push payload as described in Generating a remote notification to have a specific sound file played

You can specify a file in your app’s main bundle or in the Library/Sounds folder of your app’s container directory.

For information about how to prepare sounds, see UNNotificationSound


Argun Tekant /  DTS Engineer / Core Technologies

Audio Session in Notification Service Extension
 
 
Q