17
votes

I'm using UILocalNotification in an application.

In the application there are two sounds which are played conditionally- I have applied proper conditions for them.

But when I install the application and run it on an iOS 7 device, then it fires the local notification but a sound is not playing in the application.

Code is given below to set the notification:

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (localNotification == nil)
        return;
    localNotification.fireDate = [pickerView date];

    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    if (alarm_number == 1) {
        localNotification.alertBody = [NSString stringWithFormat:@"First Alarm"];
    }
    else
    {
        localNotification.alertBody = [NSString stringWithFormat:@"Second Alarm"];
    }

    localNotification.alertAction =@"Ok";

    NSString *message = [[NSString alloc]initWithString:@""];

    if(alarm_number == 1)
    {
        localNotification.soundName=@"Alarm_1.mp3";
        message = @"First Alarm Scheduled";
    }
    else
    {
        localNotification.soundName=@"Alarm_2.mp3";
        message = @"Second Alarm Scheduled";
    }

    localNotification.applicationIconBadgeNumber = 1;

    // Specify custom data for the notification
    NSString *alarmString;
    if (alarm_number==1) {
        alarmString = [NSString stringWithFormat:@"First Alarm"];
    }
    else
    {
        alarmString = [NSString stringWithFormat:@"Second Alarm"];
    }

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:alarmString forKey:@"AlarmFor"];

    localNotification.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [localNotification release];

What I have checked for is the Sound setting in the Settings/Notification Centre app for my app.

Please go through 1st to 3rd image to see what I have checked.

(1) Notification Center

(1) Notification Center

(2) Application

enter image description here

(3) Sound is off here

enter image description here

So, to enable this I have checked Inter App Audio at Capabilities in Targets of the application and it was Off as shown in the image below.

Capabilities in Inter-app audio

Capabilities in Inter-app audio

Then I have changed it to On and it looks like shown in the image below.

Capabilities after changed On

Yet, it still does not play any sound in iOS 7 devices.

Does anybody have any idea about why is it not working? It would be a great help.

Thanks.

9
AFAIK, it has nothing to do with Inter-App audioLior Pollak
But then can you please suggest me what is exact problem. I had already tried a lot. If you can help me.Jayeshkumar Sojitra
Have you changed the setting to sound on?Lior Pollak
Actually that is the real problem we can set sound On manually. But we want it On by default when we install application.Jayeshkumar Sojitra
It is user based setting. You can't change it- only the user can.Lior Pollak

9 Answers

10
votes

Did you try to turn off "Do not disturb" mode off? I have the same problem, then I found out "Do not Disturb" is on. After turn off that, it all works the right way.

3
votes

try using .caf file instead of .mp3

afconvert -f caff -d LEI16@44100 -c 1 wolf.wav wolf_out.caf
2
votes

My guess is your sound isn't formatted appropriately to play by the OS. Make sure your sound is formatted in IMA4 format. Also, make sure your file is in your application bundle, and you should be good to go.

For more reference and similar question, see this SO question.

Choose custom sound for Local Notifications

Here is a link for Apple's Local and Push Notification Guide. It explains the sounds in detail and the default sounds that can be used.

1
votes

I ran into the same issue, I had to do the following steps:

  1. Convert the file from mp3 or wav to caf. As stated in other answers you can convert wav, aif and mp3 files in terminal using the command:

    afconvert -f caff -d LEI16@44100 -c 1 in.mp3 out.caf

  2. You can also export with Quicktime 7 as .aif with the following settings Linear PCM Rate 44.100 Linear PCM sample size 16 these aif files seem to be supported.

  3. Make sure you files are less than 30 seconds, i.e 29 seconds or less.

  4. Add you files to the root of your bundle, you can drag drop into Xcode or right click "add files to (projectname)" Note you must select the following options: a)Destination Copy items if needed b)Add to targets (your build name(s)) 4)Reference the file by its name including the extension e.g notif.soundName = @"sound.caf";

Make sure you delete the app from the device / simulator between tests or you will not see the results of your changes.

If your still having problems, check the sound file file is in the root of the project. Check the Sound file "Target Membership" is selected in the show file inspector.

0
votes

Please try first whether setting->notificationcenter->sounds is not off.Then check whether in setting->sounds->slider is not on zero.That means if u have sounds on zero that will not allow sound to come up. Hope that helps.

0
votes

Check that the "Sound Effects" is not mute or low. The volume may be high but if the Sound Effects volume is mute then the local notification may not sound. U can increase it when you are in the Home screen of iOS and then press the volume up button.

0
votes

If mp3 file is edited with some tool, it is possible that something was missed by edit tool.

I had similar problem while trying to play mp3 file as local notification sound. Sound was shorter than 30 seconds, it was bundled, everything appears regular on the surface. After few hours i have realised that problem was caused by online mp3 cutter tool. I have find better editor, and file became playable on notification.

0
votes

Make sure the sound is added to the main bundle! enter image description here

0
votes

I didn't get any idea why it did work.

But, at last I have solved my problem by making a new project in Xcode 5.0 and ported old project to this new project. So it's working and I can get sound too.