ichthyocentaurs
ichthyocentaurs

Reputation: 2175

iOS Unable to play custom sound for remote notifications

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.

  1. How to play custom sound file when user get push notification?

  2. Custom sounds in remote notifications iOS 10, swift 3

  3. Change push notification sound

Upvotes: 2

Views: 5637

Answers (3)

Frank Eno
Frank Eno

Reputation: 2649

Swift 5 answer, you can do that with AudioToolbox, ideal for short sound clips:

  1. Play a default System sound (here's a list of sound codes: http://iphonedevwiki.net/index.php/AudioServices)

    import AudioToolbox

    AudioServicesPlaySystemSound(1103)



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

vaibhav
vaibhav

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

  1. For custom sound file must be exist in your Xcode project in resources.
  2. Make sure sound file duration no longer than 30 seconds. Otherwise it will play default sound.
  3. And you must add the below key UILocalNotificationDefaultSoundName inside .plist file. See below image:

enter image description here

Upvotes: 5

shallowThought
shallowThought

Reputation: 19602

Remove the AVPlayer stuff in didReceiveRemoteNotification.

Make sure:

  • The sounds length is < 25 seconds
  • The file really is in one of the supported formats (aiff, wav, caf)
  • The file really exists whitin your bundle (Copy Bundle Resources)
  • 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

Related Questions