Add System Sound to iOS remote notification in UNNotificationServiceExtension - ios

I am successfully modifying my remote notification payload using the UINotificationServiceExtension.
I want to change the alert sound based on the user's choice here. In order to do that I need to assign a UINotificationSound object to the bestAttemptContent:
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "alertTone.caf"))
This will work for tones which I have created and imported into my project. One named alertTone.caf in the above example.
However, I want to use the system alert tones instead of my own. I know I can set the default tone but I want access to other custom tones. Is there anyway at all to do this?

You can't reference the system sounds for UNNotificationSound, but you can try to play the sound when you receive the notification in the service extension.
// import
import AVFoundation
// create a sound ID, in this case its the tweet sound.
let systemSoundID: SystemSoundID = 1016
// Play the correct sound when the notification is received in the service extension
AudioServicesPlaySystemSound (systemSoundID)
Source for playing system sounds here.
This is a list of all the system sound ids that you can use for reference.

Related

No audio when iOS ring/silent switch is set to silent mode

Issue: When the iOS device is on silent, the sound being played from the app is muted.
Upon some digging into the iOS code, I found that out of the 7 "audio session categories" the right one to use for a music app is playback.
Question: How do I set the category in the audio_service package?
Package version: 0.18.0-beta.1
audio_service only manages remote control of your app via notifications, lock screens, etc. The audio session is typically managed by the audio player plugin that you use.
If you use just_audio, it will by default set the required category, but if not, you can manually override the category via the audio_session package. e.g. The code below will configure reasonable defaults for a podcast app, including setting the category to playback:
(await AudioSession.instance).configure(const AudioSessionConfiguration.speech());
This library helped me to solve this problem: https://pub.dev/packages/audio_session
import 'package:audio_session/audio_session.dart';
...
final session = await AudioSession.instance;
await session.configure(AudioSessionConfiguration.music());
...

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.
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.

Get the soundName by soundID to assign it to a UILocalNotification

I have a UILocalNotification, and I don't want to set it to use a default tone. I have used AudioToolbox.framework, and I have a SystemSoundID by referring to this library: https://github.com/TUNER88/iOSSystemSoundsLibrary.
But UILocalNotification needs a sound name. How can I get the name from the SystemSoundID?
Description of soundName in UILocalNotification Class Reference mentions
For this property, specify the filename (including extension) of a
sound resource in the app’s main bundle or
UILocalNotificationDefaultSoundName to request the default system
sound.
So if you want the default local notification sound use UILocalNotificationDefaultSoundName.
Also, a note from Multimedia Programming Guide
Note: System-supplied alert sounds and system-supplied user-interface
sound effects are not available to your application.
So you cannot use any system sound id you find in iOSSystemSoundsLibrary for this.
If you want a custom sound, you'll have to provide your own.

How to assign device alert tones as notitfication sound in iPhone

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

How can I play a recorded sound using a local notification?

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.

Resources