Use custom sounds as notification sounds
In iOS 10 after,iOS uses a new
UserNotifications As the notification API,This allows developers to use images、video、Sound and other rich media content as the notification,While further,You can also use the Notification Content Extension Size customize the content and layout of notice。
If you are self-defined tone notification,Then you can give
UNNotificationContent of
sound Add property
UNNotificationSound Example:
1 2 |
let content = UNMutableNotificationContent() content.sound = UNNotificationSound(named: sound+".m4a") |
As can be seen from the highlighted lines,Since the specific location defined ring tone is assigned automatically by the system iOS。All to known,These custom tone must be pre-positioned in the app main bundle in,Length should not exceed 30s,Otherwise it will be replaced by a system beep。
Then ,Is there any way,App to makeUsersIt sounds as a notification tone it?
Use user-defined sound as a notification sounds
User-customizable sound,Here we refer users from other app Importingthe sound of、From the siteDownloadthe sound of,Or is our app downloaded from the server's own post-beep。Then,There is no way,Do not put in the app in the preset tone,ButLate loadedIt?
This is rarely mentioned on the web,Actually it is a。If you go read the official document,Then you willThis pageI found this sentence:
Files for custom sounds must be located in your executable’s main bundle or in the Library/Sounds directory of your app’s container directory.
That is,In addition to the preset in the app main bundle In addition to,We can also download voice placed Library/Sounds This directory!
1 2 3 4 |
let path = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask) let url = path[0].appendingPathComponent("Sounds", isDirectory: true) try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil) try? FileManager.default.copyItem(atPath: --sound-path--, toPath: url.path+ --sound-name--) |
Thus,Download the user to import or sound can be used as a tone! --of course,Do not forget to also follow the provisions of the 30s。
The same time,Due to restrictions api,Preferably, do not repeat,Otherwise, say bad system with which sound as the tone。
References
UILocalNotification: Playing a Custom Audio File saved in Documents Directory
Original article written by LogStudio:R0uter's Blog » UserNotifications user-created using sound as a tone
Reproduced Please keep the source and description link:https://www.logcg.com/archives/2984.html
Have you tried this? I can’t use this custom voice,Still the default sound。Can I talk to you?