0

I am creating a notification service extension and I have a special use case so I have to play sound using AudioToolbox instead of regular notification sound.

It works pretty well, but I don't know how to stop it.

My desired behavior is that when remote push notification is received, it starts playing (that works) and when the app is launched or brought to the front it is stopped (don't know how to do it).

I play the sound like this:

func playSound() {
    guard let soundURL = Bundle.main.url(forResource: "mySound", withExtension: "aiff") else {
        return
    }
    
    var soundID: SystemSoundID = 0
    AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
            
    // Play the sound
    AudioServicesPlaySystemSound(soundID)
}

And I want to stop it in AppDelegate like this:

public func applicationWillEnterForeground(_ application: UIApplication) {
    AudioServicesDisposeSystemSoundID(0)
}

Is it caused because notification service extension is different target than app?

I also tried to call AudioServicesDisposeSystemSoundID(0) in serviceExtensionTimeWillExpire() but this method is not being called.

Ajvar1
  • 107
  • 8
  • I don't think this can be reached. The sound is not in your app's process. It runs in the system notification. It is another process. What you can to is only to make it shorter. – Owen Zhao Mar 31 '23 at 22:01
  • You may try "UNUserNotificationCenter" with removeDeliveredNotifications(withIdentifiers:) to remove the latest notification and see if that can stop the sound playing. – Owen Zhao Mar 31 '23 at 22:04

0 Answers0