J'ai rencontré le même problème, il semble que ce n'était pas supporté auparavant ou qu'il n'était pas possible de jouer un son personnalisé pour une notification locale qui n'est pas incluse dans l'App Bundle. Mais plus tard, cela a été pris en charge et vous pouvez assigner des sons personnalisés à partir du conteneur d'application spécifiquement dans le répertoire Library/Sounds. Vous pouvez soit télécharger des sons du serveur vers ce répertoire, soit le tester en copiant un fichier .caf (Apple a indiqué que d'autres extensions pouvaient fonctionner) de l'offre groupée vers le répertoire Library/sounds, puis en supprimant la référence au fichier de l'offre groupée afin de vous assurer que si des sons sont joués, ils proviennent d'un répertoire hors de l'offre groupée. Tout cela fonctionne très bien, le principal problème pour moi est que si l'utilisateur force la citation de l'application, elle cesse de fonctionner pour les notifications locales alors qu'elle fonctionne toujours avec les notifications du kit push, je planifie cependant une notification locale lorsque le kit push arrivera. Voici le code pour tester la lecture d'un son à partir d'un répertoire :
func copyFileToDirectoryFromBundle()
{
// get reference to library directory
let LibraryDirectoryPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, true)
// see what is in there
for path in LibraryDirectoryPaths
{
print("di=\(path)")
}
// prepare for sound directory
let libraryDirectoryPath: AnyObject = LibraryDirectoryPaths[0]
let soundsDirectoryPath = libraryDirectoryPath.stringByAppendingPathComponent("Sounds")
// create sounds directory under library if not there already
if !NSFileManager.defaultManager().fileExistsAtPath(soundsDirectoryPath)
{
do {
try NSFileManager.defaultManager().createDirectoryAtPath(soundsDirectoryPath, withIntermediateDirectories: false, attributes: nil)
print("sounds library created,,,")
} catch let error as NSError {
print("creating sounds directory error = \(error.localizedDescription)");
}
}
else
{
print("Sounds directory is there already under Library")
}
do {
// sounds directory should have been added under library
let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( NSURL(string: libraryDirectoryPath as! String)!, includingPropertiesForKeys: nil, options: [])
print("Library directs=\(directoryContents)")
// prepare for adding subdirectory with file name from bundle
// here is where you should save files if downloaded from server
// here im just moving one from bundle
let subDirectoryPath = directoryContents.last!
if let pathFromBundle = NSBundle.mainBundle().pathForResource("notification", ofType:"caf") {
let myFilePathUnderSounds = "\(subDirectoryPath.path!)\("/notification.caf")"
if !NSFileManager.defaultManager().fileExistsAtPath(myFilePathUnderSounds)
{
do {
print("bundle path=\(pathFromBundle)and datacontainer path=\(subDirectoryPath.path!)")
try NSFileManager.defaultManager().copyItemAtPath(pathFromBundle, toPath:myFilePathUnderSounds )
print("item copied")
} catch let error as NSError {
print(error.localizedDescription);
}
}
else
{
print("Your file from bundle path = \(pathFromBundle) is already there under Sounds")
}
}
else
{
print("Item not found in bundle")
}
// see item there after you add it
let subDirectories = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( subDirectoryPath, includingPropertiesForKeys: nil, options: [])
print("files under sounds")
for fileDirectory in subDirectories
{
print(fileDirectory)
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
Après avoir vérifié que le fichier .caf se trouve dans le répertoire Library/Sounds . Tout ce que vous avez à faire est d'assigner le nom du fichier avec son extension à la propriété soundName de la notification locale, comme si le fichier se trouvait dans le bundle.