开发者

How can I use a sound file present in the documents directory for a local notification?

Within my iOS application, how can I play a sound file stored in the application's documents direct开发者_如何学JAVAory for a local notification, rather than one inside the application bundle?

I've seen a couple of applications on the App Store do this, and I was wondering how this is possible.


It is actually possible to play a custom sound in a local notification that isn't in the app's main bundle (NSBundle).

The sound has to be stored in the Library/Sounds directory, for example: /var/mobile/Containers/Data/Application/DD7E6BA0-2FC0-4DE0-95BF-C2E9C9A94E08/Library/Sounds/mySound.caf

When you reference the sound file in the UILocalNotification soundName property, you simply write the filename, rather than the full directory path:

alert.soundName = @"mySound.caf";

Tested and working on iOS9.3.5. I haven't tried on other iOS versions yet.

Footnote:

I happened to find this information when searching the Apple Documentation regarding Remote Notifications Here - Specifically, the 'Payload Keys' section, where it mentions sound.

Apple's documentation for Local & Remote Notifications (Here) also mentions that the sound files can be utilised if they are in the app's main bundle OR the data container:

To play a sound, assign a sound to the soundName property. You can assign the filename of a nonlocalized custom sound in the app’s main bundle (or data container) or you can assign UILocalNotificationDefaultSoundName to get the default system sound. A sound should always accompany the display of an alert message or the badging of an icon; a sound should not be played in the absence of other notification types.


No you can not play an audio file that is not in the bundle and you can not modify the application bundle at runtime. One reason why Apple requires all sounds for local notifications to be bundled with the application may be for security reasons, that would make it possible for all bundled files to be checked for security risks during the review process.


Exactly .....

    You can not play audio file stored in document directory for alarm as a local push notification.

I also had gone through various apps on app store .those apps basically look like playing customise audio from document directory for local push notification but it is not .insted it using push notification and when notification come at that time when we click on view button of that notification the app which generated the local push notification will get invoked and finally the selected audio file get played .

In this way the app is look like customized push notification using customized audio file from document directory

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜