I want to change the UNMutableNotificationContent.sound to play dynamic sound for user. I found comment below:
// The name of a sound file to be played for the notification.
// The sound file must be contained in the app’s bundle or in the Library/Sounds folder of the app's data container.
// If files exist in both locations then the file in ~/Library/Sounds will be preferred.
+ (instancetype)soundNamed:(NSString *)name __WATCHOS_PROHIBITED;
For example, push playload will bring a number to client, then I do text to speech work using the extension code.
But I have no permission to move the tts audio file to the ~/Library/Sounds directory of the container app.
If there are any suggestion than can help to solve this problem, or any other method to play the dynamic number sound when the container app is inactive.
Related
How can I repeat sound file when PN(push-notification) arrive in my device using Objective- c.
You can find how to Custom Alert Sounds in this website: Managing Your App’s Notification Support and the ringtone can not be Over 30s
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 must be in the main bundle of the client app.
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.
You may 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.
You can make this example dictionary send to APNs server with your custom alert sound name for the value of 'sound':
// Create the payload body
$body['aps'] = array(
'alert' => 'TestNotification',
'sound' => 'YourCustomAlertSoundName.caf',
'badge' => 8
After making a good ringtone to your project's main bundle, the important step is to add the sound file you added (YourCustomAlertSoundName.caf) to Build Phases-> Copy Bundle Resources in the Project Settings, and the ringtone can not be Over 30s, otherwise the system will enable the default ringtone (default).
You just have to set custom file into sound on push payload. Put the same audio file into your build resource and just fire notification from server, it will play audio.
{
"aps" :
{
"alert" : "This is your alert text"
"sound" : "YOUR_COSTOM_FILE_NAME.mp4"
},
}
When the application is in foreground mode, then you need to play same audio file when you get receive notification.
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.
It needs to be in the correct format as well, see https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW6
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.
I send a parse push notification which the app receives, but the sound requested does not play, instead the default sound plays. The sounds have been saved by the app in the documents folder. I can check in iTunes that they are there. At some point this was working, but for some reason it has stopped playing the requested sounds. If I log the push notification user info dictionary the sound name is correctly there:
2015-01-11 12:17:41.356 My App[239:60b] didReceiveRemoteNotification, userInfo: {
aps = {
alert = "XXX says FOO from the console!";
sound = "w8nmridutW_v1.mp3";
};
}
Any ideas?
It is possible that the audio format that you use is not supported. Was it a different file when it was working ?
Check with the apple documentation.
According to the documentation here, mp3 isn't a supported file type. Are you sure you were getting this mp3 file to play before? If so, you were experiencing undocumented/unsupported behavior.
Here's the pertinent bit:
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 must be in the main bundle of the client app.
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.
You may 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.
I'm currently writing an iOS app that allows users to stream music from soundcloud. Currently I am able to stream just fine from Soundcloud, in booth forefront and background app states. I have an issue when I download an MP3 files and attempt to play it and have continuously playback when the app in in the background, more specifically in the locked screen or the screen is off.
When I play a downloaded file and lock the screen, the audio continues to play for a while. Usually it plays for 2 -3 mins. After that playback will stop and any other downloaded mp3 files in the playlist will not playback until the user returns to the app. Items in the playlist that are not downloaded will playback perfectly if the user has an internet connection, regardless if a downloaded item failed to play previously.
There are times when I receive the following error:
AVPlayerItemStatusFailed: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not becompleted" UserInfo=0x170270500 {NSUnderlyingError=0x170059380 "The operation couldn’t be completed. Operation not permitted", NSLocalizedFailureReason=An unknown error occurred (1), NSLocalizedDescription=The operation could not be completed}
I will additionally get the following notification from the AVPlayerItem
Notification: NSConcreteNotification 0x1700538c0 {name = AVPlayerItemDidPlayToEndTimeNotification; object = AVPlayerItem: 0x178009300, asset = AVURLAsset: 0x17822aaa0, URL = file:///var/mobile/Applications/51118E74-3334-4EFC-B148-B485DE675F9E/Documents/Downloads/SC_165903784.mp3}
This notification is received when the first item that is playing stop playing. It doesn't make sense that I get this notification when it doesn't finish playing to end of its duration.
My guess is that because the app is in background mode there is a limited time set for reading files, but I somehow doubt that.
I'm using the following to create a AVPlayerItem from a Local file.
NSURL* url = [NSURL fileURLWithPath:song.downloadFilePath];
[AVPlayerItem playerItemWithURL:url];
I have tried building my own AVQueuePlayer and also using iOS Hysteria Player, but both instances have given me the same bug for offline download playback.
Any insights or solutions are greatly appreciated.
Thanks!
Are you playing your files from the Documents directory? (Or anywhere besides the app bundle?)
If so, be sure to save the files to the directory using
[fileToSave writeToFile:filePath options:NSDataWritingFileProtectionNone error:nil];.
The NSDataWritingFileProtectionNone is the critical bit! What's going on, at least in my case, is this: iOS is trying to be secure, and so is by default enabling file protection on the files you add to the Documents directory (and also the other non-app-bundle directories as well, I believe). By adding NSDataWritingFileProtectionNone, you're requesting the system to no longer enable this protection on your files.
From Apple's iOS Documentation:
If you protect a file, your app must be prepared to lose access to
that file. When complete file protection is enabled, your app loses
the ability to read and write the file’s contents when the user locks
the device.
Now, as soon as I read that, I was almost positive that this was the cause for my app not being able to continue playback once the device was locked. For security's sake, iOS seems to default to automatically putting files under protection… but this keeps everything, including your app, from being able to access it while the device is locked. My bug was that it only played part of the song as soon as the device was locked, and that makes sense… since its access to the actual song file was revoked, it was only able to play the part of the song that had already been buffered into RAM!
This was the fix for a month-long problem I've been pulling my hair out to try to solve. I sincerely hope it fixes the issue for you too.
I believe this issue is caused by the limitation of AVQueuePlayer, and HysteriaPlayer using AVQueuePlayer as core player so that's why.
I've seen AVQueuePlayer document mentioned (I can't find it unfortunately) that it can't handle the queue mixed with local and remote audios.
There's some workarounds:
Use AVPlayer
Don't use AVQueuePlayer, handle the queue by yourself and feed it to AVPlayer.
Separate two AVQueuePlayer
One for local media, one for remote media. Insert and use PlayerItem and Player properly.
I would patch HysteriaPlayer to meet this requirement a month later, you can come back that time.
I have an app that requires that the user record their own message to be played back at some future time. My intent was to do that using UILocalNotification. Unfortunately, it seems that the sound associated with the local notif has to be stored in the main bundle but the user cannot make a recording and save it in the bundle.
How do I get a user recorded sound file to be played via local notification?
Alternatively - is there a way if the app is not running to capture the local notification (without waiting for the user to respond to an alert) and then play the desired soundfile?
Thanks!
You can assign a (custom) sound to a UILocalNotification by setting its property as follows:
localNotification.soundName = #"MySoundFile.wav";
Unfortunately, according to the reference, it is not possible to use a sound file that is not stored in the main bundle or is declared by apple:
For this property, specify the filename (including extension) of a sound resource in the application’s main bundle or UILocalNotificationDefaultSoundName to request the default system sound.
See also UILocalNotification Class Reference #soundName
I think this is possible for ios 9, this is what apple documentation says:
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.
I tested saving a sound into the Library/Sounds folder then using it with a local notification and it worked fine on iOS 9, after that I tried the same on iOS 8 and it didn't work, so my conclussion was that this is possible for iOS 9 only
You can access the library directory in this way:
let fileManager = NSFileManager.defaultManager()
let libraryPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
let soundsPath = libraryPath + "/Sounds"
You have to create the directory if it doesn't exist:
fileManager.createDirectoryAtPath(soundsPath, withIntermediateDirectories: false, attributes: nil)
and you can then save your sounds there.
The local notification's soundname property can refer to the path of a sound file within the app's bundle.
A sound recording of the user's voice cannot be used as it cannot be saved to the app bundle at runtime.
You cannot have a default notification show up and then open the app and play the recorded voice, unless the user has tapped on the notification.
Which means, if the user is not using the phone, the only chance that they will listen to the recording is if they pick up the phone, unlock it, and tap on the notification. So I believe that this is far from what you want.