Reputation: 2175
I have been trying to implement a custom sound for notifications on iOS.
According to Apple's documentation in example 3 here. All we need to do is to ensure that the Push notification payload should be something like:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
}
}
And bingbong.aiff has to be placed in the application bundle.
Also if the App is in active state I was advised to add the following code:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"bingbong" ofType:@"aiff"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
theAudio.delegate = self;
[theAudio play];
}
}
"bingbong.aiff"
is placed in "SupportingFiles"
I still donot get the custom sound for remote notification.
I have already gone through other SO Questions similar to this but these are not different from what I am doing.
Upvotes: 2
Views: 5637
Reputation: 2649
Swift 5 answer, you can do that with AudioToolbox, ideal for short sound clips:
Play a default System sound (here's a list of sound codes: http://iphonedevwiki.net/index.php/AudioServices)
import AudioToolbox
AudioServicesPlaySystemSound(1103)
Play a sound file that you previously dragged into your Xcode project:
var soundID:SystemSoundID = 0
let filePath = Bundle.main.path(forResource: "file_name", ofType: "wav") // or mp3, it depends by your sound file's extension
let soundURL = URL(fileURLWithPath: filePath!)
AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
AudioServicesPlaySystemSound(soundID)
Upvotes: 0
Reputation: 4096
For Default sound:
According to the apple documentation & guidelines to play default sound you must have to send "Default" value in sound file. Other wise it will not play default sound either.
For custom sound:
You need to pass custom sound file name in place of default
inside your php script
**.
Xcode
project in
resources.UILocalNotificationDefaultSoundName
inside .plist
file. See below image:Upvotes: 5
Reputation: 19602
Remove the AVPlayer
stuff in didReceiveRemoteNotification
.
Make sure:
You registered for push notifications:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
The name matches exactly to the file name:
{
"aps" : {
"alert" : "message",
"badge" : 1,
"sound" : "name.aiff"
}
}
Upvotes: 5