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.
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 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.
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.
How to get only Alert tones of device (not all the device sounds) and select one of them as my app notification.
I've gone through Apple documentation which states that we cannot use System Sounds for our app.But, in WhatsApp you can see this feature.
Please help me out to solve this issue.
The aps dictionary contains a property that specify a sound to play:
{
"aps" : {
"alert" : "You got a push.",
"badge" : 9,
"sound" : "bingbong.aiff" //here
}
}
You have to embed the sound file in your app, if you like the systems sounds, then try to make your own.
Documentation
The name of a sound file in the application bundle. The sound in this
file is played as an alert. If the sound file doesn’t exist or default
is specified as the value, the default alert sound is played. The
audio must be in one of the audio data formats that are compatible
with system sounds; see “Preparing Custom Alert Sounds” for details.
Note: You Not put Alert Notification sound means Its default Take System(iPhone or iPad) Message Tone.
You can't get List of Tones from iPhone.Apple not allowed this. WhatsApp may be used for copy the tones and add their resource file.
Reference:
Access apple's iOS ringtones and display them
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.