How can I get a custom sound to play with an Apple

2019-03-02 11:47发布

问题:

I am trying to get a custom sound to play when my Apple push notification is delivered. The popup message displays, and the badge updates too, but I always get the default iPhone sound playing instead of my custom sound.

Here's the JSON data I am sending:

{"aps": {"sound": "sound.caf", "badge": 2, "alert": "Successfully registered device"}}

My sound.caf file is at the top level of my Xcode project, and it definitely appears in the resulting .app (as confirmed by 'view package contents' in Finder).

The sound file plays fine on my Mac. Initially I tried .caf, .m4r and .wav files, but none would play. I have also tried using one of the .caf files from an Urban Airship sample project (which I'm pretty confident must work, but admittedly I haven't tried it myself), but that didn't work either.

The Apple docs define the accepted encodings and file formats (.wav, .aiff or .caf).

I've tried very short sound files as well as longer ones (to be sure I am underneath the undocumented 30 second limit).

Has anybody got any other hints/suggestions for me to try?

回答1:

My problem was that I wasn't actually updating the application on my test iPhone. I was updating the version in iTunes, but I hadn't incremented the "Bundle version", so iTunes wasn't pushing the new application to the phone.

To be sure your updated application is being copied to the phone, watch the iTunes status message carefully. It should say:

Updating applications on 'device'
Installing 'application'

If in doubt just delete the existing app from the device.