简体   繁体   中英

iOS Push notification Custom sound

I need to customize push notification sound. I don't want to create and include to Bundle. I need to implement like whatsapp notification sounds or please give a list of Apple provided sounds(like default sound). Any help would be highly appreciated.

Thanks

After digging this issue for more than 3 weeks, and writing back and forth with apple this is the only solution change payload to something like

 sound = "custom"

now you need to show all your system sounds in a table view (if you don't know how to do it, look on github). once the user mark his sound notification you need to copy the file to library/sound and give it the name of custom (or whatever the exact name in the payload). that way you never change the server side code and you keep it on custom, and on the other hand you just overwrite the custom file with a new sound the user has been chosen.

*SIDE NOTE: on version 9.2.1 there's a bug which cause the notification not to work on the second time, or at all, it's should be fixed according to Apple in the next version 9.3 !

in the breath I wish the solution I could override the payload like we can do in Android, Apple makes push notification a lot harder on the developers.

in you bundle add a sound file named "pushSound.caf".

//write your payload this way

{
 aps =
 {
    alert = "message";
    sound = "pushSound.caf";//this file will have to your bundle
   };
}

Preparing Custom Alert Sounds

For remote notifications in iOS, you can specify a custom sound that iOS plays when it presents a local or remote notification for an app. The sound files can be in the main bundle of the client app or in the Library/Sounds folder of the app's data container.

Custom alert sounds are played by the iOS system-sound facility, so they must be in one of the following audio data formats:

  • Linear PCM
  • MA4 (IMA/ADPCM)
  • µLaw
  • aLaw

You can package the audio data in an aiff, wav, or caf file. Then, in Xcode, add the sound file to your project as a nonlocalized resource of the app bundle or to the Library/Sounds folder of your data container.

You can use the afconvert tool to convert sounds. For example, to convert the 16-bit linear PCM system sound Submarine.aiff to IMA4 audio in a CAF file, use the following command in the Terminal app:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v

You can inspect a sound to determine its data format by opening it in QuickTime Player and choosing Show Movie Inspector from the Movie menu.

Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.

To use the default sound for a notification

let content = UNMutableNotificationContent()

/// Set up content ...

content.sound = UNNotificationSound.default()

To use a custom sound, the sound file has to be stored in the app's main bundle OR download it and store it in the Library/Sounds subdirectory of the app's container directory.

The "main bundle" approach can only be used with a new application release, the "downloading the sound file" approach is more flexible and makes shipping new sounds without a new version release.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM